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'2import { withSupabase } from 'npm:@supabase/server@^1'34const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''5const botClient = new WebClient(slackBotToken)67console.log(`Slack URL verification function up and running!`)89// Slack calls this endpoint, so deploy with --no-verify-jwt.10export default {11 fetch: withSupabase({ auth: 'none' }, async (req) => {12 try {13 // Implement your Slack request signature verification here before trusting the payload14 // (validate `x-slack-signature` / `x-slack-request-timestamp` with your signing secret).15 const reqBody = await req.json()16 console.log(JSON.stringify(reqBody, null, 2))17 const { token, challenge, type, event } = reqBody1819 if (type == 'url_verification') {20 return Response.json({ challenge })21 } else if (event.type == 'app_mention') {22 const { user, text, channel, ts } = event23 // Here you should process the text received and return a response:24 const response = await botClient.chat.postMessage({25 channel: channel,26 text: `Hello <@${user}>!`,27 thread_ts: ts,28 })29 return new Response('ok', { status: 200 })30 }31 } catch (error) {32 return Response.json({ error: error.message }, { status: 500 })33 }34 }),35}