botmasterai / botmaster-messenger

The Facebook Messenger Botmaster integration
MIT License
15 stars 9 forks source link

Issue when I use the multi-page bot example #13

Closed mronceray closed 6 years ago

mronceray commented 6 years ago

Hi 👋 I want to thank @jdwuarin who worked on on the support of several Facebook pages. I did some tests using two pages.

I had this error at first

/Users/maxime/BotMasterTest/app.js:19
    fbAppSecret: '***',
    ^^^^^^^^^^^
SyntaxError: Unexpected identifier
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

The error was no longer displayed by placing "fbAppSecret" above "pages" like that

const messengerSettings = {
  credentials: {
    verifyToken: 'TOKEN',
    fbAppSecret: '***',
    pages: {
      'pageID1': { pageToken: '***' },
      'pageID2': { pageToken: '***' },
    }
  },
  webhookEndpoint: 'webhook',
};

But I still have an error that I do not understand how to debug it

I have this error in my terminal when I send a message to my facebook page (node:73321) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: Uncaught, unspecified "error" event. ([object Object])

I'm not sure if I'm using the example correctly, do you know where the error comes from?

Thank you for your reply

jdwuarin commented 6 years ago

Can you post all the code you are running so I see where the error could come from?

Also, the Readme was a bit off. Thanks for pointing that out. Updating it now

mronceray commented 6 years ago
const Botmaster = require('botmaster');
const MessengerBot = require('botmaster-messenger');
const botmaster = new Botmaster();

const messengerSettings = {
    credentials: {
      verifyToken: 'TOKEN',
      fbAppSecret: 'fdbd2ec***',
      pages: {
        '1572***': {
          pageToken: 'EAAN***'
        },
      '10227***': {
        pageToken: 'EAAN***'
      },
      '177399***': {
        pageToken: 'EAAN***'
      }
    }
  },
  webhookEndpoint: 'webhook',
};

const messengerBot = new MessengerBot(messengerSettings);

botmaster.addBot(messengerBot);

botmaster.use({
  type: 'incoming',
  name: 'my-middleware',
  controller: (bot, update) => {
    const messageToSend = bot.createOutgoingMessageFor(userId);
    messageToSend.addText('Hello World');
    messageToSend.sender = {
      id: '1773***',
    };
    return bot.sendMessage(messageToSend);
  }
});
jdwuarin commented 6 years ago

Right. Another issue with the Readme; Your middleware should read:

botmaster.use({
  type: 'incoming',
  name: 'my-middleware',
  controller: (bot, update) => {
    const messageToSend = bot.createOutgoingMessageFor(update.sender.id); // or another userId you know about
    messageToSend.addText('Hello World');
    messageToSend.sender = {
      id: '1773***',
    };
    return bot.sendMessage(messageToSend);
  }
});
mronceray commented 6 years ago

Perfect, it works! Thank you

jdwuarin commented 6 years ago

Just to let you know that I just deployed a new version that will let you use multi-pages like before. I.e. no need to specify the pageId (only works within middleware) - as the pageId defaults to whichever page received an update. This means, all the reply, send* functions will now work also for multi-page bots. Checkout the updated docs.