Messaging API client for Messenger
npm i --save messaging-api-messenger
or
yarn add messaging-api-messenger
import { MessengerClient } from 'messaging-api-messenger';
// get accessToken from facebook developers website
const client = MessengerClient.connect(accessToken);
async function() {
await client.sendText(recipientId, text, options);
}
or
client.sendText(recipientId, text, options).then(() => {
// do something
});
All methods return a Promise.
client.getUserProfile('1')
.then(user => {
console.log(user);
// {
// first_name: 'Johnathan',
// last_name: 'Jackson',
// profile_pic: 'https://example.com/pic.png',
// locale: 'en_US',
// timezone: 8,
// gender: 'male',
// }
});
client.sendRawBody({
recipient: {
id: '1',
},
message: {
text: 'Hello!',
},
});
client.send('1', {
text: 'Hello!',
});
client.sendText('1', 'Hello!');
client.sendIssueResolutionText('1', 'Hello!');
client.sendAttachment('1', {
type: 'image',
payload: {
url: 'https://example.com/pic.png',
},
});
client.sendAudio('1', 'https://example.com/audio.mp3');
client.sendImage('1', 'https://example.com/pic.png');
client.sendVideo('1', 'https://example.com/video.mp4');
client.sendFile('1', 'https://example.com/word.docx');
client.sendTemplate('1', {
template_type: 'button',
text: 'title',
buttons: [
{
type: 'postback',
title: 'Start Chatting',
payload: 'USER_DEFINED_PAYLOAD',
},
],
});
client.sendButtonTemplate('1', 'my_title', [
{
type: 'postback',
title: 'Start Chatting',
payload: 'USER_DEFINED_PAYLOAD',
},
]
client.sendGenericTemplate('1', [
{
title: "Welcome to Peter's Hats",
image_url: 'https://petersfancybrownhats.com/company_image.png',
subtitle: "We've got the right hat for everyone.",
default_action: {
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/view?item=103',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
buttons: [
{
type: 'postback',
title: 'Start Chatting',
payload: 'DEVELOPER_DEFINED_PAYLOAD',
},
],
},
]);
client.sendShippingUpdateTemplate('1', [
{
title: "Welcome to Peter's Hats",
image_url: 'https://petersfancybrownhats.com/company_image.png',
subtitle: "We've got the right hat for everyone.",
default_action: {
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/view?item=103',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
buttons: [
{
type: 'postback',
title: 'Start Chatting',
payload: 'DEVELOPER_DEFINED_PAYLOAD',
},
],
},
]);
client.sendReservationUpdateTemplate('1', [
{
title: "Welcome to Peter's Hats",
image_url: 'https://petersfancybrownhats.com/company_image.png',
subtitle: "We've got the right hat for everyone.",
default_action: {
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/view?item=103',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
buttons: [
{
type: 'postback',
title: 'Start Chatting',
payload: 'DEVELOPER_DEFINED_PAYLOAD',
},
],
},
]);
client.sendIssueResolutionTemplate('1', [
{
title: "Welcome to Peter's Hats",
image_url: 'https://petersfancybrownhats.com/company_image.png',
subtitle: "We've got the right hat for everyone.",
default_action: {
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/view?item=103',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
buttons: [
{
type: 'postback',
title: 'Start Chatting',
payload: 'DEVELOPER_DEFINED_PAYLOAD',
},
],
},
]);
client.sendListTemplate('1', [
{
title: 'Classic T-Shirt Collection',
image_url:
'https://peterssendreceiveapp.ngrok.io/img/collection.png',
subtitle: 'See all our colors',
default_action: {
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/shop_collection',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
buttons: [
{
title: 'View',
type: 'web_url',
url: 'https://peterssendreceiveapp.ngrok.io/collection',
messenger_extensions: true,
webview_height_ratio: 'tall',
fallback_url: 'https://peterssendreceiveapp.ngrok.io/',
},
],
},
],
[
{
type: 'postback',
title: 'Start Chatting',
payload: 'USER_DEFINED_PAYLOAD',
},
],
'compact'
);
client.sendQuickReplies('1', { text: 'Pick a color:' }, [
{
content_type: 'text',
title: 'Red',
payload: 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED',
},
]
);
client.sendSenderAction('1', 'typing_on');
client.turnTypingIndicatorsOn('1');
client.turnTypingIndicatorsOff('1');