All documentation

Telegram bot in Python or Node.js

How to run a bot on aiogram, python-telegram-bot, telegraf or grammY — with long polling or a webhook.

A bot is a regular app from Git or an archive. There are two ways to run it, and the price depends on the choice.

WayWhen it fitsPrice
Long polling (background process)The bot asks Telegram for new messages itself. Simplest, works everywhereThe full app plan: the bot does not sleep
WebhookTelegram sends messages to the bot's addressYou can enable sleep: with no messages you pay 25% of the plan

Bot with long polling

  1. “New resource” → “App”.
  2. Enter the repository link and tick “Background process or Telegram bot, no website”. No port needed: the deploy checks that the process is running.
  3. In “Environment variables” add the token from @BotFather:
BOT_TOKEN=123456:ABC...
  1. Plan Nano or Micro: an aiogram bot needs 256–512 MB.

How the platform knows what to run:

  • a Procfile with the line worker: python bot.py — that line runs;
  • Python without a Procfile — it looks for app.py, then main.py, then bot.py;
  • Node.js — npm start from package.json.

Read the token from the variable, not from the code:

import os
from aiogram import Bot
bot = Bot(token=os.environ["BOT_TOKEN"])

Bot with a webhook

Create a regular app without the "background process" tick. The platform passes the port in the PORT variable — listen on it. The app address appears in the resource card; set the webhook like this:

https://api.telegram.org/bot<token>/setWebhook?url=https://<app address>/webhook

In the “Settings” tab you can enable sleep: the first message after a quiet period wakes the bot in a few seconds.

Log and restarts

The “Log” tab streams the bot's output. If the bot crashes, the resource restarts by itself and the event appears in the feed. To learn about crashes right away, add a notification channel or an account webhook for the “Resource crashed” event.