23

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.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
BobK
  • 860
  • 6
  • 12

3 Answers3

40

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

BobK
  • 860
  • 6
  • 12
  • JFC... Hooray! Why or WHY is this not mentioned in any of the user info / etc guides? It's crazy I wasted almost an hour to find your answer (ty for however much time you spent searching and actually finding the answer...) – Ajax Jun 29 '22 at 21:56
2

https://api.slack.com/methods/auth.test

make sure that you are using User token instead of Bot token.

user token screeshot in slack API

  async function findUserId(): Promise<string | undefined> {
    try {
      const result = await app.client.auth.test({
        token: SLACK_USER_OAUTH_TOKEN,
      });

      return result.user_id;
    } catch (error) {
      console.error(error);
    }
  }
Community
  • 1
  • 1
1

When your app is installed to a workspace through OAuth you will get a specific bot token together with the bot ID for that workspace.

Here is an example of the response from Slack with the bot token and bot user ID:

{
    "access_token": "xoxp-XXXXXXXX-XXXXXXXX-XXXXX",
    "scope": "incoming-webhook,commands,bot",
    "team_name": "Team Installing Your Hook",
    "team_id": "XXXXXXXXXX",
    "user_id": "XXXXXXXXXX",
    "incoming_webhook": {
        "url": "https://hooks.slack.com/TXXXXX/BXXXXX/XXXXXXXXXX",
        "channel": "#channel-it-will-post-to",
        "configuration_url": "https://teamname.slack.com/services/BXXXXX"
    },
    "bot":{
        "bot_user_id":"UTTTTTTTTTTR",
        "bot_access_token":"xoxb-XXXXXXXXXXXX-TTTTTTTTTTTTTT"
    }
}

Note that the response will include also include the user ID of the user who installed your app (although that is not shown in the official documentation). Make sure to use the bot_user_id, not the user_id.

See here for the official documentation on bot tokens.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • This seems to apply only when my app is added to another workspace and uses OAuth to authorize. However, my app is private a credentials (bot access token & signing secret) were obtained on the app admin page. – BobK Feb 01 '19 at 14:47
  • yes, my solution only works if your are using Oauth to install your app to a workspace. – Erik Kalkoken Feb 01 '19 at 17:40