【问题标题】:Creating Slack Bot to Answer Request创建 Slack 机器人来回答请求
【发布时间】:2015-03-06 06:23:42
【问题描述】:

我想创建一个 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 吗?

【问题讨论】:

    标签: python slack-api


    【解决方案1】:

    如您所见here,此调用接受一个可以为真的参数“as_user”。如果您将其设置为 true,则消息将作为您创建的机器人发布。

    【讨论】:

      【解决方案2】:

      我现在也在创建一个机器人。我发现如果你指定as_user='true',它会以你的身份发布,这个经过身份验证的用户。如果您希望它成为您的机器人,请输入我们的机器人名称和其他选项,例如表情符号,如下所示:

      sc.api_call(
          'chat.postMessage',
          username='new_slack_bot',
          icon_emoji=':ghost:',
          as_user='false',
          channel='magic',
          text='Hello World!'
      )
      

      查看emoji cheat sheet了解更多信息。

      然后,如果您想收听事件,例如问题或命令,请尝试拦截发送的消息。示例位于 this post

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        • 2017-11-26
        • 2022-01-02
        • 2021-10-20
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        相关资源
        最近更新 更多