Skip to content
Edge Functions

Slack 机器人提及 Edge 功能

Slack 机器人提及边缘功能可以让你处理 Slack 中的提及并作出相应回应。

🌐 The Slack Bot Mention Edge Function allows you to process mentions in Slack and respond accordingly.

配置 Slack 应用 #

🌐 Configuring Slack apps

为了让你的机器人与 Slack 无缝互动,你需要配置 Slack 应用:

🌐 For your bot to seamlessly interact with Slack, you'll need to configure Slack Apps:

  1. 前往 Slack 应用页面。
  2. 在“事件订阅”下,添加 slack-bot-mention 函数的 URL,然后点击验证 URL。
  3. Edge 功能会响应,确认一切都已正确设置。
  4. 在机器人将要订阅的事件中添加 app-mention

创建边缘功能 #

🌐 Creating the Edge Function

使用 CLI 将以下代码部署为 Edge 函数:

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

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

这是 Edge Function 的代码,你可以更改响应来处理收到的文本:

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

1
import { WebClient } from 'npm:@slack/web-api@^7'
2
import { withSupabase } from 'npm:@supabase/server@^1'
3
4
const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''
5
const botClient = new WebClient(slackBotToken)
6
7
console.log(`Slack URL verification function up and running!`)
8
9
// Slack calls this endpoint, so deploy with --no-verify-jwt.
10
export default {
11
fetch: withSupabase({ auth: 'none' }, async (req) => {
12
try {
13
// Implement your Slack request signature verification here before trusting the payload
14
// (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 } = reqBody
18
19
if (type == 'url_verification') {
20
return Response.json({ challenge })
21
} else if (event.type == 'app_mention') {
22
const { user, text, channel, ts } = event
23
// 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 Response.json({ ok: true })
30
}
31
} catch (error) {
32
return Response.json({ error: error.message }, { status: 500 })
33
}
34
}),
35
}