diff --git a/Node/core/lib/botbuilder.d.ts b/Node/core/lib/botbuilder.d.ts index d041168f44..6d3b76fc43 100644 --- a/Node/core/lib/botbuilder.d.ts +++ b/Node/core/lib/botbuilder.d.ts @@ -909,6 +909,10 @@ export interface IUniversalBotSettings { /** Implemented by connector plugins for the UniversalBot. */ export interface IConnector { + + /** Used to register a handler for receiving incoming invoke events. */ + onInvoke?(handler: (event: IEvent, cb?: (err: Error, body: any, status?: number) => void) => void): void; + /** Called by the UniversalBot at registration time to register a handler for receiving incoming events from a channel. */ onEvent(handler: (events: IEvent[], callback?: (err: Error) => void) => void): void; @@ -3040,6 +3044,9 @@ export class ChatConnector implements IConnector, IBotStorage { /** Registers an Express or Restify style hook to listen for new messages. */ listen(): (req: any, res: any) => void; + /** Used to register a handler for receiving incoming invoke events. */ + onInvoke(handler: (event: IEvent, cb?: (err: Error, body: any, status?: number) => void) => void): void; + /** Called by the UniversalBot at registration time to register a handler for receiving incoming events from a channel. */ onEvent(handler: (events: IEvent[], callback?: (err: Error) => void) => void): void; @@ -3064,6 +3071,9 @@ export class ConsoleConnector implements IConnector { /** Sends a message through the connector. */ processMessage(line: string): ConsoleConnector; + /** Used to register a handler for receiving incoming invoke events. */ + onInvoke(handler: (event: IEvent, cb?: (err: Error, body: any, status?: number) => void) => void): void; + /** Called by the UniversalBot at registration time to register a handler for receiving incoming events from a channel. */ onEvent(handler: (events: IEvent[], callback?: (err: Error) => void) => void): void; diff --git a/Node/core/lib/bots/ChatConnector.js b/Node/core/lib/bots/ChatConnector.js index afc5da5ff7..b46a3aa4df 100644 --- a/Node/core/lib/bots/ChatConnector.js +++ b/Node/core/lib/bots/ChatConnector.js @@ -132,7 +132,10 @@ var ChatConnector = (function () { } }; ChatConnector.prototype.onEvent = function (handler) { - this.handler = handler; + this.onEventHandler = handler; + }; + ChatConnector.prototype.onInvoke = function (handler) { + this.onInvokeHandler = handler; }; ChatConnector.prototype.send = function (messages, done) { var _this = this; @@ -358,21 +361,36 @@ var ChatConnector = (function () { } } }; - ChatConnector.prototype.dispatch = function (messages, res) { - var _this = this; - var list = Array.isArray(messages) ? messages : [messages]; - list.forEach(function (msg) { - try { - _this.prepIncomingMessage(msg); - logger.info(msg, 'ChatConnector: message received.'); - _this.handler([msg]); + ChatConnector.prototype.dispatch = function (msg, res) { + try { + this.prepIncomingMessage(msg); + logger.info(msg, 'ChatConnector: message received.'); + if (this.isInvoke(msg)) { + this.onInvokeHandler(msg, function (err, body, status) { + if (err) { + res.status(500); + res.end(); + logger.error('Received error from invoke handler: ', err.message || ''); + } + else { + res.send(status || 200, body); + } + }); } - catch (e) { - console.error(e instanceof Error ? e.stack : e.toString()); + else { + this.onEventHandler([msg]); + res.status(202); + res.end(); } - }); - res.status(202); - res.end(); + } + catch (e) { + console.error(e instanceof Error ? e.stack : e.toString()); + res.status(500); + res.end(); + } + }; + ChatConnector.prototype.isInvoke = function (message) { + return (message && message.type && message.type.toLowerCase() == consts.invokeType); }; ChatConnector.prototype.postMessage = function (msg, cb) { logger.info(address, 'ChatConnector: sending message.'); diff --git a/Node/core/lib/bots/ConsoleConnector.js b/Node/core/lib/bots/ConsoleConnector.js index 27703af369..b6371f65c3 100644 --- a/Node/core/lib/bots/ConsoleConnector.js +++ b/Node/core/lib/bots/ConsoleConnector.js @@ -23,7 +23,7 @@ var ConsoleConnector = (function () { return this; }; ConsoleConnector.prototype.processMessage = function (line) { - if (this.handler) { + if (this.onEventHandler) { var msg = new Message_1.Message() .address({ channelId: 'console', @@ -33,12 +33,15 @@ var ConsoleConnector = (function () { }) .timestamp() .text(line); - this.handler([msg.toMessage()]); + this.onEventHandler([msg.toMessage()]); } return this; }; ConsoleConnector.prototype.onEvent = function (handler) { - this.handler = handler; + this.onEventHandler = handler; + }; + ConsoleConnector.prototype.onInvoke = function (handler) { + this.onInvokeHandler = handler; }; ConsoleConnector.prototype.send = function (messages, done) { for (var i = 0; i < messages.length; i++) { diff --git a/Node/core/lib/bots/Library.js b/Node/core/lib/bots/Library.js index bdd7940e4f..eab8d2abfb 100644 --- a/Node/core/lib/bots/Library.js +++ b/Node/core/lib/bots/Library.js @@ -49,12 +49,11 @@ var Library = (function (_super) { return this._localePath; }; Library.prototype.recognize = function (context, callback) { - var skipRecognize = (context.intent && context.libraryName === this.name); - if (this.recognizers.length > 0 && !skipRecognize) { + if (this.recognizers.length > 0 && context.libraryName !== this.name) { this.recognizers.recognize(context, callback); } else { - callback(null, context.intent); + callback(null, context.intent || { intent: 'None', score: 0.0 }); } }; Library.prototype.recognizer = function (plugin) { diff --git a/Node/core/lib/bots/UniversalBot.js b/Node/core/lib/bots/UniversalBot.js index bf5b1d3a2f..845633a152 100644 --- a/Node/core/lib/bots/UniversalBot.js +++ b/Node/core/lib/bots/UniversalBot.js @@ -326,10 +326,8 @@ var UniversalBot = (function (_super) { var _this = this; var context = session.toRecognizeContext(); this.recognize(context, function (err, topIntent) { - if (topIntent && topIntent.score > 0) { - context.intent = topIntent; - context.libraryName = _this.name; - } + context.intent = topIntent; + context.libraryName = _this.name; var results = Library_1.Library.addRouteResult({ score: 0.0, libraryName: _this.name }); async.each(_this.libraryList(), function (lib, cb) { lib.findRoutes(context, function (err, routes) { diff --git a/Node/core/lib/consts.js b/Node/core/lib/consts.js index 51fe43294f..694ac2eb49 100644 --- a/Node/core/lib/consts.js +++ b/Node/core/lib/consts.js @@ -1,6 +1,7 @@ "use strict"; exports.agent = 'botbuilder'; exports.messageType = 'message'; +exports.invokeType = 'invoke'; exports.defaultConnector = '*'; exports.emulatorChannel = 'emulator'; exports.Errors = { diff --git a/Node/core/package.json b/Node/core/package.json index 20782215b1..3afdc3f114 100644 --- a/Node/core/package.json +++ b/Node/core/package.json @@ -2,7 +2,7 @@ "name": "botbuilder", "author": "Microsoft Corp.", "description": "Bot Builder is a dialog system for building rich bots on virtually any platform.", - "version": "3.5.4", + "version": "3.6.0", "license": "MIT", "keywords": [ "botbuilder",