botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[FB Messenger] failed: [400] Bad Request on Facebook Messenger

Open pourmesomecode opened this issue 9 years ago • 7 comments

System Information

  • SDK Language: Node.js
  • BotBuilder: v3.8.4
  • Restify: v4.3.0

Issue Description

The issue seems to arise after talking to my chatbot (via Facebook Messenger) for a period of time. I then randomly started getting a 400 bad request error.

If I test it on the emulator in botframework I don't get the error and since upon exiting/refreshing the emulator it deletes the persistent storage and after reading through the issue here: https://github.com/Microsoft/BotBuilder/issues/2272 I think it's something related to the persistent storage.

I've tried setting the bot.set('persistConversationData', false); persistent storage to false but I'm still getting the error.

I followed the setup suggestions on the botframework docs and I'm struggling to understand how I can console.log out all incoming requests to get some more information about the 400 error I'm getting.

Code example

  • server.js file
import request from 'request';
import botDefault from './bot';

const restify = require('restify');
const builder = require('botbuilder');

const server = restify.createServer();
server.listen(process.env.PORT || 5000, () => {
  console.log('%s 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);
bot.set('persistConversationData', false);
bot.set('persistUserData', false);

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

bot.beginDialogAction('Help', 'Help', { matches: /^help/i });
bot.beginDialogAction('/', '/', { matches: /^get started|Get Started/i });

botDefault(bot, builder);

Actual error message

[0] Error: Request to 'https://state.botframework.com/v3/botstate/facebook/conversations/539814889476018-1490746034346692/users/539814889476018' failed: [400] Bad Request

With some other useless info below that, I don't need to include.


Could anyone give me some direction what the source of this issue is and how I might go about debugging it? I've asked the question on stackoverflow (https://stackoverflow.com/questions/45465757/microsoft-botframework-failed-400-bad-request-on-facebook-messenger) but having only 18views within a day seemed fairly unlikely I was going to find any solution in the time I wanted to.

pourmesomecode avatar Aug 02 '17 21:08 pourmesomecode

@pourmesomecode What code is inside import botDefault from './bot';?

nwhitmont avatar Aug 02 '17 23:08 nwhitmont

@nwhitmont

/**
import some more dialogs here
**/

const botDefault = (bot, builder) => {
  /**
   * Initiate bot conversations
   */

  bot.dialog('/', [
    (session) => {
      session.send(`Hello ${session.message.address.user.name}`);
      session.send('some text');
      const msg = new builder.Message(session)
        .text('menu')
        .suggestedActions(
        builder.SuggestedActions.create(
          session, [
            builder.CardAction.imBack(session, 'opt1', 'op1'),
            builder.CardAction.imBack(session, 'opt2', 'opt2'),
            builder.CardAction.imBack(session, 'op3', 'op3'),
          ],
        ));

      builder.Prompts.text(session, msg);
    },
    (session, results) => {
      if (results.response === 'op1') {
       /* starts a new dialog */
      } else if (results.response === 'op2') {
       /* starts a new dialog */
      } else if (results.response === 'op3') {
       /* starts a new dialog */
      } else {
        session.beginDialog('Help');
      }
    },
  ]);

  bot.dialog('/returning', [
    (session) => {
      session.send(`Hello ${session.message.address.user.name}, great to see you again.`);
      const msg = new builder.Message(session)
        .text('What can I do for you today?')
        .suggestedActions(
          builder.SuggestedActions.create(
            session, [
            builder.CardAction.imBack(session, 'opt1', 'op1'),
            builder.CardAction.imBack(session, 'opt2', 'opt2'),
            builder.CardAction.imBack(session, 'op3', 'op3'),
            ],
          ),
        );

      builder.Prompts.text(session, msg);
    },
    (session, results) => {
      if (results.response === 'opt1') {
       /* starts a new dialog */
      } else if (results.response === 'opt2') {
       /* starts a new dialog */
      } else if (results.response === 'opt3') {
       /* starts a new dialog */
      } else {
        session.beginDialog('Help');
      }
    },
  ]);
};

export default botDefault;

I removed some stuff to make it smaller. But essentially it's just a list of all my dialogs that I've separated out into separate files. I'm not doing anything (at least I think I'm not) too fancy.

pourmesomecode avatar Aug 02 '17 23:08 pourmesomecode

@pourmesomecode for reference, what is your deployment method? Are you using Azure Bot Service? App Service? other?

nwhitmont avatar Aug 02 '17 23:08 nwhitmont

@nwhitmont It's hosted on https://dev.botframework.com/bots/ with the url pointing to an ec2 instance that's running my bot.

After doing some digging around and reading I found this thread https://github.com/Microsoft/BotBuilder/issues/1520

I added this line from that thread bot.endConversationAction('goodbye', 'Goodbye :)', { matches: /^goodbye/i }); and after the user saying Goodbye it seemed to work. After awhile of testing with it, it begins to error out again. So my understanding from that is it's related to the dialog stack.

Obviously, this isn't a fix because I don't understand the cause of the issue properly.

I thought session.beginDialog('dialogName'); pops the current dialog out the dialog stack and triggers the new one. Do I need to call session.endDialog(); after I've called beginDialog?

pourmesomecode avatar Aug 03 '17 08:08 pourmesomecode

@pourmesomecode What is your bot handle and App ID as you registered in the Bot Portal at https://dev.botframework.com?

Note: https://dev.botframework.com does not host any bots. Your deployment method is AWS EC2.

nwhitmont avatar Aug 04 '17 01:08 nwhitmont

@nwhitmont I think this issue can be closed.

I'm not 100% sure what the issue is. I have however fixed whatever it was or got a work around for it.

In short, I could console.log my way around my application and could get into dialogs but as soon as I would call session.send('text') it would error out. I figure it was something related to the dialog stack and botbuilder struggling to handle all the dialogs.

I kept the convo persistent data as false bot.set('persistConversationData', false);

And to start dialogs I used beginDialogAction instead. So I'd do a match and add a trigger action so when the user presses a quick reply button it would trigger the beginDialogAction.

bot.beginDialogAction('/buttonTrigger', 'digaloName', { matches: /^test/i });

I'm not sure the issue. I just think it's around the dialog stack therefore switched up my methods.

pourmesomecode avatar Aug 08 '17 14:08 pourmesomecode

thanks for the update @pablocastro

nwhitmont avatar Aug 09 '17 00:08 nwhitmont