Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Created April 22, 2016 19:00
Show Gist options
  • Save devStepsize/c5a50f7171b03ab386c760948a82fe4a to your computer and use it in GitHub Desktop.
Save devStepsize/c5a50f7171b03ab386c760948a82fe4a to your computer and use it in GitHub Desktop.
An example Botkit Slack bot showing how to perform slightly different actions (from https://github.com/howdyai/botkit/blob/master/examples/demo_bot.js)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack bot built with Botkit.
This bot demonstrates many of the core features of Botkit:
* Connect to Slack using the real time API
* Receive messages based on "spoken" patterns
* Send a message with attachments
* Send a message via direct message (instead of in a public channel)
# RUN THE BOT:
Get a Bot token from Slack:
-> http://my.slack.com/services/new/bot
Run your bot from the command line:
token=<MY TOKEN> node demo_bot.js
# USE THE BOT:
Find your bot inside Slack to send it a direct message.
Say: "Hello"
The bot will reply "Hello!"
Say: "Attach"
The bot will send a message with a multi-field attachment.
Send: "dm"
The bot will reply with a direct message.
Make sure to invite your bot into other channels using /invite @<my bot>!
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var Botkit = require('../lib/Botkit.js');
if (!process.env.token) {
console.log('Error: Specify token in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
debug: false
});
controller.spawn({
token: process.env.token
}).startRTM(function(err) {
if (err) {
throw new Error(err);
}
});
controller.hears(['hello','hi'],['direct_message','direct_mention','mention'],function(bot,message) {
bot.reply(message,"Hello.");
});
controller.hears(['attach'],['direct_message','direct_mention'],function(bot,message) {
var attachments = [];
var attachment = {
title: 'This is an attachment',
color: '#FFCC99',
fields: [],
};
attachment.fields.push({
label: 'Field',
value: 'A longish value',
short: false,
});
attachment.fields.push({
label: 'Field',
value: 'Value',
short: true,
});
attachment.fields.push({
label: 'Field',
value: 'Value',
short: true,
});
attachments.push(attachment);
bot.reply(message,{
text: 'See below...',
attachments: attachments,
},function(err,resp) {
console.log(err,resp);
});
});
controller.hears(['dm me'],['direct_message','direct_mention'],function(bot,message) {
bot.startConversation(message,function(err,convo) {
convo.say('Heard ya');
});
bot.startPrivateConversation(message,function(err,dm) {
dm.say('Private reply!');
});
});
Status API Training Shop Blog About
© 2016 GitHub, Inc. Terms Privacy Security Contact Help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment