Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Slack Bots for Fun and Profit

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Slack Bots for Fun and Profit

Given at Nodevember in 2016, learn how to design and build Slack bots easily using the Skellington library.

Avatar for Cole Furfaro-Strode

Cole Furfaro-Strode

November 20, 2016

More Decks by Cole Furfaro-Strode

Other Decks in Technology

Transcript

  1. const request = require('request')
 const Ws = require('ws')
 
 //

    use Slack web API to get an RTM API URL
 request({
 method: 'GET',
 uri: `https://slack.com/api/rtm.start`,
 json: true,
 qs: {
 token: process.env.SLACK_TOKEN
 }
 },(err, res, body) => {
 // use the url to open a new websocket
 const rtm = new Ws(body.url, null, {agent: null})
 let lastMessageId = 0
 
 // this is where the magic will happen
 rtm.on('message', (event) => {
 const message = JSON.parse(event) const replyMessage = {
 id: ++lastMessageId,
 type: 'message',
 channel: message.channel,
 text: 'GO CUBS'
 } ! rtm.send(JSON.stringify(replyMessage))
 })
 }) https://github.com/colestrode/fun-and-profit/tree/master/examples/01
  2. const api = require('./api')
 
 api.rtm.start((err, bot) => {
 if

    (err) {
 return console.log(err)
 }
 
 bot.on(/hello/, (bot, message) => {
 bot.reply(message, 'GO CUBS')
 })
 
 bot.on(/howdy/, (bot, message) => {
 bot.reply(message, 'GO TRIBE')
 })
 }) https://github.com/colestrode/fun-and-profit/tree/master/examples/02
  3. module.exports = {
 init: function (controller) {
 controller.hears('hello', ['ambient'], (bot,

    message) => {
 bot.reply(message, 'GO CUBS')
 })
 
 controller.hears('howdy', ['ambient'], (bot, message) => {
 bot.reply(message, 'GO TRIBE')
 })
 }
 }
 ! https://github.com/colestrode/fun-and-profit/tree/master/examples/04
  4. module.exports = {
 init: init,
 help: {
 command: 'hello',
 text:

    'Type `hello` or `howdy` and see what happens!'
 }
 } https://github.com/colestrode/fun-and-profit/tree/master/examples/04
  5. require('skellington')({
 clientId: process.env.CLIENT_ID,
 clientSecret: process.env.CLIENT_SECRET,
 port: 9000,
 successRedirectUri: 'https://github.com/colestrode/ skellington',


    botkit: {
 json_file_store: './db/' // persists authentication across restarts
 },
 plugins: [require('./plugin')]
 }) https://github.com/colestrode/fun-and-profit/tree/master/examples/07
  6. module.exports => {
 init: init,
 help: {
 command: 'hello',
 text:

    'Type `hello` or `howdy` and see what happens! Or maybe give `/ secret` a try!'
 }
 }
 
 function init (controller) {
 controller.on('slash_command', (bot, message) => {
 if (message.command !== '/secret') {
 return true
 }
 
 bot.replyPrivate(message, /hello/.test(message.text) ? 'GO CUBS' : 'GO TRIBE')
 return false // stop event propagation
 })
 
 controller.hears('hello', ['ambient'], (bot, message) => {
 bot.reply(message, 'GO CUBS')
 })
 
 controller.hears('howdy', ['ambient'], (bot, message) => {
 bot.reply(message, 'GO TRIBE')
 })
 }
 ! https://github.com/colestrode/fun-and-profit/tree/master/examples/07
  7. const skellington = require('skellington') ! skellington({
 slackToken: process.env.SLACK_TOKEN_1,
 plugins: [require('./plugin')]


    })
 ! ! skellington({
 slackToken: process.env.SLACK_TOKEN_2,
 plugins: [require('./another-plugin')]
 })
 https://github.com/colestrode/skellington/blob/master/test/functional/index.js