我想用 http 请求触发我的 bot(例如只输入 http://localhost:3978/api/messages/http )所以在触发它之后,它会向连接到这个 bot 的每个用户发送一些消息。 我看过这个话题: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();
}
但现在它通过我一个错误: 类型错误:无法读取未定义的属性“对话”。 在我的 bot.send(globalreply); 行。 期待您的帮助。
最好的问候。
最佳答案
如果你想设置一个普通的 HTTP API 路由,我建议使用 Restify API 样式路由,而不是机器人的 /api/messages
路由处理程序。
例如:
function apiResponseHandler(req, res, next) {
// trigger botbuilder actions/dialogs here
next();
}
server.get('/hello/:name', apiResponseHandler);
关于javascript - 微软 botframework (node.js) : how to trigger bot from HTTP request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39473675/