Slacking with a bot
• Mark Eschbach
So I’ve got a basic SlackBot going in NodeJS. Fairly decent. Now I want my slack bot to communicate with only specific people on specific matters. The easiest dynamic content I can think of is to have th bot reply with the time. Looks like there is a resource user.list which supports it.
I’m not entirely sure how to invoke resource or methods yet. So to the offical Slack Node client source!. Hmm, the real time client doesn’t seem like much help here. I couldn’t find any reference to the User model. Perhaps I could just go straight for the WebClient.
Oh hey! They have actual human documentation. Us huamns are so horrible we can’t just follow the code. Well that confirms the RTMClient
doesn’t offer the service I need. Their documentation on the SlackDataStore leaves me a bit wanting. It offers the services one might expect to be able to find a user. You may even get the datastore from the RTMClient. Their official documentation states it’s just a cache. Let’s see what information we can get out of it.
let rtm = new RtmClient( token );
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
console.log( "Start data users: ", rtmStartData.users )
});
Ah! That dumps out an entity similar to:
{
id: 'U134324',
name: 'example-user'
}
Well, that is what I was looking for. Sweet! I’m guessing that is how it’s populated. So let’s see if I can send a direct message on load. Hmm, using rtm.sendMessage( "some message", user.id )
failed there. According to this SO question it should as easy as using the "@user"
for the channel. Unsurprisingly that didn’t work. I wonder if there is a list of direct messages. There is a model for direct messages. Perhaps it’s on the initialization data? Nope, that doens’t exist on the startup info object either.
Okay, so dumping the information gives an interesting element ims
. This looks promising. Each element in the array contains a number of properties, however the two which look interesting are id
and user
. I think user
property directly corrolates to the id
of the users. Slack’s memory implementation of the bot cache interface provides a convience method getDMByName(name) which takes care of all this for me. So let’s try that. This returns an object with a lot of information however the property of interest is id
which should hopefully give me the channel. BINGO! That worked.
Unforunately I’m now getting sycnrhonization errors from Slack regarding messages not having reply_to
. A bit unforunate and annoying. Apprently it’s a known issue. Next time I’ll look into deployment!