user70192
user70192

Reputation: 14214

Integrating Microsoft Bot Framework with Node Express Web site

I am experimenting with the Microsoft Bot Framework. In my experiment, I'm trying to integrate "Hello World" into an existing web app. My web app is an express Node.js app.

In my Node.js app, I want to have a web page that has a text box that let's me send a message to a bot. Basically, I'm trying to imitate the framework channel emulator in a web page. In an attempt to do this, I have a webpage with the following:

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;
}

Then, on the server side, in my Node.js app, which is using Express, I have the following:

// 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, {});
});

I'm not sure how to get "Hello World" back to my web page. I see the call to session.send, but I don't see a way to connect session to the res object. Or am I misunderstanding the architecture altogether?

Upvotes: 0

Views: 1858

Answers (2)

Chris
Chris

Reputation: 445

The callback in your app.post should be connector.listen(), and you should only define/call connector a single time. Calling connector.listen() will return a function suitable for express-style callbacks, and because your connector is wired up to bot, your bot dialog routes will be activated on POST.

Here is a sample app:

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);
});

And I see that you are attempting to handle potential errors. If you need to communicate a failure to the client, you should call session.error() in lieu of 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');
  }
});

Upvotes: 2

Lars
Lars

Reputation: 10092

Try using the 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

Upvotes: 0

Related Questions