This repository contains scripts for creating a serverless Telegram bot using Google Apps Script. The bot responds to the /start
command and can be configured to restrict access to certain users.
The Maker Alif
- Responds to the
/start
command. - Allows only authorized users to interact with the bot.
- Configurable via Google Apps Script.
- A Telegram account to create a bot.
- Access to Google Apps Script via your Google account.
-
Open Telegram and search for BotFather.
-
Start a chat with BotFather by clicking the Start button or sending
/start
. -
Create a new bot by sending the command
/newbot
. -
Follow the instructions provided by BotFather:
- Choose a name for your bot (this can be anything).
- Choose a username for your bot. The username must be unique and must end with
bot
(e.g.,myawesomebot
).
-
After successfully creating the bot, BotFather will send you a message with the bot token:
Save this bot token as it will be needed for making API requests.
- Open the following URL in your web browser, replacing
YOUR_BOT_TOKEN
with the token you got from BotFather:https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates
- Send a message to your bot from your personal Telegram account or a group where the bot is added.
- Refresh the URL, and you will see a JSON response. Find the section that looks like this:
"chat": { "id": 123456789, "first_name": "YourName", "username": "yourusername" }
- The value of
id
is your chat ID.
-
Go to Google Apps Script and create a new project.
-
Replace the default code in the script editor with the contents of
kode.gas
:// CONFIG var BOT_TOKEN = "YOUR_BOT_TOKEN"; // Ganti dengan TOKEN BOT Anda var USERS = [YOUR_CHAT_ID]; // Ganti dengan CHAT ID yang diizinkan, bisa lebih dari 1 function doGet(e) { return HtmlService.createHtmlOutput('<h1>OK</h1>'); } function doPost(e) { if (e.postData.type == "application/json") { let update = JSON.parse(e.postData.contents); if (update) { handleCommands(update); return true; } } } function handleCommands(update) { let chatId = update.message.chat.id; let first_name = update.message.chat.first_name; let text = update.message.text || ''; if (USERS.includes(chatId)) { if (text.startsWith("/start")) { sendMessage({ chat_id: chatId, text: `🙋🏽 Halo, ${first_name}!\n\n` + `buatan Alif.\n\n` + `Selamat datang di Bot Anda! Silakan gunakan perintah yang tersedia untuk melanjutkan.` }); } else { sendMessage({ chat_id: chatId, text: "❓ Perintah tidak dikenal. Silakan gunakan /start untuk melihat perintah yang tersedia." }); } } else { sendMessage({ chat_id: chatId, text: "🚫 Anda tidak memiliki akses untuk menggunakan bot ini." }); } } function sendMessage(postdata) { var options = { 'method': 'post', 'contentType': 'application/json', 'payload': JSON.stringify(postdata), 'muteHttpExceptions': true }; UrlFetchApp.fetch('https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage', options); }
-
Create another file in the same project and add the contents of
webhook.gs
:// CONFIGURATION var token = "YOUR_BOT_TOKEN"; // Your Telegram bot token var url = "YOUR_GOOGLE_APPS_SCRIPT_URL"; // Your Google Apps Script URL // Function to set the Telegram webhook function setWebhook() { var apiEndpoint = `https://api.telegram.org/bot${token}/setWebhook?url=${url}`; try { // Make the HTTP request to set the webhook var response = UrlFetchApp.fetch(apiEndpoint); // Log the response to the Logger for debugging Logger.log(response.getContentText()); } catch (error) { Logger.log("Error setting webhook: " + error); } }
- Replace
YOUR_BOT_TOKEN
andYOUR_GOOGLE_APPS_SCRIPT_URL
inwebhook.gs
with your actual bot token and the URL of your Google Apps Script. - Save your project and click on
Deploy > New deployment
. - Select
Web app
, give it a name, and ensure to set the access to "Anyone". - Deploy the web app and copy the provided URL.
- Run the
setWebhook
function inwebhook.gs
to set the webhook for your bot.
- You can customize the message sent to users when they type
/start
by modifying the text in thesendMessage
function insidehandleCommands
. - You can add additional commands by extending the
handleCommands
function.
This project is licensed under the MIT License. Feel free to modify and use it as you wish.
Feel free to make any further modifications!