Slack Bot Mention Edge Function
The Slack Bot Mention Edge Function allows you to process mentions in Slack and respond accordingly.
Configuring Slack apps#
For your bot to seamlessly interact with Slack, you'll need to configure Slack Apps:
- Navigate to the Slack Apps page.
- Under "Event Subscriptions," add the URL of the
slack-bot-mentionfunction and click to verify the URL. - The Edge function will respond, confirming that everything is set up correctly.
- Add
app-mentionin the events the bot will subscribe to.
Creating the Edge Function#
Deploy the following code as an Edge function using the CLI:
1supabase --project-ref nacho_slacker secrets \2set SLACK_TOKEN=<xoxb-0000000000-0000000000-01010101010nacho101010>Here's the code of the Edge Function, you can change the response to handle the text received:
1import { WebClient } from 'https://deno.land/x/slack_web_api@6.7.2/mod.js'23const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''4const botClient = new WebClient(slackBotToken)56console.log(`Slack URL verification function up and running!`)7Deno.serve(async (req) => {8 try {9 const reqBody = await req.json()10 console.log(JSON.stringify(reqBody, null, 2))11 const { token, challenge, type, event } = reqBody1213 if (type == 'url_verification') {14 return new Response(JSON.stringify({ challenge }), {15 headers: { 'Content-Type': 'application/json' },16 status: 200,17 })18 } else if (event.type == 'app_mention') {19 const { user, text, channel, ts } = event20 // Here you should process the text received and return a response:21 const response = await botClient.chat.postMessage({22 channel: channel,23 text: `Hello <@${user}>!`,24 thread_ts: ts,25 })26 return new Response('ok', { status: 200 })27 }28 } catch (error) {29 return new Response(JSON.stringify({ error: error.message }), {30 headers: { 'Content-Type': 'application/json' },31 status: 500,32 })33 }34})