Home

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:


_10
supabase --project-ref nacho_slacker secrets \
_10
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:

index.ts

_35
import { serve } from 'https://deno.land/std@0.197.0/http/server.ts'
_35
import { WebClient } from 'https://deno.land/x/slack_web_api@6.7.2/mod.js'
_35
_35
const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''
_35
const botClient = new WebClient(slackBotToken)
_35
_35
console.log(`Slack URL verification function up and running!`)
_35
serve(async (req) => {
_35
try {
_35
const reqBody = await req.json()
_35
console.log(JSON.stringify(reqBody, null, 2))
_35
const { token, challenge, type, event } = reqBody
_35
_35
if (type == 'url_verification') {
_35
return new Response(JSON.stringify({ challenge }), {
_35
headers: { 'Content-Type': 'application/json' },
_35
status: 200,
_35
})
_35
} else if (event.type == 'app_mention') {
_35
const { user, text, channel, ts } = event
_35
// Here you should process the text received and return a response:
_35
const response = await botClient.chat.postMessage({
_35
channel: channel,
_35
text: `Hello <@${user}>!`,
_35
thread_ts: ts,
_35
})
_35
return new Response('ok', { status: 200 })
_35
}
_35
} catch (error) {
_35
return new Response(JSON.stringify({ error: error.message }), {
_35
headers: { 'Content-Type': 'application/json' },
_35
status: 500,
_35
})
_35
}
_35
})