【问题标题】:Microsoft botframework (node.js): how to trigger bot from HTTP requestMicrosoft botframework (node.js):如何从 HTTP 请求触发机器人
【发布时间】:2016-09-13 15:22:23
【问题描述】:

我想用 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); 行。 期待您的帮助。

最好的问候。

【问题讨论】:

    标签: javascript node.js botframework


    【解决方案1】:

    如果你想设置一个普通的 HTTP API 路由,我建议使用Restify API 风格的路由,而不是机器人的/api/messages 路由处理程序。

    例如:

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

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 2018-08-07
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2020-01-29
      相关资源
      最近更新 更多