Skip to content

Commit 2e7830d

Browse files
committed
Updated read me and a fix.
1 parent f8d8c4c commit 2e7830d

4 files changed

Lines changed: 209 additions & 1 deletion

File tree

README.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,200 @@
11
# Python TelegramBots
22

33
A pure and modern python package to communicate with telegram bot api.
4+
5+
## About
6+
7+
This is an async wrapper over telegram bot api written using python ( + 3.10 ).
8+
9+
This package tries to be close to the bot api interface and there are almost
10+
nothing more than it. Of course the package is extendable and other features
11+
can be installed separately.
12+
13+
- All objects and methods ( 6.0 ) are implemented in python using `dataclass`es.
14+
15+
- The serialization stuff are done to convert api python objects to json-like (`dict`, `list`) objects and then json-string and reverse.
16+
17+
- A simple and async http client is available using aiohttp to send requests
18+
19+
_This package contains no full-featured client or bound methods! there're only some classes and a client._
20+
21+
## How to
22+
23+
All api methods are available under following namespace:
24+
25+
```py
26+
telegrambots.wrapper.types.methods
27+
```
28+
29+
And for objects
30+
31+
```py
32+
telegrambots.wrapper.types.methods
33+
```
34+
35+
### Make a request
36+
37+
You can use our async client to make requests.
38+
39+
```py
40+
import asyncio
41+
42+
from src.telegrambots.wrapper import TelegramBotsClient
43+
from src.telegrambots.wrapper.types.methods import SendMessage
44+
from src.telegrambots.wrapper.types.objects import (
45+
InlineKeyboardButton,
46+
InlineKeyboardMarkup,
47+
)
48+
49+
50+
async def main():
51+
client = TelegramBotsClient("BOT_TOKEN")
52+
53+
message = await client(
54+
SendMessage(
55+
123456789,
56+
"Here is a message from python-telegrambots",
57+
reply_markup=InlineKeyboardMarkup(
58+
InlineKeyboardButton.with_url(
59+
"Repo", "https://github.com/python-telegrambots"
60+
)
61+
),
62+
)
63+
)
64+
65+
print(message.message_id)
66+
67+
68+
if __name__ == "__main__":
69+
asyncio.run(main())
70+
71+
```
72+
73+
_Everything is documented and well type hinted._
74+
75+
Send a file or even multiple files ( local or online ).
76+
77+
```py
78+
import asyncio
79+
from pathlib import Path
80+
81+
from src.telegrambots.wrapper import TelegramBotsClient
82+
from src.telegrambots.wrapper.types.methods import SendMediaGroup
83+
from src.telegrambots.wrapper.types.objects import InputFile, InputMediaPhoto
84+
85+
86+
async def main():
87+
client = TelegramBotsClient("BOT_TOKEN")
88+
89+
file_path_1 = Path(__file__).parent.resolve().joinpath("test_photo_1.jpg")
90+
file_path_2 = Path(__file__).parent.resolve().joinpath("test_photo_2.jpg")
91+
92+
with InputFile(file_path_1) as file:
93+
with SendMediaGroup(
94+
123456789,
95+
[
96+
InputMediaPhoto(file, "My first photo"),
97+
InputMediaPhoto(InputFile(open(file_path_2, "rb"), "test_photo_2.jpg")),
98+
InputMediaPhoto("https://imgur.com/t/funny/MpMGFRQ"),
99+
],
100+
) as send_media_group:
101+
result = await client(send_media_group)
102+
103+
print(result.__len__())
104+
105+
106+
if __name__ == "__main__":
107+
asyncio.run(main())
108+
```
109+
110+
Send multiple requests using one `aiohttp.ClientSession`.
111+
112+
``` py
113+
import asyncio
114+
115+
from src.telegrambots.wrapper import TelegramBotsClient
116+
from src.telegrambots.wrapper.types.methods import (
117+
SendMessage,
118+
EditMessageText,
119+
DeleteMessage,
120+
)
121+
122+
123+
async def main():
124+
client = TelegramBotsClient("BOT_TOKEN")
125+
126+
async with client:
127+
128+
message = await client(
129+
SendMessage(12345678, "It's a long time that want to say ... I love you.")
130+
)
131+
132+
edited_message = await client(
133+
EditMessageText(
134+
chat_id=message.chat.id,
135+
message_id=message.message_id,
136+
text="Oh sorry, how are you today??!",
137+
)
138+
)
139+
140+
assert edited_message
141+
142+
await client(
143+
DeleteMessage(
144+
chat_id=edited_message.chat.id, message_id=edited_message.message_id
145+
)
146+
)
147+
148+
149+
if __name__ == "__main__":
150+
asyncio.run(main())
151+
152+
```
153+
154+
Print things! usual way or pretty.
155+
156+
```py
157+
import asyncio
158+
159+
from src.telegrambots.wrapper import TelegramBotsClient
160+
from src.telegrambots.wrapper.types.methods import SendMessage
161+
162+
163+
async def main():
164+
client = TelegramBotsClient("BOT_TOKEN")
165+
166+
async with client:
167+
168+
message = await client(SendMessage(12345678, "It's Show Time."))
169+
170+
# let's see what we got.
171+
print(message)
172+
# Message(message_id=134, date=1651407978, chat=Chat(id=106296897, ...
173+
# ---- A long story here ----
174+
175+
# make it more clear
176+
print(message.pretty_str())
177+
# {
178+
# "chat": {
179+
# "first_name": "A̤̮ʀαՏH",
180+
# "id": 12345678,
181+
# "type": "private"
182+
# },
183+
# "date": 1651407978,
184+
# "from": {
185+
# "first_name": "TelegramBots Test",
186+
# "id": 87654321,
187+
# "is_bot": true,
188+
# "username": "TestBot"
189+
# },
190+
# "message_id": 134,
191+
# "text": "It's Show Time."
192+
# }
193+
194+
if __name__ == "__main__":
195+
asyncio.run(main())
196+
```
197+
198+
## Install
199+
200+
_The preview package is available at [PYPI](https://pypi.org/project/telegrambots)._

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = telegrambots
3-
version = 0.0.7-preview
3+
version = 0.0.8-preview
44
author = immmdreza
55
author_email = ir310022@gmail.com
66
description = A pure and modern python package for telegram bots.

src/telegrambots/wrapper/types/api_object.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABCMeta
22
import dataclasses
3+
import json
34
from typing import Any, Optional, TypeVar, cast
45

56
from .._client_utilities._client_setter import ClientSetter
@@ -182,3 +183,8 @@ def _deserialize(
182183
return result
183184

184185
# endregion
186+
187+
def pretty_str(self):
188+
return json.dumps(
189+
self.serialize(), indent=4, sort_keys=True, ensure_ascii=False
190+
)

src/telegrambots/wrapper/types/objects/message.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,8 @@ class Message(TelegramBotsObject, ClientTargetable):
408408
)
409409
"""*Optional*. Inline keyboard attached to the message. `login_url` buttons are represented as ordinary `url` buttons.
410410
"""
411+
412+
def __getattr__(self, item: str):
413+
if item == "from":
414+
return self.from_user
415+
raise AttributeError(item)

0 commit comments

Comments
 (0)