2017-03-03 68 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