|
| 1 | +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + ______ ______ ______ __ __ __ ______ |
| 3 | + /\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ |
| 4 | + \ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/ |
| 5 | + \ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\ |
| 6 | + \/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/ |
| 7 | +
|
| 8 | +
|
| 9 | +This is a sample Slack bot built with Botkit. |
| 10 | +
|
| 11 | +This bot demonstrates many of the core features of Botkit: |
| 12 | +
|
| 13 | +* Connect to Slack using the real time API |
| 14 | +* Receive messages based on "spoken" patterns |
| 15 | +* Send a message with attachments |
| 16 | +* Send a message via direct message (instead of in a public channel) |
| 17 | +
|
| 18 | +# RUN THE BOT: |
| 19 | +
|
| 20 | + Get a Bot token from Slack: |
| 21 | +
|
| 22 | + -> http://my.slack.com/services/new/bot |
| 23 | +
|
| 24 | + Run your bot from the command line: |
| 25 | +
|
| 26 | + token=<MY TOKEN> node demo_bot.js |
| 27 | +
|
| 28 | +# USE THE BOT: |
| 29 | +
|
| 30 | + Find your bot inside Slack to send it a direct message. |
| 31 | +
|
| 32 | + Say: "Hello" |
| 33 | +
|
| 34 | + The bot will reply "Hello!" |
| 35 | +
|
| 36 | + Say: "Attach" |
| 37 | +
|
| 38 | + The bot will send a message with a multi-field attachment. |
| 39 | +
|
| 40 | + Send: "dm" |
| 41 | +
|
| 42 | + The bot will reply with a direct message. |
| 43 | +
|
| 44 | + Make sure to invite your bot into other channels using /invite @<my bot>! |
| 45 | +
|
| 46 | +# EXTEND THE BOT: |
| 47 | +
|
| 48 | + Botkit has many features for building cool and useful bots! |
| 49 | +
|
| 50 | + Read all about it here: |
| 51 | +
|
| 52 | + -> http://howdy.ai/botkit |
| 53 | +
|
| 54 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 55 | + |
| 56 | +var Botkit = require('../lib/Botkit.js'); |
| 57 | + |
| 58 | + |
| 59 | +if (!process.env.token) { |
| 60 | + console.log('Error: Specify token in environment'); |
| 61 | + process.exit(1); |
| 62 | +} |
| 63 | + |
| 64 | +var controller = Botkit.slackbot({ |
| 65 | + debug: false |
| 66 | +}); |
| 67 | + |
| 68 | +controller.spawn({ |
| 69 | + token: process.env.token |
| 70 | +}).startRTM(function(err) { |
| 71 | + if (err) { |
| 72 | + throw new Error(err); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +controller.hears(['hello','hi'],['direct_message','direct_mention','mention'],function(bot,message) { |
| 78 | + bot.reply(message,"Hello."); |
| 79 | +}); |
| 80 | + |
| 81 | +controller.hears(['attach'],['direct_message','direct_mention'],function(bot,message) { |
| 82 | + |
| 83 | + var attachments = []; |
| 84 | + var attachment = { |
| 85 | + title: 'This is an attachment', |
| 86 | + color: '#FFCC99', |
| 87 | + fields: [], |
| 88 | + }; |
| 89 | + |
| 90 | + attachment.fields.push({ |
| 91 | + label: 'Field', |
| 92 | + value: 'A longish value', |
| 93 | + short: false, |
| 94 | + }); |
| 95 | + |
| 96 | + attachment.fields.push({ |
| 97 | + label: 'Field', |
| 98 | + value: 'Value', |
| 99 | + short: true, |
| 100 | + }); |
| 101 | + |
| 102 | + attachment.fields.push({ |
| 103 | + label: 'Field', |
| 104 | + value: 'Value', |
| 105 | + short: true, |
| 106 | + }); |
| 107 | + |
| 108 | + attachments.push(attachment); |
| 109 | + |
| 110 | + bot.reply(message,{ |
| 111 | + text: 'See below...', |
| 112 | + attachments: attachments, |
| 113 | + },function(err,resp) { |
| 114 | + console.log(err,resp); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +controller.hears(['dm me'],['direct_message','direct_mention'],function(bot,message) { |
| 119 | + bot.startConversation(message,function(err,convo) { |
| 120 | + convo.say('Heard ya'); |
| 121 | + }); |
| 122 | + |
| 123 | + bot.startPrivateConversation(message,function(err,dm) { |
| 124 | + dm.say('Private reply!'); |
| 125 | + }); |
| 126 | + |
| 127 | +}); |
0 commit comments