Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for interactive messages #1

Merged
merged 26 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7d93e00
add basic support for interactive messages
May 24, 2016
f62821f
logging close error code/message and reconnecting for now
selfcontained Jun 6, 2016
c5e9d98
limiting reconnect during close event to abnormal closes
selfcontained Jun 11, 2016
c380c28
changing comment to jsdoc
selfcontained Jun 11, 2016
fa3e0ab
Minor typo fix (extra comma)
Jun 14, 2016
714788d
Merge pull request #274 from selfcontained/close-error-reconnect
Jun 16, 2016
cb7781d
Update changelog and package for 0.2.1
Jun 16, 2016
5f44bf4
Refer to Messenger Platform in Facebook botkit log
mcrumm Jun 18, 2016
9535ab9
get rid of deprecated log and debug functions
Jun 18, 2016
1f777e6
fix typo
Haroenv Jun 20, 2016
342f99f
Merge branch 'master' of github.com:howdyai/botkit into slack_interac…
Jun 20, 2016
ecdbe64
updates for interactive messages
Jun 20, 2016
51d29a9
updates
Jun 20, 2016
5a0d512
Add changelog
Jun 20, 2016
ce06f71
fix a few scope issues in example
Jun 20, 2016
4739cd9
restore example
Jun 20, 2016
b07f531
add readme stuff for interactive messages
Jun 21, 2016
0c46b83
readme formatting
Jun 21, 2016
c20975f
add link to slack docs
Jun 21, 2016
54b7823
Merge pull request #283 from mcrumm/fix_facebook_webhook_log
anonrig Jun 21, 2016
9a0b7b8
Merge pull request #285 from Haroenv/patch-1
anonrig Jun 21, 2016
9a9ca5f
Merge pull request #277 from aysark/patch-1
anonrig Jun 21, 2016
e7a9b1a
Merge branch 'slack_interactives'
Jun 21, 2016
7a2c61c
update version in package file
Jun 21, 2016
284483d
Adjust the example command-line call to use the new filename.
scheijan Jun 21, 2016
002da9c
Merge pull request #289 from scheijan/master
Jun 21, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## 0.2.2

Add support for Slack Interactive Messages.

Add example of Slack button application that provides a bot that uses interactive messages.

New functionality in Slack bot: Botkit will track spawned Slack bots and route incoming webhooks to pre-existing RTM bots. This enables RTM bots to reply to interactive messages and slash commands.

## 0.2.1

Improves Slack RTM reconnects thanks to @selfcontained [PR #274](https://github.com/howdyai/botkit/pull/274)

## 0.2

Adds support for Twilio IP Messenging bots
Expand Down
332 changes: 332 additions & 0 deletions examples/slackbutton_bot_interactivemsg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/


This is a sample Slack Button application that adds a bot to one or many slack teams.

# RUN THE APP:
Create a Slack app. Make sure to configure the bot user!
-> https://api.slack.com/applications/new
-> Add the Redirect URI: http://localhost:3000/oauth
Run your bot from the command line:
clientId=<my client id> clientSecret=<my client secret> port=3000 node slackbutton_bot_interactivemsg.js
# USE THE APP
Add the app to your Slack by visiting the login page:
-> http://localhost:3000/login
After you've added the app, try talking to your bot!
# EXTEND THE APP:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/* Uses the slack button feature to offer a real time bot to multiple teams */
var Botkit = require('../lib/Botkit.js');

if (!process.env.clientId || !process.env.clientSecret || !process.env.port) {
console.log('Error: Specify clientId clientSecret and port in environment');
process.exit(1);
}


var controller = Botkit.slackbot({
// interactive_replies: true, // tells botkit to send button clicks into conversations
json_file_store: './db_slackbutton_bot/',
}).configureSlackApp(
{
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
scopes: ['bot'],
}
);

controller.setupWebserver(process.env.port,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver);

controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
});


// just a simple way to make sure we don't
// connect to the RTM twice for the same team
var _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}


