Search code examples
node.jstypescriptbotframeworkfacebook-messenger-botngrok

BotFrameworkAdapter missing activity type - Debug Messenger locally


I've been working in my bot locally, using bot emulator. All seems to work fine. Now is time to integrate with Messenger and I'm trying to run it locally too.

From Messenger to my local bot through ngrok.

I'm basically trying to follow this link here.

When I send a message from my messenger it seems to take a while to reach my endpoint (and breakpoint) but when it does I'm getting the following error:

/api/messages - POST
index.ts:72
BotFrameworkAdapter.processActivity(): 400 ERROR - Error: BotFrameworkAdapter.parseRequest(): missing activity type.

 UnhandledPromiseRejectionWarning: Error: Error: BotFrameworkAdapter.parseRequest(): missing activity type.
    at BotFrameworkAdapter.processActivity (.../node_modules/botbuilder/src/botFrameworkAdapter.ts:608:19)

On my ngrok console I can see a 400 request and sometimes 502 the same on facebook APP log errors.

Here is the point where this is going:

// using restify
server.post('/api/messages', (req, res) => {
  console.log('/api/messages - POST');
  adapter.processActivity(req, res, async turnContext => {
    await bot.run(turnContext);
  });
});

Basically the bot does not work at all but I did a test using botkit adapter to connect to facebook messenger and it seems to almost work in most cases apart from special messages like Prompt Choice, Carousel and HeroCards. Basically it only works with simple text.

BotFrameworkAdapter is supposed to translate fine. Any idea on what's going on? Not sure if I'm missing something.


Solution

  • Ok. The problem was the callback link on Facebook's App.

    Since I have a verification token on my code, as you can see below, I was pointing the facebook callback URL to my ngrok link and the verification was working fine. But it looks like to debug on azure configurations you must use the callback link that azure provides to you. Something like "https://facebook.botframework.com/api..." that you can find under channels / facebook.

    Once I set the facebook APP to use azure callback link everything is working fine.

    server.post('/api/messages', (req, res) => {
      adapter.processActivity(req, res, async turnContext => {
        await bot.run(turnContext);
      });
    });
    
    server.get('/api/messages', (req, res) => {
      const mode = req.query['hub.mode'];
      const token = req.query['hub.verify_token'];
      const challenge = req.query['hub.challenge'];
    
      if (mode && token) {
        if (mode === 'subscribe' && token === VERIFY_TOKEN) {
          // Responds with the challenge token from the request
          console.log('WEBHOOK_VERIFIED', challenge);
          res.status(200);
          res.end(challenge);
        } else {
          res.send(403);
        }
      }
    });