A pure and modern python package for telegram bots.
Project description
Python TelegramBots
A pure and modern python package to communicate with telegram bot api.
About
This is an async wrapper over telegram bot api written using python ( + 3.10 ).
This package tries to be close to the bot api interface and there are almost nothing more than it. Of course the package is extendable and other features can be installed separately.
-
All objects and methods ( 6.0 ) are implemented in python using
dataclass
es. -
The serialization stuff are done to convert api python objects to json-like (
dict
,list
) objects and then json-string and reverse. -
A simple and async http client is available using aiohttp to send requests
This package contains no full-featured client or bound methods! there're only some classes and a client.
How to
All api methods are available under following namespace:
telegrambots.wrapper.types.methods
And for objects
telegrambots.wrapper.types.methods
Make a request
You can use our async client to make requests.
import asyncio
from src.telegrambots.wrapper import TelegramBotsClient
from src.telegrambots.wrapper.types.methods import SendMessage
from src.telegrambots.wrapper.types.objects import (
InlineKeyboardButton,
InlineKeyboardMarkup,
)
async def main():
client = TelegramBotsClient("BOT_TOKEN")
message = await client(
SendMessage(
123456789,
"Here is a message from python-telegrambots",
reply_markup=InlineKeyboardMarkup(
InlineKeyboardButton.with_url(
"Repo", "https://github.com/python-telegrambots"
)
),
)
)
print(message.message_id)
if __name__ == "__main__":
asyncio.run(main())
Everything is documented and well type hinted.
Send a file or even multiple files ( local or online ).
import asyncio
from pathlib import Path
from src.telegrambots.wrapper import TelegramBotsClient
from src.telegrambots.wrapper.types.methods import SendMediaGroup
from src.telegrambots.wrapper.types.objects import InputFile, InputMediaPhoto
async def main():
client = TelegramBotsClient("BOT_TOKEN")
file_path_1 = Path(__file__).parent.resolve().joinpath("test_photo_1.jpg")
file_path_2 = Path(__file__).parent.resolve().joinpath("test_photo_2.jpg")
with InputFile(file_path_1) as file:
with SendMediaGroup(
123456789,
[
InputMediaPhoto(file, "My first photo"),
InputMediaPhoto(InputFile(open(file_path_2, "rb"), "test_photo_2.jpg")),
InputMediaPhoto("https://imgur.com/t/funny/MpMGFRQ"),
],
) as send_media_group:
result = await client(send_media_group)
print(result.__len__())
if __name__ == "__main__":
asyncio.run(main())
Send multiple requests using one aiohttp.ClientSession
.
import asyncio
from src.telegrambots.wrapper import TelegramBotsClient
from src.telegrambots.wrapper.types.methods import (
SendMessage,
EditMessageText,
DeleteMessage,
)
async def main():
client = TelegramBotsClient("BOT_TOKEN")
async with client:
message = await client(
SendMessage(12345678, "It's a long time that want to say ... I love you.")
)
edited_message = await client(
EditMessageText(
chat_id=message.chat.id,
message_id=message.message_id,
text="Oh sorry, how are you today??!",
)
)
assert edited_message
await client(
DeleteMessage(
chat_id=edited_message.chat.id, message_id=edited_message.message_id
)
)
if __name__ == "__main__":
asyncio.run(main())
Print things! usual way or pretty.
import asyncio
from src.telegrambots.wrapper import TelegramBotsClient
from src.telegrambots.wrapper.types.methods import SendMessage
async def main():
client = TelegramBotsClient("BOT_TOKEN")
async with client:
message = await client(SendMessage(12345678, "It's Show Time."))
# let's see what we got.
print(message)
# Message(message_id=134, date=1651407978, chat=Chat(id=106296897, ...
# ---- A long story here ----
# make it more clear
print(message.pretty_str())
# {
# "chat": {
# "first_name": "A̤̮ʀαՏH",
# "id": 12345678,
# "type": "private"
# },
# "date": 1651407978,
# "from": {
# "first_name": "TelegramBots Test",
# "id": 87654321,
# "is_bot": true,
# "username": "TestBot"
# },
# "message_id": 134,
# "text": "It's Show Time."
# }
if __name__ == "__main__":
asyncio.run(main())
Install
The preview package is available at PYPI.
Project details
Release history Release notifications | RSS feed
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
Hashes for telegrambots-0.0.8rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66d7d8dd8fe1b7840f07e4b8cc7cb56a771d735d27f88640023651fc00c2862f |
|
MD5 | e3a239a84c6cfd0429d55b46900d2205 |
|
BLAKE2b-256 | 4fe1183df91d94de80e382025f757d01df6e6c6418eb7d9272fa8a82d6249ce9 |