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

时间:2016-09-13 15:22:24

标签: javascript node.js botframework

我想用http请求触发我的机器人(例如只输入http://localhost:3978/api/messages/http)所以在触发它之后,它会向连接到这个机器人的每个用户发送一些消息。 我看过这个话题:How to send message later in bot framework? 这就是我到目前为止所做的:

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:发送 - 消息缺少地址或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); 行。 期待你的帮助。

最好的问候。

1 个答案:

答案 0 :(得分:2)

如果您想设置正常的HTTP API路由,我建议使用Restify API样式路由,而不是机器人的/api/messages路由处理程序。

例如:

function apiResponseHandler(req, res, next) {
  // trigger botbuilder actions/dialogs here
  next();
}

server.get('/hello/:name', apiResponseHandler);