微软botframework(node.js):如何从HTTP请求中触发bot

我想用http请求触发我的机器人(例如,只需inputhttp:// localhost:3978 / api / messages / http ),所以在触发它之后,它会向连接到这个机器人的每个用户发送一些消息。 我已经看到了这个话题: 如何在bot框架中稍后发送消息? 这是我迄今为止:

var restify = require('restify'); var builder = require('botbuilder'); var server = restify.createServer(); server.listen(process.env.port || process.env.PORT || 3978, function () { console.log('%s listening to %s', server.name, server.url); }); var connector = new builder.ChatConnector({ appId: process.env.MICROSOFT_APP_ID, appPassword: process.env.MICROSOFT_APP_PASSWORD }); server.post('/api/messages', connector.listen()); var bot = new builder.UniversalBot(connector); bot.dialog('/',function (session) { var reply = session.message; // address: reply.address reply.text = 'Wake up!' console.log(reply.text); bot.send(reply); }); // Create response function function respond(req, res, next) { res.send('hello ' + req.params.name); bot.send(reply); next(); } server.get('/api/messages/:name', respond); 

不幸的是,当我访问我的http:// localhost:3978 / api / messages / http时,它不发送任何消息。 我也试过用

 connector.send('message'); 

但它总是通过我“错误:ChatConnector:发送 – 消息是缺less地址或serviceUrl”。

更新:我已经宣布了一个全球变种的答复

 var globalreply; bot.dialog('/',function (session) { globalreply = session.message; // address: reply.address globalreply.text = 'Wake up!' console.log(globalreply.text); bot.send(globalreply); }); // Create response function function respond(req, res, next) { res.send('hello ' + req.params.name); bot.beginDialog; bot.send(globalreply); next(); } 

但现在它通过了一个错误: TypeError:无法读取未定义的属性“对话” 。 在我的bot.send(globalreply); 线。 期待你的帮助。

最好的祝福。

如果你想build立一个正常的HTTP API路由,我build议使用Restify API风格路由,而不是bot的/api/messages路由处理程序。

例如:

 function apiResponseHandler(req, res, next) { // trigger botbuilder actions/dialogs here next(); } server.get('/hello/:name', apiResponseHandler);