DEV Community

Cover image for πŸš€ Deploy a Telegram Bot for FREE Using Google Apps Script (GAS)
Sh Raj
Sh Raj

Posted on

πŸš€ Deploy a Telegram Bot for FREE Using Google Apps Script (GAS)

Image

Image

Image

Image

πŸš€ Deploy a Telegram Bot for FREE Using Google Apps Script (GAS)

Want to deploy a Telegram bot for free, without servers, VPS, or DevOps headaches?
Google Apps Script (GAS) is one of the simplest ways to do itβ€”especially for text-based bots, MVPs, and learning projects.

This guide walks you through a complete, production-ready setup using webhooks.


🧠 What Is Google Apps Script?

Google Apps Script is a serverless JavaScript runtime by Google, designed to automate Google productsβ€”but it can also act as a lightweight backend.

Key benefits:

  • βœ… Free
  • βœ… No server management
  • βœ… HTTPS by default
  • βœ… Easy webhook support

πŸ€– What You’ll Build

A Telegram bot that:

  • Receives messages via webhook
  • Processes them in GAS
  • Replies instantly using Telegram Bot API

Architecture:

Telegram β†’ Webhook β†’ GAS Web App β†’ Telegram API
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Prerequisites

  1. Telegram account
  2. A bot token from @botfather on Telegram
  3. Google account

πŸͺœ Step 1: Create a Telegram Bot

  1. Open Telegram
  2. Search for @botfather
  3. Run /start
  4. Run /newbot
  5. Set:
  • Bot name
  • Bot username
    1. Copy the Bot Token (keep it secret)

πŸͺœ Step 2: Create Google Apps Script Project

  1. Go to https://script.google.com
  2. Click New Project
  3. Rename it (e.g. telegram-bot-gas)

πŸͺœ Step 3: Add Bot Code (Webhook Handler)

Paste this minimal working bot:

const TOKEN = "YOUR_BOT_TOKEN";
const TELEGRAM_API = `https://api.telegram.org/bot${TOKEN}`;

function doPost(e) {
  const update = JSON.parse(e.postData.contents);

  if (update.message) {
    const chatId = update.message.chat.id;
    const text = update.message.text || "";

    sendMessage(chatId, `You said: ${text}`);
  }

  return ContentService.createTextOutput("OK");
}

function sendMessage(chatId, text) {
  const payload = {
    chat_id: chatId,
    text: text
  };

  UrlFetchApp.fetch(`${TELEGRAM_API}/sendMessage`, {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify(payload)
  });
}
Enter fullscreen mode Exit fullscreen mode

πŸ”’ Tip: For production, store the token in Script Properties instead of hardcoding.


πŸͺœ Step 4: Deploy as a Web App

  1. Click Deploy β†’ New deployment
  2. Type: Web app
  3. Execute as: Me
  4. Who has access: Anyone
  5. Click Deploy
  6. Copy the Web App URL

Example:

https://script.google.com/macros/s/AKfycbxxxxx/exec
Enter fullscreen mode Exit fullscreen mode

πŸͺœ Step 5: Set Telegram Webhook

Open this URL in your browser (replace values):

https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook?url=<WEB_APP_URL>
Enter fullscreen mode Exit fullscreen mode

If successful, you’ll see:

{"ok":true,"result":true}
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Your bot is now live.


πŸͺœ Step 6: Test Your Bot

  1. Open Telegram
  2. Send a message to your bot
  3. You should get an instant reply

✨ Adding Commands (Optional)

if (text === "/start") {
  sendMessage(chatId, "Welcome to my GAS bot!");
}
Enter fullscreen mode Exit fullscreen mode

βŒ› Important Limitations of GAS

Before scaling, understand these constraints:

Limitation Details
Execution time ~30s (free)
Cold starts 1–5s delay after idle
No background jobs Webhook-only execution
No streaming Bad for large media
No npm packages Pure JS only
Limited storage Sheets / Properties

βœ… Best for text bots, MVPs, automation
❌ Not for AI bots, media-heavy bots, high traffic


🟒 When GAS Is a Good Choice

Use GAS if:

  • You want 100% free hosting
  • Bot is text-based
  • Traffic is low to medium
  • You want fast setup
  • You’re building a prototype

πŸ”΅ When to Switch Platforms

Move away from GAS if you need:

  • Media streaming
  • Databases
  • AI inference
  • High concurrency

Better free alternatives:

  • Cloudflare Workers
  • Fly.io
  • Supabase (DB)

🧾 Final Thoughts

Google Apps Script is one of the fastest ways to deploy a Telegram bot for free.

Think of it as:

A powerful serverless glue, not a full backend.

For learning, MVPs, and simple botsβ€”it’s perfect.


If you want:

  • A production-grade GAS bot template
  • Inline keyboards
  • Sheets as a database
  • Migration guide to Cloudflare Workers

Comment below πŸ‘‡

Top comments (1)

Collapse
 
sh20raj profile image
Sh Raj