A simple and easy-to-use Python wrapper for Telegram bots
Project description
Simple Telegram API
A simple and easy-to-use Python wrapper for Telegram bots. This library allows you to send messages, edit messages, fetch updates, and handle messages easily.
Getting a Bot Token
To get a bot token, message @BotFather on Telegram and follow the instructions to create a new bot. BotFather will provide you with a token that looks like 123456789:ABCdefGHIjklmNOPQrstUVwxyz
.
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)
# Delete 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["result"]:
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()
Example Get Updates Output
Here is an example of the output from the get_updates()
function:
{
"ok": True,
"result": [
{
"update_id": 123456789,
"message": {
"message_id": 123,
"from": {
"id": 123456789,
"is_bot": False,
"first_name": "Person Name",
"username": "person",
"language_code": "en"
},
"chat": {
"id": 123456789,
"first_name": "Person Name",
"username": "person",
"type": "private"
},
"date": 1733920402,
"text": "Hi!"
}
}
]
}
Reset Updates
This function deletes old messages from updates.
bot.reset_updates(updates=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)
Example Send Message Output
Here is an example of the output from the send_message()
function:
{
"ok": True,
"result": {
"message_id": 124,
"from": {
"id": 123456789,
"is_bot": True,
"first_name": "Bot",
"username": "bot"
},
"chat": {
"id": 123456789,
"first_name": "Person Name",
"username": "person",
"type": "private"
},
"date": 1733920404,
"text": "Hi!"
}
}
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
This project is licensed under the MIT 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.9.0.tar.gz
.
File metadata
- Download URL: simple_telegram_api-0.9.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2347ab729b4ec85783ff8000ef09487416c4085dae21c44565e52c4c424a4a52 |
|
MD5 | 2d1f9cbcbdbd65a1a056d4994c56a838 |
|
BLAKE2b-256 | f8aec4a3f0735282017952032f7076fda2fe0699a08a8fbcd4f892719ba868d4 |
File details
Details for the file simple_telegram_api-0.9.0-py3-none-any.whl
.
File metadata
- Download URL: simple_telegram_api-0.9.0-py3-none-any.whl
- Upload date:
- Size: 4.8 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 | 879ec16adb64cfd442a788390f4f19d591686ce11def5895fb45caae3eed5156 |
|
MD5 | 8b4f1944ec634c9cf1ddd894d53cdbe9 |
|
BLAKE2b-256 | 82e484afb1f017152b78404e61dc2abaa83f7904cea674509cb64cb3f5a14607 |