microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.47k stars 2.44k forks source link

Error: req.on is not a function with BotBuilder 3.9.1 #3438

Closed rcarmo closed 6 years ago

rcarmo commented 7 years ago

Bot Info

Issue Description

I am trying to develop a bot in Azure Functions, but with botbuilder 3.9.1, whenever I try to pass the Azure Functions request object to the ChatConnector listener, I get this error:

Exception while executing function: Functions.bot. mscorlib: TypeError: req.on is not a function

This happens near line 48:

ChatConnector.prototype.listen = function () {
        var _this = this;
        return function (req, res) {
            if (req.body) { // this seems to be skipped despite req.body having all the required data
                _this.verifyBotFramework(req, res);
            }
            else {
                var requestData = '';
                req.on('data', function (chunk) { // Errors out here
                    requestData += chunk;
                });
                req.on('end', function () {
                    req.body = JSON.parse(requestData);
                    _this.verifyBotFramework(req, res);
                });
            }
        };
    };

I've inspected req.body using context.log, and get a valid set of data:

{ type: 'message',
     id: '9a947c0fae0b421c89a781291112f19d|0000003',
     timestamp: 2017-09-07T17:57:07.627Z,
     serviceUrl: 'https://webchat.botframework.com/',
     channelId: 'webchat',
     from: { id: '2WYuDtYFHhv', name: 'You' },
     conversation: { id: '9a947c0fae0b421c89a781291112f19d' },
     recipient: { id: 'FaaS@9iE0S4LW9r4', name: 'faas-bot' },
     textFormat: 'plain',
     locale: 'en-US',
     text: 'hello?',
     channelData: { clientActivityId: '1504806655964.09441823711544628.5' } },

I've been looking at whatever few samples there are out there using Azure Functions, and the ones that work all seem to use botbuilder 3.4 or 3.5, so I reverted from 3.9.1 to 3.4.2 and overcame this, which confirms that something in the intermediate releases broke Azure Functions deployments.

(I had other issues, though, and I really need the features in 3.8+, so that's irrelevant).

Code Example

This is how I am trying to bind the context and response objects in Azure Functions:


if (process.env.FUNCTIONS_EXTENSION_VERSION) {
    // If we are inside Azure Functions, export the standard handler.
    module.exports = function (context, req) {
        context.log("Passing body", req.body);
        listener(req, context.res);
    }
} else {
    // Local server for testing
    var server = require('restify').createServer();
    server.post('/api/messages', listener);
    server.listen(process.env.port || process.env.PORT || 3978, function () {
        console.log('%s listening to %s', server.name, server.url);
    });
}

Full source code is here

Expected Behavior

I expected to be able to run the ChatConnector listener inside Azure Functions with recent releases, because Functions provides a much richer environment to implement the back-end logic required for a complex bot.

Existing third-party samples that are purported to work inside Azure Functions (that I've been able to find) all use older versions of botbuilder and no longer work, and there is no detailed documentation regarding how to go about doing this.

References:

Microsoft/BotBuilder-Azure#10

rcarmo commented 6 years ago

Hi there!

I just (finally) got it working.

In my case, I was missing a character from the application secret, which was very hard to spot since I only figured it out when I decided to set up a new environment to do deeper debugging and it worked first time.

So thanks for taking the time to go through this issue.

(edit: removed mail junk, clarifications)

JasonSowers commented 6 years ago

Glad I was wrong and you got it working, going to delete my comment so others aren't misled.

rcarmo commented 6 years ago

Thanks. Re-edited mine too. Unsure if I should file a bug regarding the way app secrets are validated in this context, but the working code is here.

jabreuar commented 5 years ago

@rcarmo how did you get the application secret? I am getting an error when a try to generate it locally: Error: You are attempting to perform an operation which needs access to the secret and --secret is incorrect.

wiazur commented 5 years ago

@jabreuar , not sure if you solved your bug, but I had the same error locally, and deleting the padlock value (leaving it like "padlock": "") in your .bot file solved it. The new v4 bot for Node seems to automatically add a padlock value to your .bot file, but old ones did not. The app seems to run fine with a missing padlock value.

wethil commented 5 years ago

@rcarmo by the way, same error is occurred on AWS lamda. Do we have a solution?