Edge Functions

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:

  1. Navigate to the Slack Apps page.
  2. Under "Event Subscriptions," add the URL of the slack-bot-mention function and click to verify the URL.
  3. The Edge function will respond, confirming that everything is set up correctly.
  4. Add app-mention in the events the bot will subscribe to.

Creating the Edge Function#

Deploy the following code as an Edge function using the CLI:

1
supabase --project-ref nacho_slacker secrets \
2
set SLACK_TOKEN=<xoxb-0000000000-0000000000-01010101010nacho101010>

Here's the code of the Edge Function, you can change the response to handle the text received:

1
import { WebClient } from 'https://deno.land/x/slack_web_api@6.7.2/mod.js'
2
3
const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''
4
const botClient = new WebClient(slackBotToken)
5
6
console.log(`Slack URL verification function up and running!`)
7
Deno.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 } = reqBody
12
13
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 } = event
20
// 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
})