Sends a text message to a specified user.
message
(string): The message to send.senderID
(string): The ID of the message recipient.
api.sendMessage("Hello", event.sender.id);
or, with a promise:
api.sendMessage("Hello", event.sender.id).then((res) => {
console.log(res).catch((err) => {
console.error(err);
});
});
Activates or deactivates the typing indicator for a user.
isTyping
(boolean): Set totrue
to show the typing indicator, orfalse
to hide it.senderID
(string): The ID of the user for whom the typing indicator is being set.
Enable the typing indicator:
api.sendTypingIndicator(true, event.sender.id);
Disable the typing indicator:
api.sendTypingIndicator(false, event.sender.id);
or, with a promise:
api.sendTypingIndicator(false, event.sender.id).then((res) => {
console.log(res);
}).catch((err) => {
console.error(err);
});
A simple and flexible API for sending buttons (text buttons, URL buttons, postback buttons) to users on Facebook Messenger. This function allows you to send a rich message that includes interactive buttons like Call to Action
buttons.
The api.sendButton
function allows you to send interactive buttons that a user can click, leading to a postback event or opening a URL.
// Sending a single button
const buttons = [
{
type: "web_url",
title: "Visit Website",
url: "https://www.facebook.com/yandeva.me",
},
];
api.sendButton("Here is a button!", buttons, senderID)
.then(response => console.log(response))
.catch(error => console.error(error));
// Sending multiple buttons (e.g., postback buttons)
const postback = [
{
type: "postback",
title: "Click Me!",
payload: "USER_CLICKED_BUTTON",
},
{
type: "web_url",
title: "Visit Website",
url: "https://www.facebook.com/yandeva.me",
},
{
type: "web_url",
title: "Chat Me",
url: "m.me/pekoai",
},
];
api.sendButton("Click a button!", postback, senderID)
.then(response => console.log(response))
.catch(error => console.error(error));
web_url
: Opens a URL when clicked (ideal for external links).postback
: Sends a postback event when clicked (ideal for triggering specific bot actions).
messageText
(string): The text content that accompanies the buttons. This can include any message you want the user to see along with the buttons.buttons
(array): An array of buttons to send, where each button is an object containing:type
(string): The button type. Can be one of:'web_url'
: A button that opens a URL.'postback'
: A button that sends a postback to the bot.
title
(string): The text to display on the button.url
(string, optional): The URL to open when the button is clicked. Required forweb_url
type buttons.payload
(string, optional): The payload to send when the button is clicked. Required forpostback
type buttons.
senderID
(string): The ID of the recipient. If not provided, it will use the sender ID from the event.
The function returns a promise that resolves with the response from the Facebook Graph API, which will contain information about the sent message or an error if something goes wrong.
{
"recipient_id": "123456789",
"message_id": "m_123456789"
}
-
Buttons Layout: You can send a maximum of 3 buttons per message. If you need to send more, consider breaking the message into multiple parts or creating a more complex button template.
-
Button Limitations: Only
web_url
andpostback
are supported as button types. The payload forpostback
buttons should be a string that you can use to trigger specific actions in your bot. -
Access Tokens: Ensure that your
PAGE_ACCESS_TOKEN
is properly set up. This token is required for authenticating requests to the Facebook Graph API.
A simple and flexible API for sending various types of attachments (file, image, audio, video, etc.) via Facebook Messenger using the Graph API. This function supports sending both file uploads and URL-based attachments.
The api.sendAttachment
function is used to send different types of attachments, including files, images, audio, and video, to a recipient via Facebook Messenger.
// Sending a file
api.sendAttachment('file', 'path/to/your/file.pdf', event.sender.id)
.then(response => console.log(response))
.catch(error => console.error(error));
// Sending an image URL
api.sendAttachment('image', 'https://example.com/image.jpg', event.sender.id)
.then(response => console.log(response))
.catch(error => console.error(error));
// Sending an audio URL
api.sendAttachment('audio', 'https://example.com/audio.mp3', event.sender.id)
.then(response => console.log(response))
.catch(error => console.error(error));
// Sending a video URL
api.sendAttachment('video', 'https://example.com/video.mp4', event.sender.id)
.then(response => console.log(response))
.catch(error => console.error(error));
file
: Upload and send a file attachment (e.g., PDFs, documents, etc.).image
: Send an image URL as an attachment.audio
: Send an audio URL as an attachment.video
: Send a video URL as an attachment.
You can extend the list of supported types by adding new types to the supportedTypes
array in the code.
-
attachmentType
(string): The type of the attachment. Can be one of the following:'file'
: For file uploads.'image'
: For image URLs.'audio'
: For audio URLs.'video'
: For video URLs.
-
attachment
(string): The attachment content. This can either be:- A file path (e.g.,
'path/to/file.pdf'
) for file uploads. - A URL (e.g.,
'https://example.com/image.jpg'
) for image, audio, or video URLs.
- A file path (e.g.,
-
senderID
(string): The ID of the recipient. If not provided, it will use the sender ID from the event.
The function returns a promise that resolves with the response from the Facebook Graph API, which will contain information about the sent message or an error if something goes wrong.
{
"recipient_id": "123456789",
"message_id": "m_123456789",
"attachment_id": "attach_987654321"
}
If an error occurs, the function will throw an error with details about what went wrong. For example:
{
"error": {
"message": "(#100) Param message[attachment][type] is not supported. Please check developer docs for details",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "ACRTjqIJmOIwRF0u868-JSM"
}
}
-
File Upload Limitations: When sending a file, the file must be uploaded to Facebook first. The function handles the upload and retrieval of the attachment ID, which is then used to send the message.
-
Supported Facebook Graph API Versions: This function is compatible with
v20.0
of the Facebook Graph API. Make sure your API version is aligned with this. -
Access Tokens: Make sure that your
PAGE_ACCESS_TOKEN
is set up correctly. This token is required to authenticate requests to the Facebook Graph API.
This API function allows you to send reactions to Facebook messages using the Facebook Graph API. It is intended for use with verified business pages only and will not work on normal Facebook pages.
- Send Reactions: Allows you to send reactions like
LIKE
,LOVE
,HAHA
,WOW
,SAD
, andANGRY
to messages on a verified business page. - Error Handling: Provides error handling and logs any issues that occur during the process.
- Verified Business Page: This API can only be used with a verified business page. It will not work on regular Facebook pages.
- PAGE_ACCESS_TOKEN: Ensure that you have a valid page access token with the necessary permissions to interact with the Facebook Graph API.
Use the api.setMessageReaction
function to send a reaction to a message by providing the desired reaction type and message ID.
reaction
(string): The type of reaction you want to send. Supported values are:LIKE
,LOVE
,HAHA
,WOW
,SAD
, andANGRY
.messageId
(string): The ID of the message to which you want to react.
const reaction = 'LIKE'; // or 'LOVE', 'HAHA', etc.
const messageId = '1234567890123456'; // The ID of the message to react
api.setMessageReaction(reaction, messageId)
.then(response => {
console.log('Reaction sent:', response);
})
.catch(error => {
console.error('Error sending reaction:', error);
});
or, simply like this:
api.setMessageReaction('LIKE', '1234567890123456');
If the API call encounters an error, it will log the error message and return null
.
Error sending reaction: Unable to send message: { error details }
- This API function only works for verified business pages.
Marks a message as seen or unseen for a user.
isSeen
(boolean): Set totrue
to mark the message as seen, orfalse
to mark it as unseen.senderID
(string): The ID of the user for whom the message is being marked.
To mark a message as seen:
api.markAsSeen(true, event.sender.id);
To mark a message as unseen:
api.markAsSeen(false, event.sender.id);
or, with a promise:
api.markAsSeen(false, event.sender.id).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
});
The api.graph
method allows for flexible message formatting, enabling you to send image attachments, buttons, and generic templates.
Sends a message with buttons that perform various actions (like opening URLs or triggering postback events).
api.graph({
recipient: { id: event.sender.id },
message: {
attachment: {
type: 'template',
payload: {
template_type: 'button',
text: "What would you like to do?",
buttons: [
{
type: 'web_url',
url: "https://www.facebook.com/yandeva.me",
title: "Visit Profile"
},
{
type: 'postback',
title: "Say Hello",
payload: "HELLO_PAYLOAD"
}
]
}
}
}
});
Sends an image attachment to the user.
api.graph({
recipient: { id: event.sender.id },
message: {
attachment: {
type: 'image',
payload: {
url: "https://example.com/image.jpg",
is_reusable: true // Set to true to reuse the image
}
}
}
});
Sends a carousel-style message with multiple items, each with an image, title, subtitle, and buttons.
api.graph({
recipient: { id: event.sender.id },
message: {
attachment: {
type: 'template',
payload: {
template_type: 'generic',
elements: [
{
title: "First Item",
image_url: "https://example.com/image1.jpg",
subtitle: "This is the first item",
buttons: [
{
type: 'web_url',
url: "https://example.com",
title: "View Item"
},
{
type: 'postback',
title: "More Info",
payload: "MORE_INFO_ITEM_1"
}
]
},
{
title: "Second Item",
image_url: "https://example.com/image2.jpg",
subtitle: "This is the second item",
buttons: [
{
type: 'web_url',
url: "https://example.com",
title: "View Item"
},
{
type: 'postback',
title: "More Info",
payload: "MORE_INFO_ITEM_2"
}
]
}
]
}
}
}
});
Sends a message with quick reply buttons that disappear after being tapped.
api.graph({
recipient: { id: event.sender.id },
message: {
text: "Choose an option:",
quick_replies: [
{
content_type: "text",
title: "Option 1",
payload: "OPTION_1"
},
{
content_type: "text",
title: "Option 2",
payload: "OPTION_2"
}
]
}
});
Each of these examples demonstrates a different format of messaging to engage users with images, buttons, or dynamic templates. Adjust the templates as needed to fit your application!
Facebook Messenger allows you to set up a "Get Started" button for new users. When clicked, it sends a payload you can use to initiate a welcome flow.
First, set up the "Get Started" button in your Facebook Page settings or by calling the Facebook API to configure it programmatically.
api.graph({
get_started: { payload: "GET_STARTED_PAYLOAD" }
});
When a user clicks the "Get Started" button, a payload is sent. You can use it to send an introductory message or guide the user through initial setup steps.
function handlePayload(payload, senderID) {
if (payload === "GET_STARTED_PAYLOAD") {
api.sendMessage("Welcome! I'm here to help you.", senderID);
api.sendMessage("Type 'help' to see what I can do.", senderID);
}
}
// Example usage
if (event.postback && event.postback.payload) {
handlePayload(event.postback.payload, event.sender.id);
}
Here's a sample README.md
that outlines the differences between Commands and Events with example code and property explanations.
In this framework, Commands and Events serve different purposes, each having unique properties and behaviors that cater to distinct types of interactions. This document explains the differences and provides examples to help understand how to set up each.
Commands are user-triggered actions that require specific keywords or prefixes to be recognized. They are typically used for straightforward, user-initiated requests, like !greet
or /help
, and need to match the configured command name in order to activate.
Commands are structured with specific properties to handle how they respond to user inputs:
- name: The name of the command that triggers the execution.
- usePrefix: A boolean that defines whether the command requires a prefix (e.g.,
!
or/
) to be recognized. - bodyIndex: Defines the matching behavior for the command name (e.g., starts with, exact match).
- adminOnly: Restricts command usage to administrators if set to
true
.
module.exports.config = {
name: "greet",
author: "YourName",
version: "1.0",
description: "Sends a greeting message.",
adminOnly: false,
usePrefix: true,
cooldown: 5
};
module.exports.run = function ({ event, args }) {
api.sendMessage(`Hello, ${args.join(" ")}!`, event.sender.id);
};
- Triggering: Commands trigger based on matching text (like
!greet
) with thename
property. TheusePrefix
property determines if the command requires a prefix (e.g.,!greet
vs. justgreet
).
Commands are ideal for specific, user-directed actions where structured responses are required, such as:
- Bot Features:
/help
to list commands or/profile
to show user details. - Quick Interactions:
/greet
to send a greeting message or/status
to check bot status. - Note: These are just examples.
Events are general actions that the bot listens for, often without requiring specific prefixes or command names. They are more versatile and are used to handle system-wide interactions, background processes, and message handling that isn’t dependent on user commands.
Events have configurations that control when they activate:
- name: Identifies the event (informational, not used for matching).
- selfListen: Determines if the event should handle messages sent by the bot itself.
- description: Describes the event’s purpose.
module.exports.config = {
name: "Message Logger",
author: "YourName",
version: "1.0",
description: "Logs every message received.",
selfListen: false, // Skip handling messages sent by the bot
};
module.exports.run = function ({ event, args }) {
if (event.type === 'message') {
console.log(`Received message: ${event.message.text}`);
}
};
- Triggering: Events automatically listen to certain actions or message types, such as new messages, reactions, or postbacks, and don’t require specific text or prefixes to activate.
- Self-Listening Control: The
selfListen
property determines if events should handle messages that are bot echoes.- If
selfListen
isfalse
and the message is an echo (event.message.is_echo
), the event won’t execute.
- If
- Flexible Context: Events are designed to handle broader contexts, often listening for any relevant actions in the system rather than specific keywords.
Events are suited for background tasks, global listeners, and anything that doesn’t require specific user input:
- Message Logging: Log every incoming message.
- Background Operations: Automatically handle user reactions, mark messages as read, or handle postbacks.
- System Monitoring: Track interactions that happen passively without user intervention.
Aspect | Commands | Events |
---|---|---|
Trigger | User-initiated, needs specific text | Passively listens for actions or messages |
Prefix | Requires a prefix if usePrefix is true |
Does not need a prefix |
Use Case | Feature-triggered actions, user commands | Background tasks, system actions, global listeners |
Config Properties | name , author , version , category , description , adminOnly , usePrefix , cooldown |
name , author , version , description selfListen , |
By keeping Commands and Events distinct, the framework allows targeted control over user-triggered interactions and system-wide listeners.
Below are the available event types that are triggered during interactions with the bot:
Event Type | Description |
---|---|
message |
A simple message sent by a sender. |
message_reply |
Triggered when a sender replies to a message. |
mark_as_seen |
Logs when a sender marks a message as seen. |
attachments |
Triggered when an attachment (image, audio, video, or file) is sent. |
postback |
Triggered when a button is clicked. |
quick_reply |
Triggered when a quick reply button is clicked. |
message_reaction |
Triggered when a sender reacts to a message. |
This gives the user context on what the event types are and how they are triggered.
Choose from one of the following terminal themes:
Theme Options | ||
---|---|---|
Fiery | Sunlight | Ghost |
Hacker | Retro | Purple |
Aqua | Teen | Rainbow |
Blue | Summer | Orange |
Pink | Flower | Red |
You can configure the terminal theme in the config.json
file, along with the ADMIN and TITLE displayed in the terminal.
{
"THEME_SETUP": {
"THEME": "Fiery",
"ADMIN": "Your Name",
"TITLE": "PAGEBOT"
}
}
Simply replace "Fiery"
, "Your Name"
, and "PAGEBOT"
with your preferred theme, name, and title to personalize your terminal experience.
If you encounter issues or need support, feel free to reach out to the author:
- Yan Maglinte (FB: @YanMaglinte)