简体   繁体   中英

Receive message from slack bot

I am quite confused on Slack bot using Python. I want to be able to receive messages sent to by bot:

client = SlackClient(API_TOKEN)

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

Then I send a message to my bot in slack. But I don't get the message. How do I start sending message to my user?

Also, is it possible to include the bot in all channels? So if I'm in any channel, I want to be able to say @myBot args 123

Slack only sends events to bots who are joined to a channel and by default they are not joined to any channels and receive events only for private messages. If you invite your bot to the channel then Slack should start routing events from the channel to it.

As for the second part of your question, mentions (eg, @bot ) are not special events and you must check each incoming event to see if it is message that is directed at your bot (see this answer . One gotcha is you need to know the ID of your bot as the event will contain something like <@YOURBOTID> args 123 . Finding the ID of your bot and other useful tips for building a python slack bot can be found in this article .

There are many types of events in slack, if they have been subscribed by your BOT, then your BOT will definitely get related traffic on your request URL.

So, if you want to receive any message that has been directly sent to your BOT, you have to subscribe direct message event.

And if you want your BOT triggered by @bot .. kind of messages, then you need to subscribe app_mention event. Your BOT would receive traffic if it has been mentioned in any group but it can post a message to the only group in which it is allowed to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM