Search code examples
node.jsazurebotframework

How can I connect a NodeJs Server to a Azure WebChat?


Good Afternoon from Spain, I'm trying to set an intermediate nodejs server between the client and the actual bot, but when I try to establish a socket connection with my bot It says there's a handshake protocol failure.

Right now my code is

Server app.js

require('dotenv').config();
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require("express-session");
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const Sequelize = require('sequelize');
const sequelize = new Sequelize(process.env.DBNAME, process.env.DBUSER, process.env.DBPASSWORD, {
  dialect: 'mysql'
});
const sequelizeStoreSession = new SequelizeStore({ db: sequelize })

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// default value for title local
app.locals.title = 'Human Data Demo';

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// SESSION PERSISTANCE USING COOKIES

sequelizeStoreSession.sync();
// Enable authentication using session + passport
app.use(session({
  secret: 'secret',
  resave: true,
  saveUninitialized: true,
  cookie: {
    httpOnly: true,
    maxAge: 2419200000
  },
  store: sequelizeStoreSession
}))

require('./passport/index')(app);

const index = require('./routes/index');
app.use('/', index);

// catch 404 and forward to error handler
app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});


module.exports = app;

The route file I'm using to connect the server to the bot

router.post('/messages/', (req, res, next) => {
  const client = new WebSocketClient();
  console.log('cliente: ', client);
  client.on('connectFailed', err => {
    console.log(err);
    next(err);
  })

  client.on('connect', connection => {
    console.log('connected');
    connection.on('error', function (error) {
      console.log("Connection Error: " + error.toString());
    });
    connection.on('close', function () {
      console.log('echo-protocol Connection Closed');
    });
    connection.on('message', function (message) {
      if (message.type === 'utf8') {
        console.log("Received: '" + message.utf8Data + "'");
      }
    });
    connection.send(req.body.text);
  })

  client.connect('http://55dce78c.ngrok.io/api/messages', 'echo-protocol')
  console.log('connectado ', client)
})

And the actual bot

var restify = require('restify');
var builder = require('botbuilder');
var http = require('http');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
    console.log('listening to %s', server.name, server.url);
});

const connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());

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

Could you give my some advice?

Edit: This is the error I'm getting right now

POST /api/project/messages - - ms - -                                                                                                                                       Error: Server responded with a non-101 status: 502 Bad Gateway                                                                                                              Response Headers Follow:                                                                                                                                                    cache-control: no-cache                                                                                                                                                     connection: close                                                                                                                                                           content-type: text/html                                                                                                                                                                                                                                                                                                                                     at WebSocketClient.failHandshake (C:\Users\David\Desktop\HumanData\Proyecto Acceso API\API_FINAL\node_modules\websocket\lib\WebSocketClient.js:326:32)                      at ClientRequest.<anonymous> (C:\Users\David\Desktop\HumanData\Proyecto Acceso API\API_FINAL\node_modules\websocket\lib\WebSocketClient.js:265:18)                          at emitOne (events.js:116:13)                                                                                                                                               at ClientRequest.emit (events.js:211:7)                                                                                                                                     at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)                                                                                               at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)                                                                                                              at Socket.socketOnData (_http_client.js:440:20)                                                                                                                             at emitOne (events.js:116:13)                                                                                                                                               at Socket.emit (events.js:211:7)                                                                                                                                            at addChunk (_stream_readable.js:263:12)                                                                                                                                    at readableAddChunk (_stream_readable.js:250:11)                                                                                                                            at Socket.Readable.push (_stream_readable.js:208:10)                                                                                                                        at TCP.onread (net.js:597:20) 

Solution

  • Your WebSocket request is failing because the client is expecting a WebSocket Handshake Response. The /api/messages endpoint isn't for initiating a WebSocket connection but rather to send messages to the bot so it's giving you a Bad Gateway and the handshake protocol is failing.

    If you wan't to connect to a bot through a WebSocket you need to use the DirectLine API doing the following:

    • Generate an access token: POST /v3/directline/tokens/generate

    • Start a conversation with the bot: POST /v3/directline/conversations

    This gives you a Conversation object with a conversationId and streamUrl in which all the bot responses will come from, and you can use it like this:

    const WebSocket = require('ws');
    
    const ws = new WebSocket(streamUrl);
    
    ws.on('message', function incoming(data) {
      // this is a bot response
      console.log(data);
    });
    

    If the conversation was already started and you already have a conversationId you can the endpoint for reconnecting to conversations.

    All of this is fine if you want to do it by hand but I suggest you use DirectLine JS which will make things easier for you.