【发布时间】:2020-01-22 04:23:19
【问题描述】:
我目前正在尝试将自定义 Slack 机器人集成到我的一个频道中。但是,我遇到了一个问题,即机器人不是以自定义机器人用户的身份发布,而是以我的身份发布。
机器人响应我的自定义命令和内容,但由于某种原因没有作为我设置的机器人发布。我正在使用设置机器人时提供给我的 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 只是“@”字符和我的机器人名称的常量。
【问题讨论】:
-
相关代码已添加,抱歉