// Name: Post Code Snippet to Slack // Description: Write code in editor and post to selected Slack channel // Author: johnlindquist import '@johnlindquist/kit' const SLACK_TOKEN = await env('SLACK_BOT_TOKEN', { hint: 'Get your Slack Bot Token from https://api.slack.com/apps', secret: true, }) // Get the code snippet from the editor const codeSnippet = await editor({ placeholder: 'Write your code snippet here...', language: 'javascript', hint: 'Press Cmd+S to continue to channel selection', }) if (!codeSnippet.trim()) { await div(md('No code snippet provided. Exiting...')) exit() } // Fetch Slack channels const channelsResponse = await get('https://slack.com/api/conversations.list', { headers: { 'Authorization': `Bearer ${SLACK_TOKEN}`, 'Content-Type': 'application/json', }, params: { types: 'public_channel,private_channel', exclude_archived: true, }, }) if (!channelsResponse.data.ok) { await div(md(`Error fetching channels: ${channelsResponse.data.error}`)) exit() } const channels = channelsResponse.data.channels .filter(channel => channel.is_member) .map(channel => ({ name: `#${channel.name}`, value: channel.id, description: channel.purpose?.value || 'No description', })) if (channels.length === 0) { await div(md('No channels found. Make sure your bot is added to channels.')) exit() } // Let user select a channel const selectedChannelId = await arg('Select a Slack channel:', channels) // Post the code snippet to Slack const postResponse = await post('https://slack.com/api/chat.postMessage', { channel: selectedChannelId, text: '```\n' + codeSnippet + '\n```', }, { headers: { 'Authorization': `Bearer ${SLACK_TOKEN}`, 'Content-Type': 'application/json', }, }) if (postResponse.data.ok) { const selectedChannel = channels.find(ch => ch.value === selectedChannelId) await toast(`Code snippet posted to ${selectedChannel.name}!`) } else { await div(md(`Error posting message: ${postResponse.data.error}`)) }