Parsing Inbound Cisco Spark Webhook Bot Mentions using Node-Flint

A while back I wrote a post on parsing inbound Cisco Spark webhook data using the hears method with the node-flint framework by Nick Marus. After much thought and general improvement in skill as a developer I have created a much simpler way that could be potentially used with other frameworks. This allows the parsing of slash commands and fallback to a NLP processing service.

Below is a conversations module that can be used with flint. Rather than have multiple flint.hears methods for the event I now have just one using the switch method. Switch allows me to test the contents of my request string variable using regex. A much better solution than multiple flint.hears methods that has limited control over when to stop processing. Once you hit a match using switch it stops processing and performs the function in the return statement.
/*
Module parses all incoming requests from Spark webhooks and provides the responses for the bot from the node-flint framework.
*/
var _ = require('lodash');
var botString = "YourBotName ";
module.exports = function(flint){
flint.hears(/(^| ).*( |.|$)/i, function(bot, trigger) {
var text = trigger.text;
var spaceId = trigger.roomId;
var request = text.replace(botString,'');
//lookup stored Spark space information using a find function
var spData; //stored space object includes space ID and any other details.
switch(true){
case (/(^| )\/hello( |.|$)/).test(request):
return hello(request, bot, trigger,spData);
case (/(^| )\/help( |.|$)/).test(request):
return help(request, bot, trigger,spData);
case (/(^| )\/release( |.|$)/).test(request):
return release(request, bot, trigger,spData);
case (/(^| )\/who( |.|$)/).test(request):
return who(request, bot, trigger,spData);
case (/(^| )\/settings( |.|$)/).test(request):
return settings(request, bot, trigger,spData);
case (/(^| )\/feedback( |.|$)/).test(request):
return feedback(request, bot, trigger,spData);
default:
return nlp(request, bot, trigger,spData);
}
});
}
function hello(request, bot, trigger,spData){
if(bot.isGroup){
flint.debug("New group hello message sent using API.ai");
bot.say('Hello %s! To get started just type @ help', trigger.personDisplayName);
}else{
bot.say('Hello %s! To get started just type help', trigger.personDisplayName);
flint.debug("1:1 hello welcome sent");
};
return;
};
//Prints out help array
function help(request, bot, trigger,spData){
return bot.say({markdown:"stuff"});
};
//prints out space settings to user
function settings(request, bot, trigger, spData){
return bot.say({markdown:'stuff'});
};
//internal feedback command for simple request to developers from users
function feedback(request, bot, trigger,spData){
request = request + " Username: "+trigger.personEmail;
var email = 'Your email';
bot.dm(email,request);
bot.say("Thank you for your feedback. For technical issues I will get to them as soon as possible");
return;
};
//allows broadcast to all Spaces.
function broadcast(request, bot, trigger,spData){
request = request.replace("/broadcast ",'');
if(trigger.personEmail.match(/youremail\.com/i)){
_.forEach(flint.bots, function(bot) { bot.say({markdown:request}); });
}else{
bot.say("Sorry but your are not authorised for this command. The authoritities have been notified.");
bot.dm('<Your email>','Unauthorised attempt by this person: '+trigger.personEmail);
};
return;
};
//print out release infomation
function release(request, bot, trigger,spData){
bot.say({markdown:'stuff'});
};
//post the email address of everyone in the room.
function who(request, bot, trigger,spData){
console.log("who triggered");
_.forEach(bot.memberships,function(person){
console.log(person.email);
bot.say(person.email);
});
}
//pass data to nlp engine
function nlp(request, bot, trigger, spData){
passrequest.nlpConversation(request,bot,trigger,spData,function(data){
bot.say({markdown: data});
});
}
return flint
};
view raw conversation.js hosted with ❤ by GitHub

VoIPNorm