-
Notifications
You must be signed in to change notification settings - Fork 18
5. Advanced Usage
Separating the commands into each file known as a plugin is a good practice to code the bot and it also helps you to organize the folder, and it gives better flexibility too.
Create the plugins directory here, ./flaregram/src/plugins/
β οΈ Be SURE to renamewrangler_sample.tomltowrangler.toml!!!
index.js would be the main file for the bot and it contains calls all the plugins inside it. Create it in the directory ./flaregram/src/index.js, that is the same level as the plugins folder.
Lets make a file start_cmd.js in our newly made plugins folder!
And we'll write the following code in it:
import { bot } from '../flaregram/bot';
export async function startCommand(body) {
// Fetching user details with message body
const user_id = body.message.from.id
const firstname = body.message.chat.first_name;
const chatId = body.message.chat.id;
// Defining the message parameters, for the 'sendMessage' function
messageparams = {
chat_id: chatId, // Chat ID to send the message to
text: `Hello [${firstname}](tg://user?id=${user_id}),\nWelcome to Sample Bot!`, // Text to send
parse_mode: "markdown"
}
// Sending the message with the 'sendMessage' function
await bot.message.sendMessage(messageparams);
}The above code defines an asynchronous function named startCommand that is designed to handle the start command for a bot. Here's a breakdown of what each part of the function does:
-
Importing framework: Imports the
botclass from the flaregram API framework. -
Function Declaration: The function
startCommandis declared as asynchronous (async), which means it can perform asynchronous operations, like network requests, and use await to wait for these operations to complete. It takes a single parameter body, which is expected to contain the message details received from the bot platform. -
Fetching User Details: Inside the function, it extracts the user's ID, first name, and chat ID from the body parameter. These are obtained from the nested properties of body, indicating that body is structured in a way that contains details about the message and the sender. This information is used to personalize the message sent back to the user and to identify where to send the message.
-
user_idis the unique identifier of the user who sent the message. -
firstnameis the first name of the user, used to personalize the greeting. -
chatIdis the identifier of the chat where the message was sent, which could be a direct message to the bot or a group chat.
-
-
Defining Message Parameters: A variable messageparams (which should ideally be declared with let or const to avoid it being global) is defined to hold the parameters for the message to be sent. These parameters include:
-
chat_id: The ID of the chat where the message should be sent. -
text: The message text, which includes a personalized greeting using Markdown formatting to make the user's first name clickable (linking to their Telegram profile). -
parse_mode: Set to "markdown" to enable Markdown formatting in the message text.
-
-
Sending the Message: Finally, the function uses await to call
bot.message.sendMessage(messageparams), which sends the message to the specified chat ID with the defined parameters. The use of await implies thatsendMessagereturns a Promise, and the function waits for this operation to complete before proceeding. This could be a network request to the Telegram API to send the message.
After making the start_cmd plugin successfully! It's time to connect the plugin with index.js to make it fully functional.
import { startCommand } from './plugins/startcmd';
import { router } from './flaregram/utils/router';
// Adding Event Listener, to handle the incoming requests
addEventListener('fetch', (event) => {
event.respondWith(router.handle(event.request));
});
// Handling the updates by telegram
export async function updateHandler(obj) {
if (obj.message) {
// if incoming message is '/start' command
if (obj.message.text == '/start'){
// Call the startCommand function
await startCommand(obj);
};
};
};Here's a breakdown of its functionality:
-
Imports:
-
startCommand: A function imported from./plugins/startcmd.js. This function is likely responsible for handling the/startcommand sent by a user to the Telegram bot. -
router: An object imported from./flaregram/utils/router.js. This object is probably used to route different types of HTTP requests to their respective handlers.
-
-
Event Listener: The code adds an event listener for the fetch event, which is triggered whenever the worker receives an HTTP request. The callback function passed to this event listener takes an event object as its argument and calls
event.respondWith(). This method is used to respond to the incoming request with the result ofrouter.handle(event.request), which suggests that the router object is used to determine how to handle different requests based on their URL, method, or other properties. -
Telegram Update Handler: The updateHandler function is an asynchronous function exported by this module. It's designed to handle updates from Telegram, which are passed to it as the obj parameter. Inside this function, there's a check to see if the obj contains a message object. If it does, the function further checks if the text of the message is
/start. This indicates that the bot is looking for messages that are commands from users. If the incoming message is the/startcommand, thestartCommandfunction is called with the obj as its argument.
Something Brewing up come back later... π
Continue reading...