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 吗?

4

2 回答 2

4

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

于 2015-04-04T23:06:19.493 回答
4

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

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
于 2015-11-14T19:14:27.050 回答