A simple and easy-to-use Python library for Telegram bots. This library allows you to send messages, edit messages, fetch updates, and handle messages easily.
Installation using pip (a Python package manager):
pip install simple-telegram-api
A simple echo bot:
from simple_telegram_api import TelegramBot
BOT_TOKEN = "BOT_TOKEN"
bot = TelegramBot(BOT_TOKEN)
# Skip old messages before bot is running.
bot.reset_updates()
print("Bot is running.")
while True:
updates = bot.get_updates()
# Check if it's empty.
if updates:
print(updates)
bot.reset_updates(updates=updates)
# For multiple coming up messages.
for update in updates["result"]:
chat_id = update["message"]["chat"]["id"]
user_message = update["message"]["text"]
bot_update = bot.send_message(user_message, chat_id=chat_id)
print(bot_update)
from simple_telegram_api import TelegramBot
bot = TelegramBot('BOT_TOKEN')
This function gets new messages from Telegram.
updates = bot.get_updates()
This function gets updates from Telegram and skips old messages.
bot.reset_updates()
To send a message:
bot.send_message(text=text, chat_id=chat_id)
To reply to a message:
bot.send_message(text=text, chat_id=chat_id, reply_to_message=True, message_id=message_id)
bot.edit_message(text=text, chat_id=chat_id, message_id=message_id)
If updates
is not provided in reset_updates()
, new updates will be fetched automatically. Use the result from get_updates()
as updates
, as shown in the example.