使用机器人 post 向 Slack 发送消息

Using a bot to post a message to Slack

我有一个 slack 实例,我是唯一的用户(到目前为止)。我正在尝试使用机器人帐户 post 一条简单的 "hello world" 消息,这样消息就不会显示为来自我的帐户。

$url = "https://slack.com/api/chat.postMessage?token=xoxb-XXXXXX&channel=general&text=hello%20world";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, TRUE); 
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$head = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 

当我 post 使用 xoxp-XXXX oAuth 令牌时,效果很好,只是它来自我的帐户。当我 post 使用 xoxb-XXXX 令牌时,我收到 "error": "not_in_channel" 回复。我收到了 chat:write:user 用户范围错误 - 虽然我已经授予我的应用程序所有 50 个权限。

如何设置我的 slack 应用程序以允许从应用程序的 bot 帐户而不是我的帐户进行写入?

解决方法很简单。

  1. 您的机器人需要 bot 范围作为机器人用户。
  2. 确保您使用的是 bot access token
  3. 邀请机器人加入频道