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:


_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

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