Skip to content

Latest commit

 

History

History
 
 

messaging-api-messenger

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

messaging-api-messenger

Messaging API client for Messenger

Table of Contents

Installation

npm i --save messaging-api-messenger

or

yarn add messaging-api-messenger

Usage

Initialize

import { MessengerClient } from 'messaging-api-messenger';

// get accessToken from facebook developers website
const client = MessengerClient.connect(accessToken);

Call API

async function() {
  await client.sendText(recipientId, text, options);
}

or

client.sendText(recipientId, text, options).then(() => {
  // do something
});

API Reference

All methods return a Promise.

User

getUserProfile(userId)

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',
    // }
  });

Send API

Official docs
Content types

sendRawBody(body)

client.sendRawBody({
  recipient: {
    id: '1',
  },
  message: {
    text: 'Hello!',
  },
});

send(userId, message)

client.send('1', {
  text: 'Hello!',
});

sendText(userId, text [, options])

client.sendText('1', 'Hello!');

sendIssueResolutionText(userId, text)

client.sendIssueResolutionText('1', 'Hello!');

sendAttachment(userId, attachment)

client.sendAttachment('1', {
  type: 'image',
  payload: {
    url: 'https://example.com/pic.png',
  },
});

sendAudio(userId, url)

client.sendAudio('1', 'https://example.com/audio.mp3');

sendImage(userId, url)

client.sendImage('1', 'https://example.com/pic.png');

sendVideo(userId, url)

client.sendVideo('1', 'https://example.com/video.mp4');

sendFile(userId, url)

client.sendFile('1', 'https://example.com/word.docx');

sendTemplate(userId, template)

client.sendTemplate('1', {
  template_type: 'button',
  text: 'title',
  buttons: [
    {
      type: 'postback',
      title: 'Start Chatting',
      payload: 'USER_DEFINED_PAYLOAD',
    },
  ],
});

Official docs

sendButtonTemplate(userId, title, buttons)

client.sendButtonTemplate('1', 'my_title', [
  {
    type: 'postback',
    title: 'Start Chatting',
    payload: 'USER_DEFINED_PAYLOAD',
  },
]

Official docs

sendGenericTemplate(userId, elements)

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',
      },
    ],
  },
]);

Official docs

sendShippingUpdateTemplate(userId, elements)

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',
      },
    ],
  },
]);

sendReservationUpdateTemplate(userId, elements)

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',
      },
    ],
  },
]);

sendIssueResolutionTemplate(userId, elements)

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',
      },
    ],
  },
]);

sendListTemplate(userId, items, topElementStyle)

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'
);

Official docs

sendReceiptTemplate

Official docs

sendAirlineBoardingPassTemplate

Official docs

sendAirlineCheckinTemplate

Official docs

sendAirlineItineraryTemplate

Official docs

sendAirlineFlightUpdateTemplate

Official docs

sendQuickReplies(userId, message, items)

client.sendQuickReplies('1', { text: 'Pick a color:' }, [
    {
      content_type: 'text',
      title: 'Red',
      payload: 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED',
    },
  ]
);

Official docs

sendSenderAction

client.sendSenderAction('1', 'typing_on');

Official docs

turnTypingIndicatorsOn(userId)

client.turnTypingIndicatorsOn('1');

turnTypingIndicatorsOff(userId)

client.turnTypingIndicatorsOff('1');

Upload API

Official docs

uploadAttachment

uploadAudio

uploadImage

uploadVideo

uploadFile

Messenger Profile

Official docs

getMessengerProfile

setMessengerProfile

deleteMessengerProfile

Get Started Button

Official docs

getGetStartedButton

setGetStartedButton

deleteGetStartedButton

Persistent Menu

Official docs

getPersistentMenu

setPersistentMenu

deletePersistentMenu

Greeting Text

Officail docs

getGreetingText

setGreetingText

deleteGreetingText

Domain Whitelist

Official docs

getDomainWhitelist

setDomainWhitelist

deleteDomainWhitelist

Account Linking URL

Official docs

getAccountLinkingURL

setAccountLinkingURL

deleteAccountLinkingURL

Payment Settings

Official docs

getPaymentSettings

setPaymentPrivacyPolicyURL

setPaymentPublicKey

setPaymentTestUsers

deletePaymentSettings

Target Audience

Official docs

getTargetAudience

setTargetAudience

deleteTargetAudience