Before starting, ensure you have a Facebook Page. If you don't have one, create a Facebook Page first.
Ensure you have Node.js installed, then run:
npm install express body-parser fs axios
-
Navigate to Facebook Developers:
- Open your web browser and go to developers.facebook.com.
-
Create a Developer Account (if you don’t have one):
- Log in with your Facebook credentials and follow the prompts to set up a developer account.
- Create an App:
- Click on "My Apps" in the top-right corner.
- Select "Create App".
- Choose "Business" as the app type.
- Fill out the required details such as the app display name and contact email, then click "Create App ID".
- Set Up Messenger:
- In the left sidebar of your app's dashboard, click on "Add Product".
- Locate "Messenger" and click the "Set Up" button.
- Generate a Page Access Token:
- Scroll to the "Access Tokens" section.
- Click on "Add or Remove Pages".
- Follow the prompts to connect your Facebook Page.
- Once connected, click "Generate Token" and copy the token for later use.
- Configure Webhooks:
- Scroll down to the "Webhooks" section in Messenger settings.
- Click "Setup Webhooks".
- Enter the following details:
- Callback URL:
https://your_hosting.site/webhook
- Verify Token:
pagebot-v3
- Callback URL:
- Subscribe to the following fields:
messages
messaging_optins
messaging_postbacks
- Click "Verify and Save".
- Subscribe to Page Events:
- Under "Webhooks", navigate to "Page Subscriptions".
- Select the page you connected earlier.
- Ensure that
messages
,messaging_optins
, andmessaging_postbacks
are selected.
- Copy the Token:
- Go back to "Access Tokens".
- Copy the generated Page Access Token.
- Enter the Token:
- Paste the Page Access Token into
configure.json
.
- Paste the Page Access Token into
- Test Bot Functionality:
- Open your Facebook Page.
- Send a message (e.g., "help") to verify if the bot responds correctly.
- Ensure that the account used for testing has a role in the app.
Note: The bot will only respond to accounts assigned specific roles within the app.
-
Deployment Instructions:
- Ensure your server is set up with Node.js.
- Deploy the app to your server or cloud platform (e.g., Heroku, Render, Vercel).
-
Run Your App:
- Navigate to your project directory and install any additional dependencies:
npm install
- Start your server:
node index.js
- To ensure your server restarts when code changes, consider using
nodemon
(optional):npm install -g nodemon nodemon index.js
- Navigate to your project directory and install any additional dependencies:
-
Navigate to developers.facebook.com:
- Go to developers.facebook.com.
-
Access Your App:
- Log in and navigate to "My Apps".
-
Select "App Roles":
- Find the "App Roles" section in your app's dashboard.
-
Add Roles:
- Click on "Add Role" to start the process.
-
Define Role Details:
- Specify role name and permissions as needed.
-
Assign Roles to Users:
- Assign the role to specific users by providing their name or user ID.
This guide explains how to create a new command for your bot using the structure provided in the sample code. Each section of the command is explained below, including where to define specific logic and utilities.
At the top of the file, import all required modules. For example:
const axios = require("axios");
const { sendMessage } = require('../handles/message');
axios
: Used for making API requests.sendMessage
: A utility function for sending messages.
Define the metadata for the command, including its name, description, role, and author.
module.exports = {
name: "ai", // The command trigger name.
description: "Gpt4o x Gemini AI", // A brief description of what the command does.
role: 1, // Role-based access control (1 = accessible to all).
author: "Kiana", // Author of the command.
Define the execute
function, which contains the core logic of the command. This function should:
- Validate inputs (e.g.,
event.sender.id
,args
). - Process user input or handle attachments (e.g., analyze images or text prompts).
- Call external APIs and handle responses.
Example:
async execute(bot, args, authToken, event) {
if (!event?.sender?.id) {
console.error('Invalid event object: Missing sender ID.');
sendMessage(bot, { text: 'Error: Missing sender ID.' }, authToken);
return;
}
const senderId = event.sender.id;
const userPrompt = args.join(" ");
if (!userPrompt && !event.message.reply_to?.mid) {
return sendMessage(bot, { text: "Please enter your question or reply with an image to analyze." }, authToken);
}
try {
// Core logic here...
} catch (error) {
console.error("Error in command execution:", error);
sendMessage(bot, { text: `Error: ${error.message || "Something went wrong."}` }, authToken);
}
}
Place utility functions below the main logic. These should handle specific tasks like:
- Interacting with APIs.
- Extracting image URLs.
- Sending long messages.
async function handleImageRecognition(apiUrl, prompt, imageUrl, senderId) {
try {
const { data } = await axios.get(apiUrl, {
params: {
q: prompt,
uid: senderId,
imageUrl: imageUrl || ""
}
});
return data;
} catch (error) {
throw new Error("Failed to connect to the Gemini Vision API.");
}
}
async function extractImageUrl(event, authToken) {
try {
if (event.message.reply_to?.mid) {
return await getRepliedImage(event.message.reply_to.mid, authToken);
} else if (event.message?.attachments?.[0]?.type === 'image') {
return event.message.attachments[0].payload.url;
}
} catch (error) {
console.error("Failed to extract image URL:", error);
}
return "";
}
function sendLongMessage(bot, text, authToken) {
const maxMessageLength = 2000;
const delayBetweenMessages = 1000;
if (text.length > maxMessageLength) {
const messages = splitMessageIntoChunks(text, maxMessageLength);
sendMessage(bot, { text: messages[0] }, authToken);
messages.slice(1).forEach((message, index) => {
setTimeout(() => sendMessage(bot, { text: message }, authToken), (index + 1) * delayBetweenMessages);
});
} else {
sendMessage(bot, { text }, authToken);
}
}
function splitMessageIntoChunks(message, chunkSize) {
const regex = new RegExp(`.{1,${chunkSize}}`, 'g');
return message.match(regex);
}
Export the entire module so it can be registered and used by the bot.
};
- Use meaningful names for functions and variables.
- Add error handling for all asynchronous tasks to ensure stability.
- Place comments to describe the purpose of each block of code.
- Test the command thoroughly before deploying it.
Refer to the full example above for detailed implementation.
- This guide was created by Clarence, Kyle, Christel, and Akira.
- Thanks to Deku (https://api.joshweb.click) for their APIs.
- Thanks to Kaiz-Api and Hiroshi for their contributions.
Note: Feel free to modify this file as needed.
Feel free to contact French Clarence Mangigo if you have any concerns: https://www.facebook.com/frenchclarence.mangigo.9