Let's make our bot smarter by adding functionality.
IMPORTANT TIP:
To activate changes in your bot code, you have to stop the script process (CTRL-C) and START IT again. (like in step 3
To print data to the terminal console, add the the following line to the controller.hears
section of your code:
console.log("MESSAGE from: " + message.original_message.personEmail);
Result:
controller.hears('hello','direct_message,direct_mention', function(bot, message) {
bot.reply(message, 'Howdy!');
// Adding the console.log line here:
console.log("MESSAGE from: " + message.original_message.personEmail);
});
Try it! Stop & Start the Node process (step 3) and send a 1:1 message to your Bot.
You will see **MESSAGE from: [email protected]
in the terminal window where your Bot code is running.
Do you want your bot only to reply to either 1:1 conversations or @-mentions (in spaces)?
In your code, remove direct_message,
text (including the comma!):
In the controller.hears
section of your code:
- controller.hears('hello','direct_message,direct_mention', function(bot, message)
+ controller.hears('hello','direct_mention', function(bot, message)
Result:
// Below, 'direct_message,' is removed
controller.hears('hello','direct_mention', function(bot, message) {
bot.reply(message, 'Howdy!');
console.log("MESSAGE from: " + message.original_message.personEmail);
});
Try it! Stop & Start the Node process (step 3) and send a 1:1 message to your Bot.
You will see that your Bot will not respond.
If you want the code not to listen to “hello” only, but anything that starts with hello
:
Add a ^
character before the 'hello' string.
In the controller.hears
section of your code:
- controller.hears( 'hello','direct_message,direct_mention', function(bot, message)
+ controller.hears('^hello','direct_message,direct_mention', function(bot, message)
Result:
// Below, 'direct_message,' is removed
controller.hears('^hello','direct_message,direct_mention', function(bot, message) {
bot.reply(message, 'Howdy!');
console.log("MESSAGE from: " + message.original_message.personEmail);
});
Try it! Stop & Start the Node process (step 3) and send a message to your Bot.
You will see that your Bot not only responds to hello
, but also hello bot
.
If you want your bot only to work (respond) for people in a specific domain, add a configuration entry
- In the
var controller = Botkit.sparkbot({
section, add:limit_to_domain: [‘@yourdomain.com’],
Only have 1 Cisco Spark account?
- Use 'yourdomain.com' and see that the bot will not respond to your message because it's not coming from the 'yourdomain.com' domain.
- Or create a new Cisco Spark account with a different email domain (Gmail?) and configure this domain in the
limit_to_domain
setting. Now you can test messages from both domains and see that it only responds to one of them. Tip: To prevent logging in and out of your Cisco Spark client, use a browser to login with your second Cisco Spark account.
Result:
var controller = Botkit.sparkbot({
public_address: process.env.public_address,
ciscospark_access_token: process.env.access_token,
secret: process.env.secret,
// the line below makes the bot only listen to users in the 'yourdomain.com' domain
limit_to_domain: [‘@yourdomain.com’],
});
Try it! Stop & Start the Node process (step 3) and using a Cisco Spark user account with a domain different than the one you configured, send a message to your Bot.
Then send a 1:1 message to your bot from a Cisco Spark account that is configured in the 'limit_to_domain' setting.