Facebook Messenger Send API Client
Send message through Facebook Messenger Send API.
Installation
$ npm install fbm-send
Usage
Use the fbm-webhook
module for handling the Facebook Messenger webhook events.
const FbmSend = ;const fbmWebhook = ; const webhook = ; const fbmSend = accessToken: "Your Page Access Token" version: "4.0"; // Listen to the message event.webhook; webhook;
accessToken
: Your Facebook page access token, default toprocess.env.FB_PAGE_ACCESS_TOKEN
.version
: The Facebook Graph API version, default to4.0
.
You can use the try...catch
block to catch the API error response:
try await fbmSend; catcherror if errorresponse console; // The API error response object.
API and Examples
Table of Contents
- Constructor
- Send Request
- Send Text
- Send Attachment
- Send Image/Video/Audio
- Send Saved Attachment
- Send Sender Action
- Send Mark Seen/Typing On/Typing Off
Constructor
Create a new fbm-send
instance.
accessToken = processenvFB_PAGE_ACCESS_TOKEN version = "4.0";
Parameters
accessToken
(optionalString
): The Facebook page access token, default toprocess.env.FB_PAGE_ACCESS_TOKEN
.version
(optionalString
): The Facebook Graph API version, default to4.0
.
Send Request
Use the request
method to send an HTTP request to the Send API. All other methods are just a wrapper around this method.
const response = await ;
Parameters
recipient
(String
|Object
): The message recipient.formData
(optionalBoolean
): Send the request as amultipart/form-data
(for uploading a local file), default tofalse
.body
(Object
): The request payload to send.
Recipient
The recipient can be a String
: PSID
, phone_number
, or a user_ref
.
It can also be an Object
as defined in the documentation: recipient object.
// Recipient as a string.const recipient = "1234567"; // PSID, phone_number, or user_ref // Equals to recipient as an object.const recipient = id: "1234567"
Messaging Type
As of Graph API version 2.2
, you're required to pass the messaging_type
(String
). There are three supported values for messaging_type
:
RESPONSE
(default value)UPDATE
MESSAGE_TAG
Read more in the messaging type documentation.
Return
It returns a Promise
which when resolved contains a response from the API.
Examples
Send a simple text to the user:
const response = await fbmSend; // Equals to:const response = await fbmSend;
Send a file from a local filesystem:
const fs = ; const myFile = fs; const response = await fbmSend;
Check the Send Attachment feature for more simpler approach.
Send quick replies:
const response = await fbmSend;
Send URL buttons:
const response = await fbmSend;
Send Text
Send a plain text to the user:
const response = await fbmSendtexttext to messagingType: "RESPONSE";
Parameters
text
(String
): The text to send.to
(String
|Object
): The recipient.messagingType
(optionalString
): The messaging type, default toRESPONSE
.
Examples
const response = await fbmSendtext"Hello World!" to: "123456";
Overriding the default messaging type:
const UPDATE from "fbm-send/messaging-types"; const response = await fbmSendtext"Hello World!" to: "123456" messagingType: UPDATE;
Send Attachment
Send an attachment to the user:
const response = await fbmSend;
Parameters
file
(String
): The remote URL of the file or the local file path.to
(String
|Object
): The recipient.messagingType
(optionalString
): The messaging type, default toRESPONSE
.type
(optionalString
): The type of the attachment:file
,image
,video
, oraudio
. Default tofile
.isReusable
(optionalBoolean
): Set totrue
to make the attachment reusable (no need to re-upload it again). Default tofalse
.
Examples
Provide file attachment as a remote URL (must be started with http://
or https://
):
const response = await fbmSend;
Provide file attachment as a local file:
const response = await fbmSend;
Set isReusable
to true
to save the attachment, so it can later be reused without the needs to upload it again.
// The attachment_id can later be usedconst attachment_id = await fbmSend;
Instead of re-uploading the file, the attachment_id
can later be referenced.
Send Image/Video/Audio
There are also wrapper methods to send an attachment with image, video, or audio type:
// Send image.const response = await fbmSend; // Send video.const response = await fbmSend; // Send audio.const response = await fbmSend;
Parameters
file
(String
): The remote URL of the file or the local file path.to
(String
|Object
): The recipient.messagingType
(optionalString
): The messaging type, default toRESPONSE
.isReusable
(optionalBoolean
): Set totrue
to make the attachment reusable (no need to re-upload it again). Default tofalse
.
Examples
const response = await fbmSend; const response = await fbmSend; const response = await fbmSend;
Send Saved Attachment
When sending an attachment, set the isReusable
to true
to save the file for later use. You'll get the attachment_id
from the API response. You can use this attachment_id
to send the same file without the needs to re-upload it again.
const response = await fbmSend;
Parameters
id
(String
): The attachment id.to
(String
|Object
): The recipient.messagingType
(optionalString
): The messaging type, default toRESPONSE
.type
(optionalString
): The type of the attachment:file
,image
,video
, oraudio
. Default tofile
.
Examples
// Send an attachment and get the id for later use.const attachment_id = await fbmSend; // Use the saved attachment file.const response = await fbmSend;
Send Sender Action
Send sender action to the user:
const response = await fbmSendactiontype to;
Parameters
type
(String
): The action type:mark_seen
,typing_on
, ortyping_off
.to
(String
|Object
): The recipient.
Examples
const MARK_SEEN TYPING_ON TYPING_OFF = ; const response = await fbmSendactionMARK_SEEN to: "1234"; const response = await fbmSendactionTYPING_ON to: "1234"; const response = await fbmSendactionTYPING_OFF to: "1234";
Send Mark Seen/Typing On/Typing Off
There are also wrapper methods to send mark seen/typing on/typing off action to the user:
const response = await fbmSend; const response = await fbmSend; const response = await fbmSend;
Parameters
to
(String
|Object
): The recipient.
Examples
// Send mark as seen action.const response = await fbmSend; // Send typing on action.const response = await fbmSend; // Send typing off action.await fbmSend;
Related
- fbm-webhook: Facebook Messenger webhook middleware for Express.
License
Legal
This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by Facebook or any of its affiliates or subsidiaries. This is an independent and unofficial API.