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

Slack Bots for Fun and Profit (Charm City JS)

Slack Bots for Fun and Profit (Charm City JS)

Some recommendations about building Slack bots, given at the Charm City JS meet up in January 2017.

Avatar for Cole Furfaro-Strode

Cole Furfaro-Strode

January 05, 2017

More Decks by Cole Furfaro-Strode

Other Decks in Technology

Transcript

  1. @colestrode 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. @colestrode 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
  3. @colestrode 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
  4. @colestrode require('skellington')({
 clientId: process.env.CLIENT_ID,
 clientSecret: process.env.CLIENT_SECRET,
 port: 9000,
 botkit: {


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


    text: 'Type `hello` or `howdy` and see what happens!'
 }
 }
 
 function init (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/07
  6. @colestrode 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/Skellington-Closet/skellington/blob/master/test/functional/index.js