Skip to content

Message Manager

Abdi Urgessa edited this page Feb 18, 2024 · 5 revisions

The MessageManager class is designed to facilitate a wide range of message-related operations for a Telegram bot, specifically tailored for use within Google Apps Script runtime environments. This class offers a comprehensive suite of methods for sending, editing, forwarding, and managing messages in various formats, including text, media, and interactive content.

Usage Examples

Below are additional examples illustrating how to use the MessageManager for various tasks. These examples build upon the simple message reply example provided:

bot.on("message", (ctx) => {
   ctx.reply("Hello, there! How can I assist you today?");
});

Replying with HTML and Markdown:

// Replying with HTML
bot.on("message", (ctx) => {
   ctx.replyWithHtml("<b>Hello, bold world!</b>");
});

// Replying with Markdown
bot.on("message", (ctx) => {
   ctx.replyWithMarkdown("*Hello, markdown world!*");
});

Sending Photos, Documents, and More:

// Sending a photo
bot.on("message", (ctx) => {
   const photoUrl = 'https://example.com/photo.jpg';
   ctx.replyWithPhoto(photoUrl, {caption: "Look at this beautiful scenery!"});
});

// Sending a document
bot.on("message", (ctx) => {
   const documentUrl = 'https://example.com/document.pdf';
   ctx.sendDocument({chat_id:ctx.chat().id, document: documentUrl, caption: "Here is a document for you."});
});

// Sending a location
bot.on("message", (ctx) => {
   ctx.sendLocation({chat_id:ctx.chat().id, latitude: 48.8584, longitude: 2.2945});
});

Editing Messages:

// Editing a previously sent message's text
bot.on("command", (ctx) => {
   const messageId = ctx.messageId();
   const chatId = ctx.chat().id;
   ctx.editMessageText({chat_id: chatId, message_id: messageId, text: "Updated message text"});
});

Forwarding and Copying Messages:

// Forwarding a message
bot.on("message", (ctx) => {
   const fromChatId = ctx.chat().id;
   const messageId = ctx.messageId;
   ctx.forwardMessage({chat_id: "<TARGET_CHAT_ID>", from_chat_id: fromChatId, message_id: messageId});
});

// Copying a message
bot.on("message", (ctx) => {
   const fromChatId = ctx.chat().id;
   const messageId = ctx.messageId;
   ctx.copyMessage({chat_id: "<TARGET_CHAT_ID>", from_chat_id: fromChatId, message_id: messageId});
});

Managing Chat Actions and Polls:

// Sending a chat action (e.g., typing)
bot.on("message", (ctx) => {
   ctx.sendChatAction({chat_id: ctx.chat().id, action: "typing"});
});

// Sending a poll
bot.on("message", (ctx) => {
   ctx.sendPoll({
      chat_id: ctx.chat().id, 
      question: "What's your favorite color?", 
      options: ["Red", "Blue", "Green"]
   });
});
Clone this wiki locally