A simple telegram bot api
Project description
Simple Telegram Bot
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
Installation using pip (a Python package manager):
pip install simple-telegram-api
Usage/Examples
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)
Using the TelegramBot Class
from simple_telegram_api import TelegramBot
bot = TelegramBot('BOT_TOKEN')
Get Updates
This function gets new messages from Telegram.
updates = bot.get_updates()
Reset Updates
This method gets updates from Telegram and skips old messages.
bot.reset_updates()
Send Message
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)
Edit Message
bot.edit_message(text=text, chat_id=chat_id, message_id=message_id)
Recommendations
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.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file simple_telegram_api-0.2.0.tar.gz
.
File metadata
- Download URL: simple_telegram_api-0.2.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a153bb4af8259dd60c16c49589b04224ed709f469a7810cc14997d87aa58fa5 |
|
MD5 | cd206468703fae0eb7ff66ba481252b7 |
|
BLAKE2b-256 | ef5f9f5635efd1bf49de0b6a26ec50a0c79d467aad0d7de77f27b7b035f3431b |
File details
Details for the file simple_telegram_api-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: simple_telegram_api-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc9746b3d7a9b4dcc6fc4a84432bd3dc5a6c37573c51c3db2e9b6b9f003e0f69 |
|
MD5 | 3e2ac924c64a39147df44529be489de1 |
|
BLAKE2b-256 | 68204f087a4803308d3c7c600ee011dc15ade068e63fd97b586927edfc94996f |