将 Microsoft Bot Framework 与 Node Express 网站集成

Integrating Microsoft Bot Framework with Node Express Web site

我正在尝试将 Microsoft Bot Framework. In my experiment, I'm trying to integrate "Hello World" 放入现有的网络应用程序中。我的网络应用程序是一个快速 Node.js 应用程序。

在我的 Node.js 应用程序中,我想要一个包含文本框的网页,让我可以向机器人发送消息。基本上,我试图在网页中模仿框架通道模拟器。为了尝试这样做,我有一个包含以下内容的网页:

bot.html

<form>
  <div class="input-group">
    <input id="message" type="text" class="form-control" placeholder="">
    <span class="input-group-btn">
      <button id="sendButton" class="btn btn-secondary" type="button">send</button>
    </span>
  </div>
</form>

...

$('#sendButton').click(onSendButtonClick);
function onSendButtonClick() {
    var message = $('#message').val();
    if (message) {
        $.post('/my-bot', function(data, status) {
            console.log(data);
            console.log(status);
        });
    }
    return false;
}

然后,在服务器端,在我使用 Express 的 Node.js 应用程序中,我有以下内容:

// This route is intended to listen for messages POSTed from the text box on my web site.
app.post('/my-bot', function(req, res) {
    try {
        // Not sure about this...
        let connector = new BotBuilder.ConsoleConnector().listen();            
        let bot = new BotBuilder.UniversalBot(connector );
        bot.dialog('/', function (session) {                
            session.send('Hello World');
        });
    } catch (ex) {
        res.status(500);
        res.end();
    }
});

// The following serves up my web page
app.use('/my-bot', function(req, res) { 
    let view = './bot.html';        
    res.render(view, {});
});

我不确定如何 "Hello World" 回到我的网页。我看到了对 session.send 的调用,但我没有看到将 session 连接到 res 对象的方法。还是我完全误解了架构?

尝试使用 Direct Line API.

The Direct Line API is a simple REST API for connecting directly to a single bot. This API is intended for developers writing their own client applications, web chat controls, mobile apps, or service-to-service applications that will talk to their bot

app.post 中的回调应该是 connector.listen(),您应该只 define/call connector 一次。调用 connector.listen() 将 return 一个适用于快速回调的函数,并且由于您的连接器连接到 bot,您的机器人对话路由将在 POST 上激活。

这是一个示例应用程序:

var express = require('express')
  , http = process.env.HTTPS == 'on' ? require('https') : require('http')
  , builder = require('botbuilder');

var app = express()
  , server = http.createServer(app)
  , port = process.env.port || 3000
  , config = { appId: process.env.MICROSOFT_APP_ID, appPassword: process.env.MICROSOFT_APP_PASSWORD }
  , connector = new builder.ChatConnector(config)
  , bot = new builder.UniversalBot(connector);

// define bot dialog routes
bot.dialog('/', function(session) {
  session.send('Hello World');
});

// define app HTTP routes
app

  // respond to basic HTTP GET
  .get('/', function(req, res) {
    res.send('hello world');
  })

  // respond to bot messages
  .post('/api/messages', connector.listen());

// start the server
server.listen(port, function() {
  console.log('Listening on %s', port);
});

而且我看到您正在尝试处理潜在的错误。如果您需要向客户端传达故障,您应该调用 session.error() 代替 session.send():

bot.dialog('/', function (session) {

  // do some processing
  var gotError = true;

  // respond with error
  if (gotError) {
    session.error(new Error('oops'))

  // respond normally
  } else {
    session.send('Hello World');
  }
});