【问题标题】:Custom Slack Bot Not Posting as User自定义 Slack 机器人未以用户身份发布
【发布时间】:2020-01-22 04:23:19
【问题描述】:

我目前正在尝试将自定义 Slack 机器人集成到我的一个频道中。但是,我遇到了一个问题,即机器人不是以自定义机器人用户的身份发布,而是以我的身份发布。

Image of bot test

机器人响应我的自定义命令和内容,但由于某种原因没有作为我设置的机器人发布。我正在使用设置机器人时提供给我的 API 令牌并将其添加到我当前正在测试的频道中。有人知道可能导致此问题的原因吗?

相关代码:

def handle_command(command, channel):
    """Receives commands directed at the bot and determines if they are valid commands. If so,
    then acts on the commands. If not, returns back what it needs for clarification.
    """
    response = "Hello there!"
    if command.startswith("yes"):
        response = "You posted 'yes'!"
    SLACK_CLIENT.api_call("chat.postMessage", channel=channel,
                          text=response, as_user=True)

def parse_slack_output(slack_rtm_output):
    """The Slack Real Time Messaging API is an events firehose. This parsing function returns
    None unless a message is directed at the Bot, based on its ID.
    """
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                # return text after the @ mention, whitespace removed
                return output['text'].split(AT_BOT)[1].strip().lower(), \
                       output['channel']
    return None, None


def main():
    """Obtains Google Credentials to rotate and update a Google Spreadsheet that keeps track of the
    current engineer with 10 percent time. Notifies the engeineering team through a Google Calendar
    event.
    """

    if SLACK_CLIENT.rtm_connect():
        print "Bot connected and running."
        while True:
            command, channel = parse_slack_output(SLACK_CLIENT.rtm_read())
            if command and channel:
                handle_command(command, channel)
            time.sleep(1)
    else:
        print "Connection failed."

SLACK_CLIENT 是使用 API 和给定令牌初始化的,AT_BOT 只是“@”字符和我的机器人名称的常量。

【问题讨论】:

  • 相关代码已添加,抱歉

标签: python slack-api slack


【解决方案1】:

我想可能是因为你设置了as_user=True

official documentation

传递 true 以作为授权用户而不是机器人发布消息。

您是否尝试将其设置为 false?

SLACK_CLIENT.api_call("chat.postMessage", channel=channel,
                      text=response, as_user=False)

【讨论】:

  • 感谢您的回复,我尝试将其设置为 false,但随后它发布为名为“Slack API Tester”的通用机器人,而不是我的实际机器人名称。它仍然会注册命令
  • 我遇到了同样的问题。因为我的 API 调用是用“Slack API Tester”的bot_id 发布的,所以我无法实际发布消息。
【解决方案2】:

此行为的另一个潜在原因可能是使用了哪个令牌。

当您使用机器人用户安装 Slack 应用程序时,您将始终获得两个访问令牌:用户访问令牌和机器人访问令牌。 (source)

如果您使用机器人访问令牌消息将始终显示机器人名称,而不管chat.postMessageas_user 设置如何。 (source)

【讨论】:

    【解决方案3】:

    传递 as_user="true" 而不是 as_user=True 对我有用。我不认为客户端对 Python 布尔值进行任何特殊处理/转换,API 可能期望文档中指出的“true”。

    as_usertrue 可选
    传递 true 以作为 authed 用户而不是机器人发布消息。默认为假。请参阅下面的作者身份。此参数不能与较新的机器人令牌一起使用。

    来自https://api.slack.com/methods/chat.postMessage

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 2015-06-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2020-04-16
      • 2022-12-07
      相关资源
      最近更新 更多