Skip to content

Commit

Permalink
Added a 'subscriptions' skill and changed the default storage system …
Browse files Browse the repository at this point in the history
…to handle a subscriptions db
  • Loading branch information
Rafael Campos committed Jul 14, 2017
1 parent fbe5773 commit 86ffcc2
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ SECRET=Not that secret !
# https://developer.ciscospark.com/endpoint-webhooks-webhookId-delete.html
#WEBHOOK_NAME=built with BotKit (development)

# AppDynamics account
# The part that comes before "saas.appdynamics.com" in the URL of the SaaS version
# APPD_ACCOUNT=myappdaccount1234567890

# Local port where your bot will be started
# defaults to 3000
#PORT=3000
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

Inspired by [BotKit samples for Cisco Spark](https://github.com/CiscoDevNet/botkit-ciscospark-samples) by Stève Sfartz <mailto:[email protected]>

You can try the bot live by inviting the [email protected] bot to a Cisco Spark space.

3 changes: 2 additions & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var env = require('node-env-file');
env(__dirname + '/.env');
var AppD = require('./lib/appd.js');
var storage = require('./lib/storage.js')


//
Expand Down Expand Up @@ -42,7 +43,7 @@ var controller = Botkit.sparkbot({
ciscospark_access_token: process.env.SPARK_TOKEN,
secret: process.env.SECRET, // this is a RECOMMENDED security setting that checks of incoming payloads originate from Cisco Spark
webhook_name: process.env.WEBHOOK_NAME || ('built with BotKit (' + env + ')'),
json_file_store: './jfs'
storage: storage({ path: './jfs' })
});

var bot = controller.spawn({});
Expand Down
1 change: 1 addition & 0 deletions jfs/subscriptions/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"error","users":[]}
1 change: 1 addition & 0 deletions jfs/subscriptions/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"info","users":["[email protected]"]}
1 change: 1 addition & 0 deletions jfs/subscriptions/warn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"warn","users":["[email protected]"]}
1 change: 1 addition & 0 deletions lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function AppD(configuration) {
var app_url_tpl = "https://{}.saas.appdynamics.com/controller/?accountName={}#/location=APP_DASHBOARD&application={}"
var msg = "* A [{}]({}) has occurred in application [{}]({}) ({}).<br/>Time of event: *{}*.<br/>Affected Tier: **{}**.<br/>Affected Node: **{}**."

//Send a DM to the people who subscribed to the notification
bot.startPrivateConversationWithPersonId(personId, function(err, convo) {
convo.say("### AppDynamics Notification");
for (var i = 0; i < req.body.length; i++) {
Expand Down
93 changes: 93 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Custom storage module for AppD bots. Overrides the default simple_storage
module to add a subscriptions_db database.
*/

var Store = require('jfs');

module.exports = function(config) {

if (!config) {
config = {
path: './',
};
}

var teams_db = new Store(config.path + '/teams', { saveId: 'id' });
var users_db = new Store(config.path + '/users', { saveId: 'id' });
var channels_db = new Store(config.path + '/channels', { saveId: 'id' });
var subscriptions_db = new Store(config.path + '/subscriptions', { saveId: 'id' });

var objectsToList = function(cb) {
return function(err, data) {
if (err) {
cb(err, data);
} else {
cb(err, Object.keys(data).map(function(key) {
return data[key];
}));
}
};
};

var storage = {
teams: {
get: function(team_id, cb) {
teams_db.get(team_id, cb);
},
save: function(team_data, cb) {
teams_db.save(team_data.id, team_data, cb);
},
delete: function(team_id, cb) {
teams_db.delete(team_id.id, cb);
},
all: function(cb) {
teams_db.all(objectsToList(cb));
}
},
users: {
get: function(user_id, cb) {
users_db.get(user_id, cb);
},
save: function(user, cb) {
users_db.save(user.id, user, cb);
},
delete: function(user_id, cb) {
users_db.delete(user_id.id, cb);
},
all: function(cb) {
users_db.all(objectsToList(cb));
}
},
channels: {
get: function(channel_id, cb) {
channels_db.get(channel_id, cb);
},
save: function(channel, cb) {
channels_db.save(channel.id, channel, cb);
},
delete: function(channel_id, cb) {
channels_db.delete(channel_id.id, cb);
},
all: function(cb) {
channels_db.all(objectsToList(cb));
}
},
subscriptions: {
get: function(subscription_id, cb) {
subscriptions_db.get(subscription_id, cb);
},
save: function(subscription, cb) {
subscriptions_db.save(subscription.id, subscription, cb);
},
delete: function(subscription_id, cb) {
subscriptions_db.delete(subscription_id.id, cb);
},
all: function(cb) {
subscriptions_db.all(objectsToList(cb));
}
}
};

return storage;
};
106 changes: 106 additions & 0 deletions skills/subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module.exports = function(controller) {

controller.hears(['subscribe|sub'], 'direct_message,direct_mention', function(bot, message) {

bot.createConversation(message, function(err, convo) {

function subscribeToNotification(type, user) {
controller.storage.subscriptions.get(type, function(err, subscription_data) {
var users = [];
if (subscription_data) {
users = subscription_data.users;
if (!users.includes(user)) {
users.push(user);
convo.say("You're now subscribed to '{}' notifications.".format(type));
} else {
convo.say("You're already subscribed to '{}' notifications.".format(type));
}
} else {
users.push(user)
convo.say("You're now subscribed to '{}' notifications.".format(type));
}
controller.storage.subscriptions.save({ id: type, users: users }, function(err) {});
});
}
convo.ask("What type of notifications do you want to subscribe to? (INFO/WARN/ERROR)", [{
pattern: "INFO|info|Info",
callback: function(response, convo) {
subscribeToNotification('info', message.user);
convo.next();
},
}, {
pattern: "WARN|warn|Warn",
callback: function(response, convo) {
subscribeToNotification('warn', message.user);
convo.next();
},
}, {
pattern: "ERROR|error|Error",
callback: function(response, convo) {
subscribeToNotification('error', message.user);
convo.next();
},
}, {
default: true,
callback: function(response, convo) {
convo.say("Sorry, I did not understand.");
convo.repeat();
convo.next();
}
}]);

convo.activate();
});
});

controller.hears(['unsubscribe|unsub'], 'direct_message,direct_mention', function(bot, message) {

bot.createConversation(message, function(err, convo) {

function unsubscribeToNotification(type, user) {
controller.storage.subscriptions.get(type, function(err, subscription_data) {
if (subscription_data) {
users = subscription_data.users;
if (users.includes(user)) {
users.splice(users.indexOf(user), 1);
convo.say("You're now unsubscribed from '{}' notifications.".format(type));
} else {
convo.say("You're not currently subscribed to '{}' notifications.".format(type));
}
} else {
convo.say("You're not currently subscribed to '{}' notifications.".format(type));
}
controller.storage.subscriptions.save({ id: type, users: users }, function(err) {});
});
}
convo.ask("What type of notifications do you want to unsubscribe from? (INFO/WARN/ERROR)", [{
pattern: "INFO|info|Info",
callback: function(response, convo) {
unsubscribeToNotification('info', message.user);
convo.next();
},
}, {
pattern: "WARN|warn|Warn",
callback: function(response, convo) {
unsubscribeToNotification('warn', message.user);
convo.next();
},
}, {
pattern: "ERROR|error|Error",
callback: function(response, convo) {
unsubscribeToNotification('error', message.user);
convo.next();
},
}, {
default: true,
callback: function(response, convo) {
convo.say("Sorry, I did not understand.");
convo.repeat();
convo.next();
}
}]);

convo.activate();
});
});
};

0 comments on commit 86ffcc2

Please sign in to comment.