2017-03-03 73 views
0

我目前正在尝试将自定义Slack bot整合到我的一个频道中。但是,我遇到了一个问题,即bot不是作为自定义bot用户发布的,而是作为我发布的。自定义Slack Bot不以用户身份发帖

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​​和给定的令牌初始化,并且​​是只为“@”字符和我的机器人的名字不变。

+0

告诉我们添加的代码 – depperm

+0

相关的代码,对不起 – AJwr

回答

1

我认为这可能是因为你在设置as_user=True

official documentation

传递true张贴消息作为authed用户,而不是作为一个机器人。

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

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

感谢您的答复,我想它设置为false,但随后的帖子作为所谓的“时差API测试仪”,而不是我的实际漫游器名称的通用机器人。它仍然注册了这些命令 – AJwr

0

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

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

如果使用机器人的访问令牌消息将始终与机器人的名字显示出来,不管as_user设置为chat.postMessage。 (source