Node-slack-sdk: @-mentions

Created on 1 May 2016  路  5Comments  路  Source: slackapi/node-slack-sdk

Is there an easy way to detect @-mentions without using a higher level kit like howdy? I hacked something together by capturing the bot's ID on the rtm.start event (as in the Readme) and shoving it into a regex... but that feels really brittle. I don't see "mention" anywhere in any of the event enumerations.

Most helpful comment

rtm.activeUserId should contain the ID you're looking for: https://github.com/slackhq/node-slack-client/blob/b4f18a482e7a34d3d03c89f221e61a669c94b643/lib/clients/rtm/client.js#L267

rtm.on(RTM_EVENTS.MESSAGE, message => {
  if (message.text.includes(`<@${rtm.activeUserId}>`)) {
    // handle message
  }
});

Use text.indexOf/text.match if you're in an environment without .includes.

All 5 comments

Detecting @-mentions meaning, towards the bot/user account running the client or just a message mentioning a user?

Towards the bot/user account running the client is what I had in mind.

I honestly think that's your best bet: grabbing the ID on startup, then using it against incoming messages from Slack, like you said. You probably could leave this open to see if anyone else can weigh in, but I don't believe there's any mention events just yet.

rtm.activeUserId should contain the ID you're looking for: https://github.com/slackhq/node-slack-client/blob/b4f18a482e7a34d3d03c89f221e61a669c94b643/lib/clients/rtm/client.js#L267

rtm.on(RTM_EVENTS.MESSAGE, message => {
  if (message.text.includes(`<@${rtm.activeUserId}>`)) {
    // handle message
  }
});

Use text.indexOf/text.match if you're in an environment without .includes.

馃憤 thanks @ekmartin, that works great!

Was this page helpful?
0 / 5 - 0 ratings