Checking and removing SUSI Username Mentions in SUSI Slackbot

The SUSI Slackbot is a custom integration bot in slack. It responds to the user’s queries in the slack channels. It makes use of Slack Real Time Messaging APIs. In the background, it takes result from SUSI Server and also have some more added features. When the user mentions susi bot in the slack channels (eg. @asksusi), we know that the message is intended for susi. So now we need to remove the susi mention part, extract the rest of the message and send it to susi server. This blog explains how the messages are received from slack real time messaging API and how to remove the SUSI username mention.

Storing the bot’s self id

Before we can detect the susi username mention, we need to actually know the self id of the bot, which is a unique id assigned by the slack to the bot. Later, we use this id to detect the mention of susi.

var Slack = require('@slack/client');
var RtmClient = Slack.RtmClient;
var RTM_EVENTS = Slack.RTM_EVENTS;

var appData={};
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (connectData) => {
    // Cache the data necessary for this app in memory
    appData.selfId = connectData.self.id;
});

Receiving message and processing it

We will be receiving message from RTM API. The code for receiving the message is:

var Slack = require('@slack/client');
var RtmClient = Slack.RtmClient;
var RTM_EVENTS = Slack.RTM_EVENTS;

rtm.on(RTM_EVENTS.MESSAGE, function(message) {
var channel = message.channel;
var text=message.text;
//send reply only when mentioned or in direct message
if(text && message.user!==appData.selfId && (text.indexOf(appData.selfId)!==-1 || channel.startsWith('D'))){
    var susiMention='<@'+appData.selfId+'>';
    text=text.replace(susiMention,'');


The function passed as a callback executes only when we receive a new message. This message could be either a DM (Direct Message) or in a channel. We need to reply when the message mentions susi username or it is a DM. In the above code, we are checking if the message received is not from the bot itself, and is either from a DM or mentions susi username.
Here is a breakdown of the message object we receive in the function:

{ type: 'message',
  channel: 'C9CLRN4M7',
  user: 'U9D7JU15Y',
  text: '<@U9H78R274> hello!',
  ts: '1526413229.000321'}

 

  • Type: This tells the type of the message.
  • Channel: It contains the channel id of the slack channel where the message has been posted.
  • User: It contains the user id of the author.
  • Text: It contains the full text of the message, including mentions. We need to extract the mention part (<@U9H78R274>) and remove it.
  • Ts: ts is the unique (per-channel) timestamp.

Note: In case of DMs, the channel id will start with a “D”. In case of common channels, the channel id will start with “C”. That is how we can differentiate between a direct message and a message in a channel.

Thus we first store the bot’s self id. Then, upon receiving a message we check if the message is not from the bot itself, is a Direct Message or if it is from a channel, its mentions susi. Then only the susi bot replies to the message.

Result:

Resources

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.