-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a 'subscriptions' skill and changed the default storage system …
…to handle a subscriptions db
- Loading branch information
Rafael Campos
committed
Jul 14, 2017
1 parent
fbe5773
commit 86ffcc2
Showing
9 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id":"error","users":[]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id":"info","users":["[email protected]"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id":"warn","users":["[email protected]"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}; |