Skip to content

Commit 3e921cc

Browse files
enrichCommand fix, added show code command
1 parent 5676566 commit 3e921cc

14 files changed

Lines changed: 355 additions & 57 deletions

bot.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ require("fs").readdirSync(normalizedPath).forEach(function (file) {
121121

122122
// Utility to add mentions if Bot is in a 'Group' space
123123
bot.enrichCommand = function (message, command) {
124-
if ("group" == message.roomType) {
124+
125+
// if the message is a raw message (from a post message callback such as bot.say())
126+
if (message.roomType && (message.roomType == "group")) {
125127
var botName = bot.botkit.identity.displayName;
126128
return "`@" + botName + " " + command + "`";
127129
}
128-
if (message.original_message) {
129-
if ("group" == message.original_message.roomType) {
130-
var botName = bot.botkit.identity.displayName;
131-
return "`@" + botName + " " + command + "`";
132-
}
130+
131+
// if the message is a Botkit message
132+
if (message.raw_message && (message.raw_message.data.roomType == "group")) {
133+
var botName = bot.botkit.identity.displayName;
134+
return "`@" + botName + " " + command + "`";
133135
}
134136

135137
return "`" + command + "`";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "Botkit template for Cisco Spark",
3-
"version": "0.5.4",
3+
"version": "0.5.5",
44
"main": "bot.js",
55
"scripts": {
66
"start": "node bot.js"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function (controller, bot) {
1212
"description": "It's an awesome bot for sure!",
1313

1414
// Where to get more information about the bot
15-
"url": "https://github.com/CiscoDevNet/botkit-ciscospark-samples",
15+
"url": "https://github.com/CiscoDevNet/botkit-template",
1616

1717
// Legal owner
1818
"legal-owner": "Cisco DevNet <https://developer.cisco.com>",
@@ -48,7 +48,7 @@ module.exports = function (controller, bot) {
4848
//
4949
// .botcommons skill
5050
//
51-
controller.hears([/^\about$/, /^\botcommons$/, /^\.commons$/, /^\.bot$/], 'direct_message,direct_mention', function (bot, message) {
51+
controller.hears([/^about$/, /^botcommons$/, /^\.commons$/, /^\.bot$/], 'direct_message,direct_mention', function (bot, message) {
5252

5353
// Return metadata
5454
var metadata = '{\n'

skills/color.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = function (controller) {
2+
3+
controller.hears([/^color$/], 'direct_message,direct_mention', function (bot, message) {
4+
5+
bot.startConversation(message, function (err, convo) {
6+
convo.say('This is a BotKit conversation sample.');
7+
8+
convo.ask('What is your favorite color?', function (response, convo) {
9+
convo.say("Cool, I like '" + response.text + "' too!");
10+
convo.next();
11+
});
12+
});
13+
14+
});
15+
};

skills/command-help.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

skills/event-join.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

skills/help.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Command: help
3+
//
4+
module.exports = function (controller) {
5+
6+
controller.hears([/^help$/], 'direct_message,direct_mention', function (bot, message) {
7+
var text = "Here are my skills:";
8+
text += "\n- " + bot.enrichCommand(message, "color") + ": ask to pick a random color";
9+
text += "\n- " + bot.enrichCommand(message, "restricted") + ": let a user pick a color among a set of options";
10+
text += "\n- " + bot.enrichCommand(message, "threads") + ": branch to another thread";
11+
text += "\n- " + bot.enrichCommand(message, "variables") + ": enriched user-context among threads";
12+
text += "\n\nI also understand:";
13+
text += "\n- " + bot.enrichCommand(message, "about") + ": shows metadata about myself";
14+
text += "\n- " + bot.enrichCommand(message, "help") + ": spreads the word about my skills";
15+
bot.reply(message, text);
16+
});
17+
}

skills/restricted.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = function (controller) {
2+
3+
controller.hears([/^restricted$/], "direct_message,direct_mention", function (bot, message) {
4+
5+
bot.startConversation(message, function (err, convo) {
6+
7+
convo.ask("What is your favorite color?", [
8+
{
9+
pattern: "^blue|green|pink|red|yellow$",
10+
callback: function (response, convo) {
11+
convo.say('Cool, I like ' + response.text + ' too!');
12+
convo.next();
13+
},
14+
},
15+
{
16+
default: true,
17+
callback: function (response, convo) {
18+
convo.say("Sorry, I don't know this color. Try another one...");
19+
convo.repeat();
20+
convo.next();
21+
}
22+
}
23+
]);
24+
});
25+
});
26+
};

skills/show.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Displays the code of the specified skill
3+
//
4+
module.exports = function (controller) {
5+
6+
controller.hears([/^show\s*(.*)$/, /^code\s*(.*)$/], 'direct_message,direct_mention', function (bot, message) {
7+
8+
// Fetch value argument
9+
var skill = message.match[1];
10+
if (skill) {
11+
showSkill(skill, bot, message);
12+
return;
13+
}
14+
15+
bot.startConversation(message, function (err, convo) {
16+
17+
convo.ask("Please choose a skill among 'color', 'restricted', 'show', 'storage', 'threads', 'variables', 'about', 'join', 'help'", [
18+
{
19+
pattern: "^color|restricted|show|storage|threads|variables|about|join|help$",
20+
callback: function (response, convo) {
21+
// ends current conversation
22+
convo.stop();
23+
24+
showSkill(response.text, bot, message);
25+
return;
26+
},
27+
},
28+
{
29+
default: true,
30+
callback: function (response, convo) {
31+
convo.say("Sorry, this skill is not correct. Try again...");
32+
convo.repeat();
33+
convo.next();
34+
}
35+
}
36+
]);
37+
});
38+
});
39+
};
40+
41+
function showSkill(skill, bot, message) {
42+
// Append .js extension
43+
var skill_source = skill + ".js";
44+
45+
// Read file contents
46+
var normalizedPath = require("path").join(__dirname, skill_source);
47+
require("fs").readFile(normalizedPath, 'utf8', function (err, data) {
48+
if (err) {
49+
bot.reply(message, "Could not find code for skill '" + skill + "'. Try again with another skill name...");
50+
return;
51+
}
52+
53+
// Post file contents back to Cisco Spark
54+
var code = "```javascript\n" + data + "\n```";
55+
bot.reply(message, code);
56+
});
57+
}

skills/storage.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// Stores a user choice in botkit 'users' storage, so that the value can be retreived later
3+
//
4+
module.exports = function (controller) {
5+
6+
controller.hears([/^storage$/], 'direct_message,direct_mention', function (bot, message) {
7+
8+
// Check if a User preference already exists
9+
var userId = message.original_message.personId;
10+
controller.storage.users.get(userId, function (err, data) {
11+
if (err) {
12+
bot.reply(message, 'could not access storage, err: ' + err.message, function (err, message) {
13+
bot.reply(message, 'sorry, I am not feeling well \uF613! try again later...');
14+
});
15+
return;
16+
}
17+
18+
// User preference found
19+
if (data) {
20+
// Show user preference
21+
showUserPreference(controller, bot, message, userId, data.value);
22+
return;
23+
}
24+
25+
// Ask for favorite color
26+
askForFavoriteColor(controller, bot, message, userId);
27+
});
28+
});
29+
}
30+
31+
function showUserPreference(controller, bot, message, userId, color) {
32+
bot.startConversation(message, function (err, convo) {
33+
34+
convo.sayFirst(`Hey, I know you <@personId:${userId}>!<br/> '${color}' is your favorite color.`);
35+
36+
convo.ask("Should I remove your preference?", [
37+
{
38+
pattern: "^yes|ya|da|si|oui$",
39+
callback: function (response, convo) {
40+
41+
// Remove user preferences
42+
controller.storage.users.remove(userId, function (err) {
43+
if (err) {
44+
convo.say(message, 'sorry, could not access storage, err: ' + err.message);
45+
convo.repeat();
46+
return;
47+
}
48+
49+
convo.say("Successfully reset your color preference.");
50+
convo.next();
51+
});
52+
53+
},
54+
},
55+
{
56+
default: true,
57+
callback: function (response, convo) {
58+
convo.say("Got it, leaving your color preference as is.");
59+
convo.next();
60+
}
61+
}
62+
]);
63+
});
64+
}
65+
66+
function askForFavoriteColor(controller, bot, message, userId) {
67+
bot.startConversation(message, function (err, convo) {
68+
69+
convo.ask("What is your favorite color?", [
70+
{
71+
pattern: "^blue|green|pink|red|yellow$",
72+
callback: function (response, convo) {
73+
74+
// Store color as user preference
75+
var pickedColor = convo.extractResponse('answer');
76+
var userPreference = { id: userId, value: pickedColor };
77+
controller.storage.users.save(userPreference, function (err) {
78+
if (err) {
79+
convo.say(message, 'sorry, could not access storage, err: ' + err.message);
80+
convo.next();
81+
return;
82+
}
83+
84+
convo.transitionTo("success", `_stored user preference_`);
85+
});
86+
87+
},
88+
},
89+
{
90+
default: true,
91+
callback: function (response, convo) {
92+
convo.say("Sorry, I don't know this color. Try another one...");
93+
convo.repeat();
94+
convo.next();
95+
}
96+
}
97+
], { key: "answer" });
98+
99+
// Success thread
100+
convo.addMessage(
101+
"Cool, I love '{{responses.answer}}' too",
102+
"success");
103+
});
104+
}

0 commit comments

Comments
 (0)