This is the FB messenger integration for botmaster. It allows you to use your botmaster bot on FB Messenger
Botmaster is a lightweight chatbot framework. Its purpose is to integrate your existing chatbot into a variety of messaging channels - currently Facebook Messenger, Twitter DM and Telegram.
Find the whole documentation for the Botmaster framework on: http://botmasterai.com/documentation/latest
yarn add botmaster-messenger
or
npm install --save botmaster-messenger
If you don't already have these, follow the steps 1-4 on the Facebook Messenger guide: https://developers.facebook.com/docs/messenger-platform/guides/quick-start
In step 2, where you setup your webhook, no need to code anything. Just specify the webhook, enter any secure string you want as a verify token(verifyToken
) and copy that value in the settings object. Also, click on whichever message [those are "update"s using botmaster semantics] type you want to receive from Messenger (message_deliveries
, messages
, message_postbacks
etc...).
To find your Facebook App Secret (fbAppSecret
), navigate to your apps dashboard and under App Secret
click show, enter your password if prompted and then there it is.
Example code using a single page for a bot. When using a single page, botmaster can be used as expected.
const Botmaster = require('botmaster');
const MessengerBot = require('botmaster-messenger');
const botmaster = new Botmaster();
const messengerSettings = {
credentials: {
verifyToken: 'YOUR verifyToken',
pageToken: 'YOUR pageToken',
fbAppSecret: 'YOUR fbAppSecret',
},
webhookEndpoint: 'webhook1234',
};
const messengerBot = new MessengerBot(messengerSettings);
botmaster.addBot(messengerBot);
botmaster.use({
type: 'incoming',
name: 'my-middleware',
controller: (bot, update) => {
return bot.reply(update, 'Hello world!');
}
});
multi-page bot example
const Botmaster = require('botmaster');
const MessengerBot = require('botmaster-messenger');
const botmaster = new Botmaster();
const messengerSettings = {
credentials: {
verifyToken: 'YOUR_VERIFY_TOKEN',
fbAppSecret: 'YOUR_FB_APP_SECRET',
pages: {
'YOUR_PAGE_ID_1': {
pageToken: 'YOUR_PAGE_TOKEN_1',
},
'YOUR_PAGE_ID_2': {
pageToken: 'YOUR_PAGE_TOKEN_2',
},
},
},
webhookEndpoint: 'webhook1234',
};
const messengerBot = new MessengerBot(messengerSettings);
botmaster.addBot(messengerBot);
botmaster.use({
type: 'incoming',
name: 'my-middleware',
controller: (bot, update) => {
// 1) if you simply want to respond as the page that received the update.
bot.reply(update, 'Hello World');
// 2) or if you want to specify another page to response as, you can always set
// the messageToSend.sender.id param as some other pageId as such.
const messageToSend = bot.createOutgoingMessageFor(update.sender.id); // or any other page-scoped userId you know is using your page
messageToSend.addText('Hello World');
messageToSend.sender = {
id: 'SOME_PAGE_ID', // update.sender.id would be this page. But you can set it to something else.
};
return bot.sendMessage(messageToSend);
}
});
If you are not too sure how webhooks work and/or how to get them to run locally, go to webhooks to read some more.
Extends BaseBot
The class to use if you want to add support for FB Messenger in your Botmaster project.
Parameters
settings
Constructor to the MessengerBot class
Parameters
settings
object MessengerBot take a settings
object as first param.Examples
// single page bot
const messengerBot = new MessengerBot({
credentials: {
verifyToken: 'YOUR verifyToken',
pageToken: 'YOUR pageToken',
fbAppSecret: 'YOUR fbAppSecret',
},
webhookEndpoint: 'someEndpoint'
})
// multi-page bot
const messengerBot = new MessengerBot({
credentials: {
verifyToken: 'YOUR_VERIFY_TOKEN',
fbAppSecret: 'YOUR_FB_APP_SECRET',
pages: {
'YOUR_PAGE_ID_1': {
pageToken: 'YOUR_PAGE_TOKEN_1',
},
'YOUR_PAGE_ID_2': {
pageToken: 'YOUR_PAGE_TOKEN_2',
},
etc...
},
},
webhookEndpoint: 'someEndpoint'
})
Adds get start button to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/get-started-button
Parameters
getStartedButtonPayload
string The payload of the postback
you will get when a user clicks on the get started button.resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesgets get started button payload from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/get-started-button
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves get started button from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/get-started-button
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesAdds account Linking to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu
Parameters
persistentMenu
string persistent menu to use for your messenger
botresolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget persistent menu from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves persistent menu from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesAdds greeting text to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
Parameters
greetingObject
string greeting objects. Can be localized.resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget greeting text from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves greeting text from bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesAdds white listed domains to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/domain-whitelisting
Parameters
domainNameLists
string List of domains to whitelist.resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget whitelisted domains from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves whitelisted domains from bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
Parameters
domainNameLists
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesAdds account Linking url to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/account-linking-url
Parameters
accountLinkingURL
string Authentication callback URL.
Must use https protocol.resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget account linking url from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/account-linking-url
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves account Linking to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/account-linking-url
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
Adds target audience url to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/target-audience
Parameters
targetAudience
string resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget target audience url from your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/target-audience
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesremoves target audience to your bot. Read more here: https://developers.facebook.com/docs/messenger-platform/messenger-profile/target-audience
Parameters
resolveWithFullResponse
boolean? specify wether request should
resolve with full response or not. By default, this is falsepageId
string? specify the page you want to set the get started button
for. This iw valid only if you are using botmaster-messenger with multiple pagesget the info for a certain user from a certain page
Parameters
userId
string id of the user whose information is requestedpageId
string specify the page you want to get the user info from.
Different pages may have different rights.In order to contribute, you will need to make sure the tests run on your local machine. To do so, follow these steps:
./tests/_config.js
file that looks like this:'use strict';
const config = {
messengerCredentials: () => ({
verifyToken: 'YOUR_VERIFY_TOKEN',
pageToken: 'YOUR_PAGE_TOKEN',
fbAppSecret: 'YOUR_FB_APP_SECRET',
}),
messengerMultiPageCredentials: () => ({
verifyToken: 'YOUR_VERIFY_TOKEN',
fbAppSecret: 'YOUR_FB_APP_SECRET',
pages: {
'YOUR_PAGE_ID_1': {
pageToken: 'YOUR_PAGE_TOKEN_1',
},
'YOUR_PAGE_ID_2': {
pageToken: 'YOUR_PAGE_TOKEN_2',
},
},
}),
messengerUserId: () => 'YOUR_USER_ID_FOR_THIS_PAGE', // who to send messages to in tests (most probably one of your accounts)
messengerBotId: () => 'YOUR_BOT_ID', // the id of the bot (as sent in message updates). I.E. your page id
};
module.exports = config;
This file is gitignored so won't be committed.
yarn test
This library is licensed under the MIT license