Recently, we officially switched from Hipchat to Slack at work. I wanted to make a simple slack bot that can help me to choose whom to pick on for PR reviews. I made a randomizer that takes in a list of names and outputs two names.
npm init
npm install @slack/client --save
var token = 'YOUR SLACK BOT API KEY' || '';
var RtmClient = require('@slack/client').RtmClient;
var _ = require('lodash')
var rtm = new RtmClient(token, { logLevel: 'debug' });
rtm.start();
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
// Listens to all `message` events from the team
rtm.on(RTM_EVENTS.MESSAGE, function(message) {
console.log(message, 'message')
var str = message.text;
// <@U22QZJNKA> is the my bot id. I am checking to see if someone @ my bot
var n = str.includes("<@U22QZJNKA>")
var array = str.split(', ');
var splicedArray = array.splice(1);
var lucky;
var msg
if (splicedArray.length > 1) {
lucky = _.sampleSize(splicedArray, 2)
msg = ':laser_cat: meow meow :thinking_face: ??? ' + lucky[0] + ' & ' + lucky[1] + ' today is your lucky day RUFFF RUFFFF :doge:!! :partyparrot: :beers::dealwithitparrot::hypnotoad: '
//get rid of the emojis if you don't have them.
}
var params = {
text: msg,
channel_id: 'G20TSFYAX', //My channel id. You should switch to whatever channel id you want the bot to be in.
}
if (n) {
// This will send the message to the channel identified by id 'G20TSFYAX'
if (lucky) {
rtm.sendMessage(params.text, params.channel_id, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
} else {
params.text = 'meow meow :thinking_face: ???'
rtm.sendMessage(params.text, params.channel_id, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
}
});
nodemon app.js
(if you have nodemon installed) or node app.js
The bot will work as long as you have it running it locally or deployed somewhere.