Telegram bots ยท pyTelegramBotAPI

pyTelegramBotAPI hosting, the library you import as telebot

pyTelegramBotAPI is the oldest of the Python Telegram libraries and the one most tutorials mean when they write telebot. Push the repository, set BOT_TOKEN, deploy: infinity_polling keeps asking Telegram for updates, the process starts again if it exits, and every push redeploys it.

Free
256 MB, always on, no card
Starter
1 GB, $2/mo or $20/yr
Standard
2 GB, $3/mo or $30/yr
Pro
4 GB, $6/mo or $60/yr

A minimal bot that deploys as is

import os
import telebot

bot = telebot.TeleBot(os.environ["BOT_TOKEN"])

@bot.message_handler(commands=["start"])
def start(message):
    bot.reply_to(message, "Hello from FadeHost")

bot.infinity_polling()

Save it as bot.py and put pyTelegramBotAPI in requirements.txt. The package name and the import name differ, which is the first thing that goes wrong here: you install pyTelegramBotAPI and you import telebot. Installing something called telebot gets you a different library and a failed start.

infinity_polling, and what starts it again

infinity_polling() absorbs network errors and keeps polling, which is what you want on a host. If the process does exit, for an unhandled exception in a handler or a memory kill, the panel starts it again and the live console keeps the traceback. There is no wrapper loop to write, no supervisor to configure and no keep-alive ping to schedule.

Threads, AsyncTeleBot and memory

The default TeleBot runs handlers in threads and AsyncTeleBot is the asyncio version of the same API. Either fits the free 256 MB for a command bot. The one that outgrows it holds images or large replies in memory per user; the 24 hour usage chart on the bot's page shows how close it is running.

Keeping data between deploys

A redeploy replaces the code, not the disk. Anything written under /data is still there afterwards, so a SQLite file or a JSON store belongs in there. When two bots need the same data, a managed MySQL or PostgreSQL is one click away and answers on your private network only.

Frequently asked questions

Is pyTelegramBotAPI the same thing as telebot?

One library, two names: the package on PyPI is pyTelegramBotAPI and the module you import is telebot. Put pyTelegramBotAPI in requirements.txt.

polling or infinity_polling?

infinity_polling for a hosted bot, because it keeps going through network errors. polling(none_stop=True) is the older spelling of the same idea and runs fine.

Polling or webhook?

Polling works everywhere and needs nothing but the token: the bot asks Telegram for updates. A webhook needs a public https address; every hosted app has one at name.fadehost.app with its plan, but on the free tier that address sleeps when idle, so polling is the simpler choice for a free bot and a webhook is for a paid app that stays up.

Where does the token go?

In the environment variables of the app, as BOT_TOKEN or whatever name your code reads, never in the repository. The panel masks the values and injects them when the bot starts.

What happens when I push a commit?

With auto-deploy on, every push to the branch you chose redeploys the bot: install, build if there is one, start. The live console shows each step and the crash doctor explains a failed start in plain words.

What does the free tier include?

One bot with 256 MB of RAM, always on, placed where there is room for it (a paid plan picks the region), with the live console, environment variables and redeploys on push. A second bot or more memory is the Starter plan at $2 a month.

Related

pyTelegramBotAPI hosting, the library you import as telebot

Deploy a telebot bot free