Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Abandon the original plans, from scratch fresh start. A simple Telegram bot client for Google Apps Script.

License

Notifications You must be signed in to change notification settings

ilanlal/basic-telegram-bot-remastered

Repository files navigation

Logo Basic Telegram Bot (REMASTERED)

A simple Telegram bot client for Google Apps Script.

GitHub release

GitHub stars GitHub repo size GitHub last commit GitHub issues clasp MIT License

Crash Course

Copy the contents of the src/lib/TelegramBotProxy.js file into new file named TelegramBotProxy.gs in your Google Apps Script project.

Note: the file extension should be .gs for Google Apps Script. This library provides a simple interface to interact with the Telegram Bot API. all API methods are accessible via the executeApiRequest method.

Then, you can use the following code to send a message using your Telegram bot:

// Replace the placeholders with your chat_id and bot token
const chat_id = '[YOUR_CHAT_ID]';
const token = '[YOUR_BOT_TOKEN]';
// Create a new instance of the TelegramBotProxy class
const proxy = new TelegramBotProxy(token);
// Send a message to the chat
const response = proxy.executeApiRequest('sendMessage', { chat_id, text: 'Hello from Google Apps Script!' });
if (response?.getResponseCode() !== 200) {
 throw new Error(`Failed to execute action: ${response?.getContentText() || 'No response'}`);
}

Advanced Usage

Getting payments

To handle payments using your Telegram bot, you can use one of the following strategies:

1. Sending an Invoice

function sendInvoice() {
 // Replace the placeholders with your chat_id and bot token
 const chat_id = '[YOUR_CHAT_ID]';
 const token = '[YOUR_BOT_TOKEN]';
 const proxy = new TelegramBotProxy(token);
 // Create & send an invoice
 const invoiceResponse = proxy.executeApiRequest('sendInvoice', {
 chat_id,
 title: 'Test Product',
 description: 'This is a test product',
 payload: 'test_payload',
 currency: 'XTR',
 prices: JSON.stringify([
 { label: 'Total', amount: 1000 } // Amount in smallest units (e.g., cents)
 ]),
 photo_url: 'https://www.gstatic.com/webp/gallery/1.jpg',
 photo_width: 240
 });
 if (invoiceResponse?.getResponseCode() !== 200) {
 throw new Error(`Failed to send invoice: ${invoiceResponse?.getContentText() || 'No response'}`);
 }
}

2. Sending a Paid media

To send a paid media (like a photo) after receiving a successful payment, you can use the following code snippet:

Paid media should be sent only after confirming the payment via the pre_checkout_query and successful_payment updates from Telegram.

function sendPaidPhoto(chat_id) {
 // Replace the placeholder with your bot token
 const token = '[YOUR_BOT_TOKEN]';
 const proxy = new TelegramBotProxy(token);
 // Send a paid photo
 const photoResponse = proxy.executeApiRequest('sendPaidMedia', {
 chat_id,
 title: 'Paid Photo',
 description: 'This is a paid photo content.',
 payload: 'paid_photo_payload',
 protect_content: true,
 star_count: 1000,
 media: [
 {
 type: 'photo',
 media: 'https://www.gstatic.com/webp/gallery/1.jpg',
 caption: 'Thank you for your purchase! Here is your paid media content.'
 }
 ]
 });
 if (photoResponse?.getResponseCode() !== 200) {
 throw new Error(`Failed to send paid photo: ${photoResponse?.getContentText() || 'No response'}`);
 }
}

Setting up a Webhook

First, copy the contents of the src/lib/TelegramBotClient.js file into a new file named TelegramBotClient.gs in your Google Apps Script project. Then, you can use the following code snippets to set up a webhook and handle incoming updates:

// Replace the placeholders with your bot token you generated from BotFather
const token = '[YOUR_BOT_TOKEN]';
// Google Apps Script Web App URL after deployment
const webDeploymentUrl = 'https://script.google.com/macros/s/[YOUR_DEPLOYMENT_ID]/exec';
const client = new TelegramBotClient(token);
client.setWebhook(webDeploymentUrl);

Handling Incoming Updates

When Telegram sends updates to your webhook URL, you can handle them using the following code snippet:

// Handle incoming inline updates from Telegram
function doPost(e) {
 const update = JSON.parse(e.postData.contents);
 
 if (update.message) {
 const chat_id = update.message.chat.id;
 const text = update.message.text;
 // Echo the received message
 const token = '[YOUR_BOT_TOKEN]';
 const proxy = new TelegramBotProxy(token);
 const response = proxy.executeApiRequest('sendMessage', { chat_id, text: `You said: ${text}` });
 if (response?.getResponseCode() !== 200) {
 throw new Error(`Failed to execute action: ${response?.getContentText() || 'No response'}`);
 }
 }
}

Roadmap

  • Define "Addon" framework for building Telegram bots with Google Apps Script.
  • Integrate Google Chat API support.
  • Improve error handling and logging mechanisms.
  • Create a comprehensive set of examples and documentation.

Overview

This project is a remastered version of the original Basic Telegram Bot.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

License

This project is licensed under the MIT license.

About

Abandon the original plans, from scratch fresh start. A simple Telegram bot client for Google Apps Script.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /