Creating a Slack Bot to respond to a request

I would like to create a Slack bot to answer a simple question or perform some task on the server. Here is what I tried

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!")

Was this published as Slackbot and not the bot account I created?

Also, if I had to listen to the message, according to the python library, it says

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

Or should I use an incoming webhook instead?

+4
source share
2 answers

As you can see here, this call accepts the as_user argument, which may be true. If you set it to true, messages will be published as the bot you created.

+4
source

. , as_user='true', , . , , , emoji:

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

emoji .

, , , . :

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

All Articles