Skip to content

Commit

Permalink
Updated LKG build and package.json version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenic committed Apr 8, 2016
1 parent fc445dd commit d841c43
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 109 deletions.
246 changes: 163 additions & 83 deletions Node/lib/bots/BotConnectorBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var __extends = (this && this.__extends) || function (d, b) {
var collection = require('../dialogs/DialogCollection');
var session = require('../Session');
var consts = require('../consts');
var utils = require('../utils');
var request = require('request');
var uuid = require('node-uuid');
var BotConnectorBot = (function (_super) {
__extends(BotConnectorBot, _super);
function BotConnectorBot(options) {
Expand All @@ -16,7 +16,6 @@ var BotConnectorBot = (function (_super) {
endpoint: process.env['endpoint'] || 'https://api.botframework.com',
appId: process.env['appId'] || '',
appSecret: process.env['appSecret'] || '',
subscriptionKey: process.env['subscriptionKey'] || '',
defaultDialogId: '/'
};
this.configure(options);
Expand Down Expand Up @@ -68,7 +67,7 @@ var BotConnectorBot = (function (_super) {
this.configure(options);
return function (req, res) {
if (req.body) {
_this.processMessage(req.body, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
_this.dispatchMessage(null, req.body, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
}
else {
var requestData = '';
Expand All @@ -78,7 +77,7 @@ var BotConnectorBot = (function (_super) {
req.on('end', function () {
try {
var msg = JSON.parse(requestData);
_this.processMessage(msg, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
_this.dispatchMessage(null, msg, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
}
catch (e) {
_this.emit('error', new Error('Invalid Bot Framework Message'));
Expand All @@ -100,14 +99,32 @@ var BotConnectorBot = (function (_super) {
if (!this.hasDialog(dialogId)) {
throw new Error('Invalid dialog passed to BotConnectorBot.beginDialog().');
}
this.processMessage(msg, dialogId, dialogArgs);
this.dispatchMessage(msg.to.id, msg, dialogId, dialogArgs);
};
BotConnectorBot.prototype.processMessage = function (message, dialogId, dialogArgs, res) {
BotConnectorBot.prototype.dispatchMessage = function (userId, message, dialogId, dialogArgs, res) {
var _this = this;
try {
if (!message || !message.type) {
this.emit('error', new Error('Invalid Bot Framework Message'));
return res.send(400);
return res ? res.send(400) : null;
}
if (!userId) {
if (message.from && message.from.id) {
userId = message.from.id;
}
else {
this.emit('error', new Error('Invalid Bot Framework Message'));
return res ? res.send(400) : null;
}
}
var sessionId;
if (message.botConversationData && message.botConversationData[consts.Data.SessionId]) {
sessionId = message.botConversationData[consts.Data.SessionId];
}
else {
sessionId = uuid.v1();
message.botConversationData = message.botConversationData || {};
message.botConversationData[consts.Data.SessionId] = sessionId;
}
this.emit(message.type, message);
if (message.type == 'Message') {
Expand All @@ -117,42 +134,51 @@ var BotConnectorBot = (function (_super) {
dialogId: dialogId,
dialogArgs: dialogArgs
});
ses.on('send', function (message) {
var reply = message || {};
reply.botUserData = utils.clone(ses.userData);
reply.botConversationData = utils.clone(ses.conversationData);
reply.botPerUserInConversationData = utils.clone(ses.perUserInConversationData);
reply.botPerUserInConversationData[consts.Data.SessionState] = ses.sessionState;
if (reply.text && !reply.language) {
reply.language = ses.message.language;
}
if (res) {
_this.emit('reply', reply);
res.send(200, reply);
res = null;
}
else if (ses.message.conversationId) {
reply.from = ses.message.to;
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
reply.replyToMessageId = ses.message.id;
reply.conversationId = ses.message.conversationId;
reply.channelConversationId = ses.message.channelConversationId;
reply.channelMessageId = ses.message.channelMessageId;
reply.participants = ses.message.participants;
reply.totalParticipants = ses.message.totalParticipants;
_this.emit('reply', reply);
_this.post('/bot/v1.0/messages', reply, function (err) {
_this.emit('error', err);
});
}
else {
reply.from = ses.message.from;
reply.to = ses.message.to;
_this.emit('send', reply);
_this.post('/bot/v1.0/messages', reply, function (err) {
_this.emit('error', err);
});
ses.on('send', function (reply) {
reply = reply || {};
reply.botConversationData = message.botConversationData;
if (reply.text && !reply.language && message.language) {
reply.language = message.language;
}
var data = {
userData: ses.userData,
conversationData: ses.conversationData,
perUserConversationData: ses.perUserInConversationData
};
data.perUserConversationData[consts.Data.SessionState] = ses.sessionState;
_this.saveData(userId, sessionId, data, reply, function (err) {
if (res) {
_this.emit('reply', reply);
res.send(200, reply);
res = null;
}
else if (ses.message.conversationId) {
reply.from = ses.message.to;
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
reply.replyToMessageId = ses.message.id;
reply.conversationId = ses.message.conversationId;
reply.channelConversationId = ses.message.channelConversationId;
reply.channelMessageId = ses.message.channelMessageId;
reply.participants = ses.message.participants;
reply.totalParticipants = ses.message.totalParticipants;
_this.emit('reply', reply);
post(_this.options, '/bot/v1.0/messages', reply, function (err) {
if (err) {
_this.emit('error', err);
}
});
}
else {
reply.from = ses.message.from;
reply.to = ses.message.to;
_this.emit('send', reply);
post(_this.options, '/bot/v1.0/messages', reply, function (err) {
if (err) {
_this.emit('error', err);
}
});
}
});
});
ses.on('error', function (err) {
_this.emit('error', err, ses.message);
Expand All @@ -163,33 +189,22 @@ var BotConnectorBot = (function (_super) {
ses.on('quit', function () {
_this.emit('quit', ses.message);
});
var sessionState;
if (message.botUserData) {
ses.userData = message.botUserData;
delete message.botUserData;
}
else {
ses.userData = {};
}
if (message.botConversationData) {
ses.conversationData = message.botConversationData;
delete message.botConversationData;
}
else {
ses.conversationData = {};
}
if (message.botPerUserInConversationData) {
if (message.botPerUserInConversationData.hasOwnProperty(consts.Data.SessionState)) {
sessionState = message.botPerUserInConversationData[consts.Data.SessionState];
delete message.botPerUserInConversationData[consts.Data.SessionState];
this.getData(userId, sessionId, message, function (err, data) {
if (!err) {
var sessionState;
ses.userData = data.userData || {};
ses.conversationData = data.conversationData || {};
ses.perUserInConversationData = data.perUserConversationData || {};
if (ses.perUserInConversationData.hasOwnProperty(consts.Data.SessionState)) {
sessionState = ses.perUserInConversationData[consts.Data.SessionState];
delete ses.perUserInConversationData[consts.Data.SessionState];
}
ses.dispatch(sessionState, message);
}
ses.perUserInConversationData = message.botPerUserInConversationData;
delete message.botPerUserInConversationData;
}
else {
ses.perUserInConversationData = {};
}
ses.dispatch(sessionState, message);
else {
_this.emit('error', err, message);
}
});
}
else if (res) {
var msg;
Expand All @@ -212,22 +227,69 @@ var BotConnectorBot = (function (_super) {
res.send(500);
}
};
BotConnectorBot.prototype.post = function (path, body, callback) {
var settings = this.options;
var options = {
url: settings.endpoint + path,
body: body
};
if (settings.appId && settings.appSecret) {
options.auth = {
username: settings.appId,
password: settings.appSecret
};
options.headers = {
'Ocp-Apim-Subscription-Key': settings.appSecret
};
BotConnectorBot.prototype.getData = function (userId, sessionId, msg, callback) {
var botPath = '/' + this.options.appId;
var userPath = botPath + '/users/' + userId;
var convoPath = botPath + '/conversations/' + sessionId;
var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId;
var ops = 3;
var data = {};
function load(id, field, store, botData) {
data[field] = botData;
if (store) {
store.get(id, function (err, item) {
if (callback) {
if (!err) {
data[field] = item;
if (--ops == 0) {
callback(null, data);
}
}
else {
callback(err, null);
callback = null;
}
}
});
}
else if (callback && --ops == 0) {
callback(null, data);
}
}
load(userPath, 'userData', this.options.userStore, msg.botUserData);
load(convoPath, 'conversationData', this.options.conversationStore, msg.botConversationData);
load(perUserConvoPath, 'perUserConversationData', this.options.perUserInConversationStore, msg.botPerUserInConversationData);
};
BotConnectorBot.prototype.saveData = function (userId, sessionId, data, msg, callback) {
var botPath = '/' + this.options.appId;
var userPath = botPath + '/users/' + userId;
var convoPath = botPath + '/conversations/' + sessionId;
var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId;
var ops = 3;
function save(id, field, store, botData) {
if (store) {
store.save(id, botData, function (err) {
if (callback) {
if (!err && --ops == 0) {
callback(null);
}
else {
callback(err);
callback = null;
}
}
});
}
else {
msg[field] = botData;
if (callback && --ops == 0) {
callback(null);
}
}
}
request.post(options, callback);
save(userPath, 'botUserData', this.options.userStore, data.userData);
save(convoPath, 'botConversationData', this.options.conversationStore, data.conversationData);
save(perUserConvoPath, 'botPerUserInConversationData', this.options.perUserInConversationStore, data.perUserConversationData);
};
return BotConnectorBot;
})(collection.DialogCollection);
Expand All @@ -240,3 +302,21 @@ var BotConnectorSession = (function (_super) {
return BotConnectorSession;
})(session.Session);
exports.BotConnectorSession = BotConnectorSession;
function post(settings, path, body, callback) {
var options = {
method: 'POST',
url: settings.endpoint + path,
body: body,
json: true
};
if (settings.appId && settings.appSecret) {
options.auth = {
username: settings.appId,
password: settings.appSecret
};
options.headers = {
'Ocp-Apim-Subscription-Key': settings.appSecret
};
}
request(options, callback);
}
16 changes: 10 additions & 6 deletions Node/lib/bots/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var SlackBot = (function (_super) {
return this;
};
SlackBot.prototype.beginDialog = function (address, dialogId, dialogArgs) {
if (!address.user && !address.channel) {
if (!address.channel) {
throw new Error('Invalid address passed to SlackBot.beginDialog().');
}
if (!this.hasDialog(dialogId)) {
Expand All @@ -98,13 +98,15 @@ var SlackBot = (function (_super) {
SlackBot.prototype.dispatchMessage = function (bot, msg, dialogId, dialogArgs, smartState) {
var _this = this;
var onError = function (err) {
_this.emit('error', err, msg);
if (err) {
_this.emit('error', err, msg);
}
};
var ses = new SlackSession({
localizer: this.options.localizer,
dialogs: this,
dialogId: this.options.defaultDialogId,
dialogArgs: this.options.defaultDialogArgs
dialogId: dialogId || this.options.defaultDialogId,
dialogArgs: dialogArgs || this.options.defaultDialogArgs
});
ses.on('send', function (reply) {
var teamData = ses.teamData && ses.teamData.id ? utils.clone(ses.teamData) : null;
Expand All @@ -117,7 +119,7 @@ var SlackBot = (function (_super) {
if (reply && (reply.text || reply.channelData)) {
var slackReply = _this.toSlackMessage(reply);
if (bot) {
if (slackReply.user && slackReply.user != msg.user) {
if (slackReply.channel && slackReply.channel != msg.channel) {
_this.emit('send', slackReply);
bot.say(slackReply, onError);
}
Expand All @@ -127,7 +129,9 @@ var SlackBot = (function (_super) {
}
}
else {
slackReply.user = ses.message.to.address;
if (!slackReply.channel) {
slackReply.channel = msg.channel;
}
_this.emit('send', slackReply);
_this.bot.say(slackReply, onError);
}
Expand Down
9 changes: 7 additions & 2 deletions Node/lib/bots/TextBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ var TextBot = (function (_super) {
_this.emit('quit', message);
});
this.getData(userId, function (err, userData, sessionState) {
ses.userData = userData || {};
ses.dispatch(newSessionState ? null : sessionState, message);
if (!err) {
ses.userData = userData || {};
ses.dispatch(newSessionState ? null : sessionState, message);
}
else {
_this.emit('error', err, message);
}
});
};
TextBot.prototype.getData = function (userId, callback) {
Expand Down
1 change: 1 addition & 0 deletions Node/lib/consts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exports.Data = {
SessionState: 'BotBuilder.Data.SessionState',
SessionId: 'BotBuilder.Data.SessionId',
Handler: 'BotBuilder.Data.Handler',
Group: 'BotBuilder.Data.Group',
Intent: 'BotBuilder.Data.Intent',
Expand Down
Loading

0 comments on commit d841c43

Please sign in to comment.