创建Slack机器人以回答请求

4
我想创建一个Slack机器人,用于回答一些简单的问题或在服务器上执行某些任务。以下是我尝试过的方法。
token = "i-put-my-bot-token-here"      # found at https://api.slack.com/#auth)
sc = SlackClient(token)

sc.api_call("chat.postMessage", channel="magic", text="Hello World!")

这条消息是由Slackbot发出的,而不是我创建的机器人账户发出的?

另外,如果我想要听取该消息,根据Python库的说法:

if sc.rtm_connect():
    while True:
        print sc.rtm_read()
        time.sleep(1)
else:
    print "Connection Failed, invalid token?"

我应该使用传入的Webhook吗?
2个回答

4
我正在创建一个机器人。我发现,如果你指定了as_user='true',它将会作为你,即已认证的用户发布。如果你想让它成为你的机器人,请传入你的机器人名称和其他选项,例如表情符号,像这样:
sc.api_call(
    'chat.postMessage',
    username='new_slack_bot',
    icon_emoji=':ghost:',
    as_user='false',
    channel='magic',
    text='Hello World!'
)

查看表情符号秘笈以获取更多信息。

然后,如果您想监听事件,例如问题或命令,请尝试拦截发送的消息。在此文章中找到示例:

while True:
    new_evts = sc.rtm_read()
    for evt in new_evts:
      print(evt)
      if "type" in evt:
        if evt["type"] == "message" and "text" in evt:    
          message=evt["text"]
          # write operations below to receive commands and respond as you like

4

正如您可以在这里看到的,这个调用接受一个“as_user”的参数,如果将其设置为true,则消息将以您创建的机器人身份发布。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接