边缘函数

Slack机器人提及边缘函数


Slack 机器人提及边缘函数允许您处理 Slack 中的提及并做出相应响应。

配置 Slack 应用

为了让您的机器人能够无缝与 Slack 交互,您需要配置 Slack 应用:

  1. 访问 Slack 应用页面
  2. 在"事件订阅"下,添加 slack-bot-mention 函数的 URL 并点击验证该 URL
  3. 边缘函数将响应,确认一切设置正确
  4. 在机器人订阅的事件中添加 app-mention

创建边缘函数

使用 CLI 部署以下代码作为边缘函数:

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

以下是边缘函数的代码,您可以修改响应逻辑来处理接收到的文本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { WebClient } from 'https://deno.land/x/slack_web_api@6.7.2/mod.js'const slackBotToken = Deno.env.get('SLACK_TOKEN') ?? ''const botClient = new WebClient(slackBotToken)console.log(`Slack URL verification function up and running!`)Deno.serve(async (req) => { try { const reqBody = await req.json() console.log(JSON.stringify(reqBody, null, 2)) const { token, challenge, type, event } = reqBody if (type == 'url_verification') { return new Response(JSON.stringify({ challenge }), { headers: { 'Content-Type': 'application/json' }, status: 200, }) } else if (event.type == 'app_mention') { const { user, text, channel, ts } = event // 在这里您应该处理接收到的文本并返回响应: const response = await botClient.chat.postMessage({ channel: channel, text: `你好 <@${user}>!`, thread_ts: ts, }) return new Response('ok', { status: 200 }) } } catch (error) { return new Response(JSON.stringify({ error: error.message }), { headers: { 'Content-Type': 'application/json' }, status: 500, }) }})