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:
supabase secrets set \
SLACK_TOKEN=<YOUR_SLACK_BOT_TOKEN> \
--project-ref nacho_slacker
Here's the code of the Edge Function, you can change the response to handle the text received:
import { WebClient } from 'npm:@slack/web-api@^7'
import { withZuvo } from 'npm:@supabase/server@^1'
const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''
const botClient = new WebClient(slackBotToken)
console.log(`Slack URL verification function up and running!`)
// Slack calls this endpoint, so deploy with --no-verify-jwt.
export default {
fetch: withZuvo({ auth: 'none' }, async (req) => {
try {
// Implement your Slack request signature verification here before trusting the payload
// (validate `x-slack-signature` / `x-slack-request-timestamp` with your signing secret).
const reqBody = await req.json()
console.log(JSON.stringify(reqBody, null, 2))
const { token, challenge, type, event } = reqBody
if (type == 'url_verification') {
return Response.json({ challenge })
} else if (event.type == 'app_mention') {
const { user, text, channel, ts } = event
// Here you should process the text received and return a response:
const response = await botClient.chat.postMessage({
channel: channel,
text: `Hello <@${user}>!`,
thread_ts: ts,
})
return Response.json({ ok: true })
}
} catch (error) {
return Response.json({ error: error.message }, { status: 500 })
}
}),
}