Skip to content

Instantly share code, notes, and snippets.

@voipnorm
voipnorm / config.example.js
Created September 26, 2016 22:04
Spark Bot Configuration Example fFle
(function() {
"use strict";
//rename file to config.js to implement project
exports.botToken = process.env.BOTTOKEN || "Your Spark access token";
exports.watsonPassword = process.env.WASTSONPASSWORD || "Your IBM watson API password";
exports.watsonUserName = process.env.WATSONUSERNAME || "Your IBM watson user code";
exports.watsonWorkSpaceID = process.env.WATSONWORKSPACEID || "Work space ID created from conversation workspace";
exports.cloud9URL = process.env.CLOUD9URL || "https://yourProject-username.c9users.io";
})();
flint.hears(/\/broadcast( |.|$)/i, function(bot, trigger){
var request = trigger.text.replace("/broadcast ",'');
if(trigger.personEmail.match(/youremail@example\.com|yourotheremail@example\.com/i)){
_.forEach(flint.bots, function(bot) { bot.say(request); });
}else{
bot.say("Sorry but your are not authorised for this command. The authoritities have been notified.");
bot.dm('youremail@example.com','Unauthorised attempt by this person: '+trigger.personEmail);
}
});
flint.hears('/roomid', function(bot, trigger) {
bot.say('This is your room ID', trigger.roomId);
});
flint.hears('/hello', function(bot, trigger) {
bot.say('Hello %s!', trigger.personDisplayName);
});
//Welcome message when a new room or 1:1 is spawned with the bot
flint.on('spawn', function(bot) {
flint.debug('new bot spawned in room: %s', bot.room.id);
//presents different messages based on room or 1:1
if(bot.isGroup){
bot.say("Hi! To get started just type @examplebot hello.");
return
}else{
bot.say("Hi! To get started just type hello.");
return
//set bot to listen to incoming webhooks based on @mentions in group rooms
//or any text in a one on one room
flint.hears(/(^| )ExampleBot|.*( |.|$)/i, function(bot, trigger) {
var text = trigger.text;
//@ mention removed before further processing for group conversations. @symbol not passed in message
var request = text.replace("ExampleBot ",'');
//match method stops slash commands being passed to NLP
if(request.match(/(^| )\/hello|\/roomid|\/broadcast|\/find( |.|$)/i)){
flint.hears('/roomid', function(bot, trigger){
var roomId = trigger.roomId;
bot.say({markdown:'**Here are the current room settings: \n **'+ roomId,file:'http://filelocation.com/file.jpg'});
})
});
@voipnorm
voipnorm / xmppToSparkBot.js
Last active October 3, 2024 21:08
xmpp client to Spark Bot
//CISCOSPARK_ACCESS_TOKEN=<your token> DEBUG=* node server.js
var xmpp = require('simple-xmpp'),
http = require('http'),
ciscospark = require('ciscospark'),
assert = require('assert'),
fs = require('fs'),
path = require('path'),
logger = require('log4js');
logger.configure({
@voipnorm
voipnorm / watsoncontext.js
Last active October 24, 2016 16:13
Context using in Spark conversation
var conversation = new ConversationV1({
username: myconfig.watsonUserName,
password: myconfig.watsonPassword,
version_date: '2016-07-01'
});
exports.watsonConversation = function(userContext, inputMessage, callback){
//User context from either a new user or previsouly saved conversation
var params = {
context:userContext,
@voipnorm
voipnorm / WatsonContextDB.js
Last active October 24, 2016 16:59
Watson Database Config
//mongojs implmentation
var mongodb = require('mongodb');
var mongojs = require('mongojs');
//Logging to file for troubleshooting
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'});
var log_stdout = process.stdout;
//debug logs to file for varying responses can be removed if not required
var mylog = function(d) {