controller.on('interactive_message_callback', function(bot, message) {

var ids = message.callback_id.split(/\-/);
var user_id = ids[0];
var item_id = ids[1];

controller.storage.users.get(user_id, function(err, user) {

if (!user) {
user = {
id: user_id,
list: []
}
}

for (var x = 0; x < user.list.length; x++) {
if (user.list[x].id == item_id) {
if (message.actions[0].value=='flag') {
user.list[x].flagged = !user.list[x].flagged;
}
if (message.actions[0].value=='delete') {
user.list.splice(x,1);
}
}
}


var reply = {
text: 'Here is <@' + user_id + '>s list:',
attachments: [],
}

for (var x = 0; x < user.list.length; x++) {
reply.attachments.push({
title: user.list[x].text + (user.list[x].flagged? ' *FLAGGED*' : ''),
callback_id: user_id + '-' + user.list[x].id,
attachment_type: 'default',
actions: [
{
"name":"flag",
"text": ":waving_black_flag: Flag",
"value": "flag",
"type": "button",
},
{
"text": "Delete",
"name": "delete",
"value": "delete",
"style": "danger",
"type": "button",
"confirm": {
"title": "Are you sure?",
"text": "This will do something!",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
})
}

bot.replyInteractive(message, reply);
controller.storage.users.save(user);


});

});


controller.on('create_bot',function(bot,config) {

if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err) {

if (!err) {
trackBot(bot);
}

bot.startPrivateConversation({user: config.createdBy},function(err,convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});

});
}

});


// Handle events related to the websocket connection to Slack
controller.on('rtm_open',function(bot) {
console.log('** The RTM api just connected!');
});

controller.on('rtm_close',function(bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});


controller.hears(['add (.*)'],'direct_mention,direct_message',function(bot,message) {

controller.storage.users.get(message.user, function(err, user) {

if (!user) {
user = {
id: message.user,
list: []
}
}

user.list.push({
id: message.ts,
text: message.match[1],
});

bot.reply(message,'Added to list. Say `list` to view or manage list.');

controller.storage.users.save(user);

});
});


controller.hears(['list','tasks'],'direct_mention,direct_message',function(bot,message) {

controller.storage.users.get(message.user, function(err, user) {

if (!user) {
user = {
id: message.user,
list: []
}
}

if (!user.list || !user.list.length) {
user.list = [
{
'id': 1,
'text': 'Test Item 1'
},
{
'id': 2,
'text': 'Test Item 2'
},
{
'id': 3,
'text': 'Test Item 3'
}
]
}

var reply = {
text: 'Here is your list. Say `add <item>` to add items.',
attachments: [],
}

for (var x = 0; x < user.list.length; x++) {
reply.attachments.push({
title: user.list[x].text + (user.list[x].flagged? ' *FLAGGED*' : ''),
callback_id: message.user + '-' + user.list[x].id,
attachment_type: 'default',
actions: [
{
"name":"flag",
"text": ":waving_black_flag: Flag",
"value": "flag",
"type": "button",
},
{
"text": "Delete",
"name": "delete",
"value": "delete",
"style": "danger",
"type": "button",
"confirm": {
"title": "Are you sure?",
"text": "This will do something!",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
})
}

bot.reply(message, reply);

controller.storage.users.save(user);

});

});

controller.hears('interactive', 'direct_message', function(bot, message) {

bot.reply(message, {
attachments:[
{
title: 'Do you want to interact with my buttons?',
callback_id: '123',
attachment_type: 'default',
actions: [
{
"name":"yes",
"text": "Yes",
"value": "yes",
"type": "button",
},
{
"name":"no",
"text": "No",
"value": "no",
"type": "button",
}
]
}
]
});
});


controller.hears('^stop','direct_message',function(bot,message) {
bot.reply(message,'Goodbye');
bot.rtm.close();
});

controller.on(['direct_message','mention','direct_mention'],function(bot,message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'robot_face',
},function(err) {
if (err) { console.log(err) }
bot.reply(message,'I heard you loud and clear boss.');
});
});

controller.storage.teams.all(function(err,teams) {

if (err) {
throw new Error(err);
}

// connect all teams with bots up to slack!
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function(err, bot) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
trackBot(bot);
}
});
}
}

});
Empty file modified examples/slackbutton_incomingwebhooks.js
100755 → 100644
Empty file.
Loading