pyTelegramBotAPI

4.33.0 · active · verified Sat Apr 11

pyTelegramBotAPI is a straightforward yet comprehensive Python library providing both synchronous and asynchronous implementations of the Telegram Bot API. It enables developers to easily create Telegram bots with features like message handling, inline queries, and custom keyboards. The library is actively maintained, with frequent updates (often monthly or bi-monthly) to support the latest Telegram Bot API versions, and the current version is 4.33.0.

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic synchronous 'EchoBot' that replies to /start, /help, and any other text message by echoing the input. Ensure you replace 'YOUR_BOT_TOKEN_HERE' or set the `TELEGRAM_BOT_TOKEN` environment variable with the token obtained from @BotFather. The bot uses `infinity_polling()` to continuously check for new messages.

import os
import telebot

API_TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN', 'YOUR_BOT_TOKEN_HERE')

if not API_TOKEN or API_TOKEN == 'YOUR_BOT_TOKEN_HERE':
    print("Warning: TELEGRAM_BOT_TOKEN environment variable not set or placeholder used. \n"\
          "Please obtain a token from @BotFather on Telegram and set it.")
    exit(1)

bot = telebot.TeleBot(API_TOKEN)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Hi there, I am an EchoBot. I will echo your messages.")

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    bot.reply_to(message, message.text)

print("Bot started polling...")
bot.infinity_polling()

view raw JSON →