Skip to content

Commit 0085760

Browse files
committed
Updated LKG build and package.json version.
1 parent 72ece86 commit 0085760

10 files changed

+473
-459
lines changed

Node/lib/Message.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var session = require('./Session');
2+
var sprintf = require('sprintf-js');
23
var Message = (function () {
34
function Message() {
45
}
@@ -7,12 +8,13 @@ var Message = (function () {
78
m.language = language;
89
return this;
910
};
10-
Message.prototype.setText = function (ses, msg) {
11+
Message.prototype.setText = function (ses, prompts) {
1112
var args = [];
1213
for (var _i = 2; _i < arguments.length; _i++) {
1314
args[_i - 2] = arguments[_i];
1415
}
1516
var m = this;
17+
var msg = typeof prompts == 'string' ? msg : Message.randomPrompt(prompts);
1618
args.unshift(msg);
1719
m.text = session.Session.prototype.gettext.apply(ses, args);
1820
return this;
@@ -22,6 +24,15 @@ var Message = (function () {
2224
m.text = ses.ngettext(msg, msg_plural, count);
2325
return this;
2426
};
27+
Message.prototype.composePrompt = function (ses, prompts) {
28+
var args = [];
29+
for (var _i = 2; _i < arguments.length; _i++) {
30+
args[_i - 2] = arguments[_i];
31+
}
32+
var m = this;
33+
m.text = Message.composePrompt(ses, prompts, args);
34+
return this;
35+
};
2536
Message.prototype.addAttachment = function (attachment) {
2637
var m = this;
2738
if (!m.attachments) {
@@ -35,6 +46,19 @@ var Message = (function () {
3546
m.channelData = data;
3647
return this;
3748
};
49+
Message.randomPrompt = function (prompts) {
50+
var i = Math.round(Math.random() * prompts.length);
51+
return prompts[i];
52+
};
53+
Message.composePrompt = function (ses, prompts, args) {
54+
var connector = '';
55+
var prompt = '';
56+
for (var i = 0; i < prompts.length; i++) {
57+
prompt += connector + ses.gettext(Message.randomPrompt(prompts[1]));
58+
connector = ' ';
59+
}
60+
return args && args.length > 0 ? sprintf.vsprintf(prompt, args) : prompt;
61+
};
3862
return Message;
3963
})();
4064
exports.Message = Message;

Node/lib/Session.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ var sprintf = require('sprintf-js');
88
var events = require('events');
99
var Session = (function (_super) {
1010
__extends(Session, _super);
11-
function Session(args) {
11+
function Session(options) {
1212
_super.call(this);
13-
this.args = args;
13+
this.options = options;
1414
this.msgSent = false;
1515
this._isReset = false;
1616
this.lastSendTime = new Date().getTime();
1717
this.sendQueue = [];
18-
this.dialogs = args.dialogs;
19-
if (typeof this.args.minSendDelay !== 'number') {
20-
this.args.minSendDelay = 1000;
18+
this.dialogs = options.dialogs;
19+
if (typeof this.options.minSendDelay !== 'number') {
20+
this.options.minSendDelay = 1000;
2121
}
2222
}
2323
Session.prototype.dispatch = function (sessionState, message) {
@@ -60,8 +60,8 @@ var Session = (function (_super) {
6060
};
6161
Session.prototype.ngettext = function (msgid, msgid_plural, count) {
6262
var tmpl;
63-
if (this.args.localizer && this.message) {
64-
tmpl = this.args.localizer.ngettext(this.message.language || '', msgid, msgid_plural, count);
63+
if (this.options.localizer && this.message) {
64+
tmpl = this.options.localizer.ngettext(this.message.language || '', msgid, msgid_plural, count);
6565
}
6666
else if (count == 1) {
6767
tmpl = msgid;
@@ -127,6 +127,10 @@ var Session = (function (_super) {
127127
args[_i - 1] = arguments[_i];
128128
}
129129
var ss = this.sessionState;
130+
if (!ss || !ss.callstack || ss.callstack.length == 0) {
131+
console.error('ERROR: Too many calls to session.endDialog().');
132+
return this;
133+
}
130134
var m;
131135
var r = {};
132136
if (result) {
@@ -172,6 +176,10 @@ var Session = (function (_super) {
172176
Session.prototype.reset = function (dialogId, dialogArgs) {
173177
this._isReset = true;
174178
this.sessionState.callstack = [];
179+
if (!dialogId) {
180+
dialogId = this.options.dialogId;
181+
dialogArgs = dialogArgs || this.options.dialogArgs;
182+
}
175183
this.beginDialog(dialogId, dialogArgs);
176184
return this;
177185
};
@@ -191,7 +199,7 @@ var Session = (function (_super) {
191199
try {
192200
var ss = this.sessionState;
193201
if (ss.callstack.length == 0) {
194-
this.beginDialog(this.args.dialogId, this.args.dialogArgs);
202+
this.beginDialog(this.options.dialogId, this.options.dialogArgs);
195203
}
196204
else if (this.validateCallstack()) {
197205
var cur = ss.callstack[ss.callstack.length - 1];
@@ -201,7 +209,7 @@ var Session = (function (_super) {
201209
}
202210
else {
203211
console.error('Callstack is invalid, resetting session.');
204-
this.reset(this.args.dialogId, this.args.dialogArgs);
212+
this.reset(this.options.dialogId, this.options.dialogArgs);
205213
}
206214
}
207215
catch (e) {
@@ -210,8 +218,8 @@ var Session = (function (_super) {
210218
};
211219
Session.prototype.vgettext = function (msgid, args) {
212220
var tmpl;
213-
if (this.args.localizer && this.message) {
214-
tmpl = this.args.localizer.gettext(this.message.language || '', msgid);
221+
if (this.options.localizer && this.message) {
222+
tmpl = this.options.localizer.gettext(this.message.language || '', msgid);
215223
}
216224
else {
217225
tmpl = msgid;
@@ -239,11 +247,11 @@ var Session = (function (_super) {
239247
if (_this.sendQueue.length > 0) {
240248
delaySend();
241249
}
242-
}, _this.args.minSendDelay - (now - _this.lastSendTime));
250+
}, _this.options.minSendDelay - (now - _this.lastSendTime));
243251
};
244252
if (this.sendQueue.length == 0) {
245253
this.msgSent = true;
246-
if ((now - this.lastSendTime) >= this.args.minSendDelay) {
254+
if ((now - this.lastSendTime) >= this.options.minSendDelay) {
247255
this.lastSendTime = now;
248256
this.emit(event, message);
249257
}

0 commit comments

Comments
 (0)