botbuilder
Advanced tools
Comparing version 0.7.2 to 0.8.0
var session = require('./Session'); | ||
var message = require('./Message'); | ||
var dialog = require('./dialogs/Dialog'); | ||
@@ -16,2 +17,3 @@ var actions = require('./dialogs/DialogAction'); | ||
exports.Session = session.Session; | ||
exports.Message = message.Message; | ||
exports.Dialog = dialog.Dialog; | ||
@@ -18,0 +20,0 @@ exports.ResumeReason = dialog.ResumeReason; |
@@ -19,3 +19,4 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
appSecret: process.env['appSecret'] || '', | ||
defaultDialogId: '/' | ||
defaultDialogId: '/', | ||
minSendDelay: 1000 | ||
}; | ||
@@ -134,2 +135,3 @@ this.configure(options); | ||
localizer: this.options.localizer, | ||
minSendDelay: this.options.minSendDelay, | ||
dialogs: this, | ||
@@ -136,0 +138,0 @@ dialogId: dialogId, |
@@ -17,3 +17,4 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
maxSessionAge: 14400000, | ||
defaultDialogId: '/' | ||
defaultDialogId: '/', | ||
minSendDelay: 1000 | ||
}; | ||
@@ -90,2 +91,3 @@ this.configure(options); | ||
localizer: this.options.localizer, | ||
minSendDelay: this.options.minSendDelay, | ||
dialogs: this, | ||
@@ -92,0 +94,0 @@ dialogId: dialogId, |
@@ -20,3 +20,5 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
defaultDialogId: '/', | ||
ambientMentionDuration: 300000 | ||
ambientMentionDuration: 300000, | ||
minSendDelay: 2000, | ||
sendIsTyping: true | ||
}; | ||
@@ -108,2 +110,3 @@ this.configure(options); | ||
localizer: this.options.localizer, | ||
minSendDelay: this.options.minSendDelay, | ||
dialogs: this, | ||
@@ -130,3 +133,3 @@ dialogId: dialogId || this.options.defaultDialogId, | ||
_this.emit('reply', slackReply); | ||
bot.reply(msg, slackReply.text); | ||
bot.reply(msg, slackReply, onError); | ||
} | ||
@@ -150,2 +153,7 @@ } | ||
}); | ||
ses.on('typing', function () { | ||
_this.emit('typing', msg); | ||
_this.bot.say({ id: 1, type: 'typing', channel: msg.channel }, onError); | ||
}); | ||
this.bot.say({ id: 1, type: 'typing', channel: msg.channel }, onError); | ||
var sessionState; | ||
@@ -235,2 +243,25 @@ var message = this.fromSlackMessage(msg); | ||
SlackBot.prototype.fromSlackMessage = function (msg) { | ||
var attachments = []; | ||
if (msg.attachments) { | ||
msg.attachments.forEach(function (value) { | ||
var contentType = value.image_url ? 'image' : 'text/plain'; | ||
var a = { contentType: contentType, fallbackText: value.fallback }; | ||
if (value.image_url) { | ||
a.contentUrl = value.image_url; | ||
} | ||
if (value.thumb_url) { | ||
a.thumbnailUrl = value.thumb_url; | ||
} | ||
if (value.text) { | ||
a.text = value.text; | ||
} | ||
if (value.title) { | ||
a.title = value.title; | ||
} | ||
if (value.title_link) { | ||
a.titleLink; | ||
} | ||
attachments.push(a); | ||
}); | ||
} | ||
return { | ||
@@ -240,2 +271,3 @@ type: msg.type, | ||
text: msg.text, | ||
attachments: attachments, | ||
from: { | ||
@@ -250,2 +282,30 @@ channelId: 'slack', | ||
SlackBot.prototype.toSlackMessage = function (msg) { | ||
var attachments = []; | ||
if (msg.attachments && !msg.channelData) { | ||
msg.attachments.forEach(function (value) { | ||
var a = {}; | ||
if (value.fallbackText) { | ||
a.fallback = value.fallbackText; | ||
} | ||
else { | ||
a.fallback = value.contentUrl ? value.contentUrl : value.text || '<attachment>'; | ||
} | ||
if (value.contentUrl && /^image/i.test(value.contentType)) { | ||
a.image_url = value.contentUrl; | ||
} | ||
if (value.thumbnailUrl) { | ||
a.thumb_url = value.thumbnailUrl; | ||
} | ||
if (value.text) { | ||
a.text = value.text; | ||
} | ||
if (value.title) { | ||
a.title = value.title; | ||
} | ||
if (value.titleLink) { | ||
a.title_link = value.titleLink; | ||
} | ||
attachments.push(a); | ||
}); | ||
} | ||
return msg.channelData || { | ||
@@ -256,2 +316,3 @@ event: 'direct_message', | ||
text: msg.text, | ||
attachments: attachments, | ||
user: msg.to ? msg.to.address : (msg.from ? msg.from.address : null), | ||
@@ -269,2 +330,5 @@ channel: msg.channelConversationId | ||
} | ||
SlackSession.prototype.isTyping = function () { | ||
this.emit('typing'); | ||
}; | ||
SlackSession.prototype.escapeText = function (text) { | ||
@@ -271,0 +335,0 @@ if (text) { |
@@ -17,3 +17,4 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
maxSessionAge: 14400000, | ||
defaultDialogId: '/' | ||
defaultDialogId: '/', | ||
minSendDelay: 1000 | ||
}; | ||
@@ -70,2 +71,3 @@ this.configure(options); | ||
localizer: this.options.localizer, | ||
minSendDelay: this.options.minSendDelay, | ||
dialogs: this, | ||
@@ -72,0 +74,0 @@ dialogId: dialogId, |
var session = require('../Session'); | ||
var consts = require('../consts'); | ||
var utils = require('../utils'); | ||
var dialog = require('./Dialog'); | ||
var consts = require('../consts'); | ||
var DialogAction = (function () { | ||
@@ -95,4 +96,49 @@ function DialogAction() { | ||
}; | ||
DialogAction.validatedPrompt = function (promptType, validator) { | ||
return function validatePromptAction(s, r) { | ||
r = r || {}; | ||
var valid = false; | ||
if (r.response) { | ||
try { | ||
valid = validator(r.response); | ||
} | ||
catch (e) { | ||
s.endDialog({ resumed: dialog.ResumeReason.notCompleted, error: e instanceof Error ? e : new Error(e.toString()) }); | ||
} | ||
} | ||
var canceled = false; | ||
switch (r.resumed) { | ||
case dialog.ResumeReason.canceled: | ||
case dialog.ResumeReason.forward: | ||
case dialog.ResumeReason.back: | ||
canceled = true; | ||
break; | ||
} | ||
if (valid || canceled) { | ||
s.endDialog(r); | ||
} | ||
else if (!s.dialogData.hasOwnProperty('prompt')) { | ||
s.dialogData = utils.clone(r); | ||
s.dialogData.promptType = promptType; | ||
if (!s.dialogData.hasOwnProperty('maxRetries')) { | ||
s.dialogData.maxRetries = 2; | ||
} | ||
var a = utils.clone(s.dialogData); | ||
a.maxRetries = 0; | ||
s.beginDialog(consts.DialogId.Prompts, a); | ||
} | ||
else if (s.dialogData.maxRetries > 0) { | ||
s.dialogData.maxRetries--; | ||
var a = utils.clone(s.dialogData); | ||
a.maxRetries = 0; | ||
a.prompt = s.dialogData.retryPrompt || "I didn't understand. " + s.dialogData.prompt; | ||
s.beginDialog(consts.DialogId.Prompts, a); | ||
} | ||
else { | ||
s.endDialog({ resumed: dialog.ResumeReason.notCompleted }); | ||
} | ||
}; | ||
}; | ||
return DialogAction; | ||
})(); | ||
exports.DialogAction = DialogAction; |
@@ -9,2 +9,4 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
var events = require('events'); | ||
var prompts = require('./Prompts'); | ||
var consts = require('../Consts'); | ||
var DialogCollection = (function (_super) { | ||
@@ -16,2 +18,3 @@ __extends(DialogCollection, _super); | ||
this.dialogs = {}; | ||
this.add(consts.DialogId.Prompts, new prompts.Prompts()); | ||
} | ||
@@ -18,0 +21,0 @@ DialogCollection.prototype.add = function (id, dialog) { |
@@ -211,6 +211,3 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
function beginPrompt(ses, args) { | ||
if (!ses.dialogs.hasDialog(consts.DialogId.Prompts)) { | ||
ses.dialogs.add(consts.DialogId.Prompts, new Prompts()); | ||
} | ||
ses.beginDialog(consts.DialogId.Prompts, args); | ||
} |
@@ -16,3 +16,8 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
this._isReset = false; | ||
this.lastSendTime = new Date().getTime(); | ||
this.sendQueue = []; | ||
this.dialogs = args.dialogs; | ||
if (typeof this.args.minSendDelay !== 'number') { | ||
this.args.minSendDelay = 1000; | ||
} | ||
} | ||
@@ -79,5 +84,4 @@ Session.prototype.dispatch = function (sessionState, message) { | ||
} | ||
this.msgSent = true; | ||
var message = typeof msg == 'string' ? this.createMessage(msg, args) : msg; | ||
this.emit('send', message); | ||
this.delayedEmit('send', message); | ||
return this; | ||
@@ -100,2 +104,5 @@ }; | ||
var ss = this.sessionState; | ||
if (ss.callstack.length > 0) { | ||
ss.callstack[ss.callstack.length - 1].state = this.dialogData || {}; | ||
} | ||
var cur = { id: id, state: {} }; | ||
@@ -159,3 +166,3 @@ ss.callstack.push(cur); | ||
else { | ||
this.emit('quit'); | ||
this.delayedEmit('quit'); | ||
} | ||
@@ -228,2 +235,30 @@ } | ||
}; | ||
Session.prototype.delayedEmit = function (event, message) { | ||
var _this = this; | ||
var now = new Date().getTime(); | ||
var delaySend = function () { | ||
setTimeout(function () { | ||
var entry = _this.sendQueue.shift(); | ||
_this.lastSendTime = now = new Date().getTime(); | ||
_this.emit(entry.event, entry.msg); | ||
if (_this.sendQueue.length > 0) { | ||
delaySend(); | ||
} | ||
}, _this.args.minSendDelay - (now - _this.lastSendTime)); | ||
}; | ||
if (this.sendQueue.length == 0) { | ||
this.msgSent = true; | ||
if ((now - this.lastSendTime) >= this.args.minSendDelay) { | ||
this.lastSendTime = now; | ||
this.emit(event, message); | ||
} | ||
else { | ||
this.sendQueue.push({ event: event, msg: message }); | ||
delaySend(); | ||
} | ||
} | ||
else { | ||
this.sendQueue.push({ event: event, msg: message }); | ||
} | ||
}; | ||
return Session; | ||
@@ -230,0 +265,0 @@ })(events.EventEmitter); |
@@ -5,3 +5,3 @@ { | ||
"description": "Bot Builder is a dialog system for building rich bots on virtually any platform.", | ||
"version": "0.7.2", | ||
"version": "0.8.0", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -7,3 +7,3 @@ var assert = require('assert'); | ||
it('should redirect to another dialog with arguments', function (done) { | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', [ | ||
@@ -31,3 +31,3 @@ function (session) { | ||
var step = 0; | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', [ | ||
@@ -34,0 +34,0 @@ function (session) { |
@@ -7,3 +7,3 @@ var assert = require('assert'); | ||
it('should reply inline with "Hello World"', function (done) { | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', function (session) { | ||
@@ -20,3 +20,3 @@ assert(session.message.text == 'hello'); | ||
it('should reply via event with "Hello World"', function (done) { | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', function (session) { | ||
@@ -34,3 +34,3 @@ session.send('Hello World'); | ||
var inlineReply = false; | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', function (session) { | ||
@@ -54,3 +54,3 @@ session.send('msg1'); | ||
var inlineReply = false; | ||
var bot = new builder.TextBot(); | ||
var bot = new builder.TextBot({ minSendDelay: 0 }); | ||
bot.add('/', function (session) { | ||
@@ -68,4 +68,3 @@ session.send('msg1'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
190442
27
4365