0
votes

I've created a bot while using MS Bot Framework and deploy it to Azure.

After the deployment, the bot is returning HTTP 500 error when we're trying the '/api/messages' URL.

Here my app.js :

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dialog_service_1 = require("./services/dialog-service");
const authentification_service_1 = require("./services/authentification-service");
const restify = require("restify");
const bot_service_1 = require("./services/bot-service");
const utilities_service_1 = require("./services/utilities-service");
require("dotenv").config();
let botService = new bot_service_1.BotService();
// let utilitiesService = new UtilitiesService(__dirname + '/assets/labels.json');
let dialogService = new dialog_service_1.DialogService(bot_service_1.BotService.bot);
let port = process.env.port || process.env.PORT || '3978';
const server = restify.createServer({
    formatters: {
        'text/html': function (req, res, body) {
            return body.toString();
        }
    }
});
console.log('server created');
// change done for restify 5.X+ (mapParams should be specified @ true)
server.use(restify.plugins.bodyParser({
    mapParams: true
}));
console.log('trying to listening..');
server.listen(port, () => {
    console.log('%s server listening to %s', server.name, server.url);
});
console.log('listening');
console.log('mounting styles folder...');
//add the build/styles folder to the restify server
server.get(/\/styles\/?.*/, restify.plugins.serveStatic({
    directory: __dirname + '/assets'
}));
console.log('mounted');
console.log('mounting api/messages endpoint...');
// entry point of your bot
server.post("/api/messages", bot_service_1.BotService.bot.connector("*").listen());
console.log('mounted...');
console.log('mounting api/oauthcallback endpoint...');
//callback handling
server.post("/api/oauthcallback", (req, res, next) => {
    let authorizationCode = req.params.code;
    if (authorizationCode !== undefined) {
        authentification_service_1.AuthentificationService.acquireTokenWithAuthorizationCode(authorizationCode).then((response) => {
            let state = req.params.state;
            if (state) {
                let address = JSON.parse(state);
                response.state = state;
                bot_service_1.BotService.bot.beginDialog(address, "/oauth-success", response);
            }
            utilities_service_1.UtilitiesService.readFile(__dirname + '/assets/html/callback.html').then(body => {
                res.send(200, body, { "Content-Length": Buffer.byteLength(body).toString(), "Content-Type": "text/html" });
                res.end();
            });
        }).catch((errorMessage) => {
            var body = "<html><body>" + errorMessage + "</body></html>";
            res.send(200, body, { "Content-Length": Buffer.byteLength(body).toString(), "Content-Type": "text/html" });
            res.end();
        });
    }
    else {
        var body = "<html><body>" + "unable to retrieve the authentication code" + "</body></html > ";
        res.send(200, body, { "Content-Length": Buffer.byteLength(body).toString(), "Content-Type": "text/html" });
        res.end();
    }
});
console.log('mounted');
//# sourceMappingURL=app.js.map

I've added some logs to help me, all console.log() is reached. so it seems that the server is well started...

Thanks for your help.

1
Are you calling api/messages directly. That endpoint is automatically called by bot framework, when bot receives user messages on configured channels. There is no need to call it manually. Is this what you are doing? - Master Chief
That’s the end point defined in the bot framework config yes - Filsdegandalf
I meant, are you calling that end-point manually. There is no need to call that end-point manually. The only important piece of code on that end-point is calling bot connector's listen method. - Master Chief

1 Answers

0
votes

I noticed you aren't initiating a bot and starting a dialog. When I try your code as-is I receive a 502 error. When I introduce code for the bot the error disappears and the bot responds, as expected.

Since I don't have access to all of your files, I had to remove the associated code calls. So, I can't say if the error you are receiving is related to any of that code.

I used connector.listen() in the server.post for 'api/messages'. Defining connector, as shown below, follows the basic setup found in the documentation for building a bot using Node.

Hope this helps.

Steve.

'use string';

const builder = require('botbuilder');
const restify = require('restify');
require('dotenv').config();

let port = process.env.port || process.env.PORT || '3978';

let server = restify.createServer({
    formatters: {
        'text/html': function (req, res, body) {
            return body.toString();
        }
    }
});

// change done for restify 5.X+ (mapParams should be specified @ true)
server.use(restify.plugins.bodyParser({
    mapParams: true
}));

server.listen(port, () => {
    console.log('%s server listening to %s', server.name, server.url);
});

// entry point of your bot
let connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata
});

server.post('/api/messages', connector.listen());

//callback handling
server.post('/api/oauthcallback', (req, res, next) => {
    var authorizationCode = req.params.code;
    if (authorizationCode !== undefined) {
        console.log('authorization code provided');
    }
    else {
        console.log('authorization code not provided');
    }
});

// inMemoryStorage should only be used for testing. It is not stable for a production environment
let inMemoryStorage = new builder.MemoryBotStorage();
let bot = new builder.UniversalBot(connector).set('storage', inMemoryStorage);

bot.dialog('/', [
    function (session) {
        session.send('Hi');
    }
]);