Skip to content

Commit d841c43

Browse files
committed
Updated LKG build and package.json version.
1 parent fc445dd commit d841c43

File tree

6 files changed

+257
-109
lines changed

6 files changed

+257
-109
lines changed

Node/lib/bots/BotConnectorBot.js

Lines changed: 163 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ var __extends = (this && this.__extends) || function (d, b) {
66
var collection = require('../dialogs/DialogCollection');
77
var session = require('../Session');
88
var consts = require('../consts');
9-
var utils = require('../utils');
109
var request = require('request');
10+
var uuid = require('node-uuid');
1111
var BotConnectorBot = (function (_super) {
1212
__extends(BotConnectorBot, _super);
1313
function BotConnectorBot(options) {
@@ -16,7 +16,6 @@ var BotConnectorBot = (function (_super) {
1616
endpoint: process.env['endpoint'] || 'https://api.botframework.com',
1717
appId: process.env['appId'] || '',
1818
appSecret: process.env['appSecret'] || '',
19-
subscriptionKey: process.env['subscriptionKey'] || '',
2019
defaultDialogId: '/'
2120
};
2221
this.configure(options);
@@ -68,7 +67,7 @@ var BotConnectorBot = (function (_super) {
6867
this.configure(options);
6968
return function (req, res) {
7069
if (req.body) {
71-
_this.processMessage(req.body, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
70+
_this.dispatchMessage(null, req.body, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
7271
}
7372
else {
7473
var requestData = '';
@@ -78,7 +77,7 @@ var BotConnectorBot = (function (_super) {
7877
req.on('end', function () {
7978
try {
8079
var msg = JSON.parse(requestData);
81-
_this.processMessage(msg, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
80+
_this.dispatchMessage(null, msg, _this.options.defaultDialogId, _this.options.defaultDialogArgs, res);
8281
}
8382
catch (e) {
8483
_this.emit('error', new Error('Invalid Bot Framework Message'));
@@ -100,14 +99,32 @@ var BotConnectorBot = (function (_super) {
10099
if (!this.hasDialog(dialogId)) {
101100
throw new Error('Invalid dialog passed to BotConnectorBot.beginDialog().');
102101
}
103-
this.processMessage(msg, dialogId, dialogArgs);
102+
this.dispatchMessage(msg.to.id, msg, dialogId, dialogArgs);
104103
};
105-
BotConnectorBot.prototype.processMessage = function (message, dialogId, dialogArgs, res) {
104+
BotConnectorBot.prototype.dispatchMessage = function (userId, message, dialogId, dialogArgs, res) {
106105
var _this = this;
107106
try {
108107
if (!message || !message.type) {
109108
this.emit('error', new Error('Invalid Bot Framework Message'));
110-
return res.send(400);
109+
return res ? res.send(400) : null;
110+
}
111+
if (!userId) {
112+
if (message.from && message.from.id) {
113+
userId = message.from.id;
114+
}
115+
else {
116+
this.emit('error', new Error('Invalid Bot Framework Message'));
117+
return res ? res.send(400) : null;
118+
}
119+
}
120+
var sessionId;
121+
if (message.botConversationData && message.botConversationData[consts.Data.SessionId]) {
122+
sessionId = message.botConversationData[consts.Data.SessionId];
123+
}
124+
else {
125+
sessionId = uuid.v1();
126+
message.botConversationData = message.botConversationData || {};
127+
message.botConversationData[consts.Data.SessionId] = sessionId;
111128
}
112129
this.emit(message.type, message);
113130
if (message.type == 'Message') {
@@ -117,42 +134,51 @@ var BotConnectorBot = (function (_super) {
117134
dialogId: dialogId,
118135
dialogArgs: dialogArgs
119136
});
120-
ses.on('send', function (message) {
121-
var reply = message || {};
122-
reply.botUserData = utils.clone(ses.userData);
123-
reply.botConversationData = utils.clone(ses.conversationData);
124-
reply.botPerUserInConversationData = utils.clone(ses.perUserInConversationData);
125-
reply.botPerUserInConversationData[consts.Data.SessionState] = ses.sessionState;
126-
if (reply.text && !reply.language) {
127-
reply.language = ses.message.language;
128-
}
129-
if (res) {
130-
_this.emit('reply', reply);
131-
res.send(200, reply);
132-
res = null;
133-
}
134-
else if (ses.message.conversationId) {
135-
reply.from = ses.message.to;
136-
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
137-
reply.replyToMessageId = ses.message.id;
138-
reply.conversationId = ses.message.conversationId;
139-
reply.channelConversationId = ses.message.channelConversationId;
140-
reply.channelMessageId = ses.message.channelMessageId;
141-
reply.participants = ses.message.participants;
142-
reply.totalParticipants = ses.message.totalParticipants;
143-
_this.emit('reply', reply);
144-
_this.post('/bot/v1.0/messages', reply, function (err) {
145-
_this.emit('error', err);
146-
});
147-
}
148-
else {
149-
reply.from = ses.message.from;
150-
reply.to = ses.message.to;
151-
_this.emit('send', reply);
152-
_this.post('/bot/v1.0/messages', reply, function (err) {
153-
_this.emit('error', err);
154-
});
137+
ses.on('send', function (reply) {
138+
reply = reply || {};
139+
reply.botConversationData = message.botConversationData;
140+
if (reply.text && !reply.language && message.language) {
141+
reply.language = message.language;
155142
}
143+
var data = {
144+
userData: ses.userData,
145+
conversationData: ses.conversationData,
146+
perUserConversationData: ses.perUserInConversationData
147+
};
148+
data.perUserConversationData[consts.Data.SessionState] = ses.sessionState;
149+
_this.saveData(userId, sessionId, data, reply, function (err) {
150+
if (res) {
151+
_this.emit('reply', reply);
152+
res.send(200, reply);
153+
res = null;
154+
}
155+
else if (ses.message.conversationId) {
156+
reply.from = ses.message.to;
157+
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
158+
reply.replyToMessageId = ses.message.id;
159+
reply.conversationId = ses.message.conversationId;
160+
reply.channelConversationId = ses.message.channelConversationId;
161+
reply.channelMessageId = ses.message.channelMessageId;
162+
reply.participants = ses.message.participants;
163+
reply.totalParticipants = ses.message.totalParticipants;
164+
_this.emit('reply', reply);
165+
post(_this.options, '/bot/v1.0/messages', reply, function (err) {
166+
if (err) {
167+
_this.emit('error', err);
168+
}
169+
});
170+
}
171+
else {
172+
reply.from = ses.message.from;
173+
reply.to = ses.message.to;
174+
_this.emit('send', reply);
175+
post(_this.options, '/bot/v1.0/messages', reply, function (err) {
176+
if (err) {
177+
_this.emit('error', err);
178+
}
179+
});
180+
}
181+
});
156182
});
157183
ses.on('error', function (err) {
158184
_this.emit('error', err, ses.message);
@@ -163,33 +189,22 @@ var BotConnectorBot = (function (_super) {
163189
ses.on('quit', function () {
164190
_this.emit('quit', ses.message);
165191
});
166-
var sessionState;
167-
if (message.botUserData) {
168-
ses.userData = message.botUserData;
169-
delete message.botUserData;
170-
}
171-
else {
172-
ses.userData = {};
173-
}
174-
if (message.botConversationData) {
175-
ses.conversationData = message.botConversationData;
176-
delete message.botConversationData;
177-
}
178-
else {
179-
ses.conversationData = {};
180-
}
181-
if (message.botPerUserInConversationData) {
182-
if (message.botPerUserInConversationData.hasOwnProperty(consts.Data.SessionState)) {
183-
sessionState = message.botPerUserInConversationData[consts.Data.SessionState];
184-
delete message.botPerUserInConversationData[consts.Data.SessionState];
192+
this.getData(userId, sessionId, message, function (err, data) {
193+
if (!err) {
194+
var sessionState;
195+
ses.userData = data.userData || {};
196+
ses.conversationData = data.conversationData || {};
197+
ses.perUserInConversationData = data.perUserConversationData || {};
198+
if (ses.perUserInConversationData.hasOwnProperty(consts.Data.SessionState)) {
199+
sessionState = ses.perUserInConversationData[consts.Data.SessionState];
200+
delete ses.perUserInConversationData[consts.Data.SessionState];
201+
}
202+
ses.dispatch(sessionState, message);
185203
}
186-
ses.perUserInConversationData = message.botPerUserInConversationData;
187-
delete message.botPerUserInConversationData;
188-
}
189-
else {
190-
ses.perUserInConversationData = {};
191-
}
192-
ses.dispatch(sessionState, message);
204+
else {
205+
_this.emit('error', err, message);
206+
}
207+
});
193208
}
194209
else if (res) {
195210
var msg;
@@ -212,22 +227,69 @@ var BotConnectorBot = (function (_super) {
212227
res.send(500);
213228
}
214229
};
215-
BotConnectorBot.prototype.post = function (path, body, callback) {
216-
var settings = this.options;
217-
var options = {
218-
url: settings.endpoint + path,
219-
body: body
220-
};
221-
if (settings.appId && settings.appSecret) {
222-
options.auth = {
223-
username: settings.appId,
224-
password: settings.appSecret
225-
};
226-
options.headers = {
227-
'Ocp-Apim-Subscription-Key': settings.appSecret
228-
};
230+
BotConnectorBot.prototype.getData = function (userId, sessionId, msg, callback) {
231+
var botPath = '/' + this.options.appId;
232+
var userPath = botPath + '/users/' + userId;
233+
var convoPath = botPath + '/conversations/' + sessionId;
234+
var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId;
235+
var ops = 3;
236+
var data = {};
237+
function load(id, field, store, botData) {
238+
data[field] = botData;
239+
if (store) {
240+
store.get(id, function (err, item) {
241+
if (callback) {
242+
if (!err) {
243+
data[field] = item;
244+
if (--ops == 0) {
245+
callback(null, data);
246+
}
247+
}
248+
else {
249+
callback(err, null);
250+
callback = null;
251+
}
252+
}
253+
});
254+
}
255+
else if (callback && --ops == 0) {
256+
callback(null, data);
257+
}
258+
}
259+
load(userPath, 'userData', this.options.userStore, msg.botUserData);
260+
load(convoPath, 'conversationData', this.options.conversationStore, msg.botConversationData);
261+
load(perUserConvoPath, 'perUserConversationData', this.options.perUserInConversationStore, msg.botPerUserInConversationData);
262+
};
263+
BotConnectorBot.prototype.saveData = function (userId, sessionId, data, msg, callback) {
264+
var botPath = '/' + this.options.appId;
265+
var userPath = botPath + '/users/' + userId;
266+
var convoPath = botPath + '/conversations/' + sessionId;
267+
var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId;
268+
var ops = 3;
269+
function save(id, field, store, botData) {
270+
if (store) {
271+
store.save(id, botData, function (err) {
272+
if (callback) {
273+
if (!err && --ops == 0) {
274+
callback(null);
275+
}
276+
else {
277+
callback(err);
278+
callback = null;
279+
}
280+
}
281+
});
282+
}
283+
else {
284+
msg[field] = botData;
285+
if (callback && --ops == 0) {
286+
callback(null);
287+
}
288+
}
229289
}
230-
request.post(options, callback);
290+
save(userPath, 'botUserData', this.options.userStore, data.userData);
291+
save(convoPath, 'botConversationData', this.options.conversationStore, data.conversationData);
292+
save(perUserConvoPath, 'botPerUserInConversationData', this.options.perUserInConversationStore, data.perUserConversationData);
231293
};
232294
return BotConnectorBot;
233295
})(collection.DialogCollection);
@@ -240,3 +302,21 @@ var BotConnectorSession = (function (_super) {
240302
return BotConnectorSession;
241303
})(session.Session);
242304
exports.BotConnectorSession = BotConnectorSession;
305+
function post(settings, path, body, callback) {
306+
var options = {
307+
method: 'POST',
308+
url: settings.endpoint + path,
309+
body: body,
310+
json: true
311+
};
312+
if (settings.appId && settings.appSecret) {
313+
options.auth = {
314+
username: settings.appId,
315+
password: settings.appSecret
316+
};
317+
options.headers = {
318+
'Ocp-Apim-Subscription-Key': settings.appSecret
319+
};
320+
}
321+
request(options, callback);
322+
}

Node/lib/bots/SlackBot.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var SlackBot = (function (_super) {
8686
return this;
8787
};
8888
SlackBot.prototype.beginDialog = function (address, dialogId, dialogArgs) {
89-
if (!address.user && !address.channel) {
89+
if (!address.channel) {
9090
throw new Error('Invalid address passed to SlackBot.beginDialog().');
9191
}
9292
if (!this.hasDialog(dialogId)) {
@@ -98,13 +98,15 @@ var SlackBot = (function (_super) {
9898
SlackBot.prototype.dispatchMessage = function (bot, msg, dialogId, dialogArgs, smartState) {
9999
var _this = this;
100100
var onError = function (err) {
101-
_this.emit('error', err, msg);
101+
if (err) {
102+
_this.emit('error', err, msg);
103+
}
102104
};
103105
var ses = new SlackSession({
104106
localizer: this.options.localizer,
105107
dialogs: this,
106-
dialogId: this.options.defaultDialogId,
107-
dialogArgs: this.options.defaultDialogArgs
108+
dialogId: dialogId || this.options.defaultDialogId,
109+
dialogArgs: dialogArgs || this.options.defaultDialogArgs
108110
});
109111
ses.on('send', function (reply) {
110112
var teamData = ses.teamData && ses.teamData.id ? utils.clone(ses.teamData) : null;
@@ -117,7 +119,7 @@ var SlackBot = (function (_super) {
117119
if (reply && (reply.text || reply.channelData)) {
118120
var slackReply = _this.toSlackMessage(reply);
119121
if (bot) {
120-
if (slackReply.user && slackReply.user != msg.user) {
122+
if (slackReply.channel && slackReply.channel != msg.channel) {
121123
_this.emit('send', slackReply);
122124
bot.say(slackReply, onError);
123125
}
@@ -127,7 +129,9 @@ var SlackBot = (function (_super) {
127129
}
128130
}
129131
else {
130-
slackReply.user = ses.message.to.address;
132+
if (!slackReply.channel) {
133+
slackReply.channel = msg.channel;
134+
}
131135
_this.emit('send', slackReply);
132136
_this.bot.say(slackReply, onError);
133137
}

Node/lib/bots/TextBot.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ var TextBot = (function (_super) {
103103
_this.emit('quit', message);
104104
});
105105
this.getData(userId, function (err, userData, sessionState) {
106-
ses.userData = userData || {};
107-
ses.dispatch(newSessionState ? null : sessionState, message);
106+
if (!err) {
107+
ses.userData = userData || {};
108+
ses.dispatch(newSessionState ? null : sessionState, message);
109+
}
110+
else {
111+
_this.emit('error', err, message);
112+
}
108113
});
109114
};
110115
TextBot.prototype.getData = function (userId, callback) {

Node/lib/consts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
exports.Data = {
22
SessionState: 'BotBuilder.Data.SessionState',
3+
SessionId: 'BotBuilder.Data.SessionId',
34
Handler: 'BotBuilder.Data.Handler',
45
Group: 'BotBuilder.Data.Group',
56
Intent: 'BotBuilder.Data.Intent',

0 commit comments

Comments
 (0)