Logo
Section 1 / 2

添加和移除表情反应

学习如何使用 Slack 的 API 调用来为特定消息添加和获取表情反应。

导入

添加和移除表情反应

学习如何使用 Slack 的 API 调用来为特定消息添加和获取表情反应。

概述

反应 是一种通过图标与消息互动的方式,但我们的应用程序也可以使用反应功能。例如,我们可以创建一个 Slack 应用程序来统计反应数量,并根据结果做出响应,就像一个投票应用一样。

本课中我们将了解以下两个端点:

  1. reactions.add:此端点用于向某项内容添加反应。
  2. reactions.remove:此端点用于从某项内容中移除反应。

添加反应

要添加反应,我们访问 https://slack.com/api/reactions.add 端点。

reactions.add 端点的一些重要查询参数如下:

参数类型必需性说明
tokentoken必需认证令牌携带了控制不同 Slack 应用和 API 使用权限的必要作用域。通常将这些令牌作为 HTTP Authorization 头或 POST 参数传递。
channelstring必需消息所在的频道 ID。
namestring必需反应(表情符号)的名称。
timestampstring必需要添加反应的消息的时间戳。

点击“运行”按钮发送一条消息到频道,然后在下一个代码块中使用该消息的时间戳来添加反应。

1import requests 2import json 3 4url = "https://slack.com/api/chat.postMessage" 5data = { 6 'channel': '{{CHANNEL_ID}}', 7 'text': '让我们添加一些反应!!🥳' 8} 9headers = { 10 'Authorization': 'Bearer {{Token}}', 11 'Content-Type': 'application/x-www-form-urlencoded' 12} 13 14response = requests.post(url, headers=headers, data=data) 15print(json.dumps(response.json(), indent=4))

接下来调用 reactions.add 端点。点击“运行”按钮,为之前发送的消息添加一个反应。

1import requests 2import json 3 4url = "https://slack.com/api/reactions.add" 5data = { 6 'name': 'eyes', 7 'channel': '{{CHANNEL_ID}}', 8 'timestamp': '{{ts}}' 9} 10headers = { 11 'Authorization': 'Bearer {{Token}}', 12 'Content-Type': 'application/x-www-form-urlencoded', 13} 14 15response = requests.post(url, headers=headers, data=data) 16print(json.dumps(response.json(), indent=4))
  • 第 5–9 行:我们指定了反应名称、希望发布消息的频道以及目标消息的时间戳。

移除反应

要移除反应,我们访问 https://slack.com/api/reactions.remove 端点。必须指定以下参数之一:file、file_comment,或者同时指定 channel 和 timestamp。

reactions.remove 端点的一些重要查询参数如下:

参数类型必需性说明
tokentoken必需认证令牌携带了控制不同 Slack 应用和 API 使用权限的必要作用域。通常将这些令牌作为 HTTP Authorization 头或 POST 参数传递。
namestring必需反应(表情符号)的名称。
channelstring可选频道 ID。
timestampstring可选消息的时间戳。
filestring可选文件 ID。
file_commentstring可选文件评论的 ID。

调用 reactions.remove 端点。点击“运行”按钮,移除上一个代码块中添加的反应。

1import requests 2import json 3 4url = "https://slack.com/api/reactions.remove" 5data = { 6 'name': 'eyes', 7 'channel': '{{CHANNEL_ID}}', 8 'timestamp': '{{ts}}' 9} 10headers = { 11 'Authorization': 'Bearer {{Token}}', 12 'Content-Type': 'application/x-www-form-urlencoded', 13} 14 15response = requests.post(url, headers=headers, data=data) 16print(json.dumps(response.json(), indent=4))
  • 第 5–9 行:我们指定了反应名称、频道 ID 和消息的时间戳,以移除该特定反应。

  • 第 15 行:我们调用了 reactions.remove 端点。

用户

列出和获取反应