slackslack-api

How can a slack bot find out its own id?


I am writing a slack bot that listens to all messages in a channel. His response should be vary based on whether the message mentions him or not. The problem is that I can not figure out how to "properly" find out bot's id. The bot is subscribed to message.channels event and when someone posts message mentioning the bot, I get this:

{"client_msg_id":"b94dcc57-d640-4815-9eeb-23c048564bf0",
 "type":"message",
 "text":"<@UFT98YYYY> how are you?",
 "user":"UD45QXXXX",
 "ts":"1548958008.003400",
 "channel":"CFK3AZZZZ",
 "event_ts":"1548958008.003400",
 "channel_type":"channel"}

What is the proper way of knowing that UFT98YYYY is bot's id? There are certainly ways of getting around this (look in the logs, set bot's id in the config), but they seem ugly, fragile and need some extra steps when installing the bot. Is there a Slack API call the bot can use to find out its id?

Btw, I know that I can subscribe to mentions only, but I need to listen&react to non-mentioning messages too.


Solution

  • Got it. The API method to use is auth.test, it returns user id (plus some extra fields).