RubigramClient 1.4.0__py3-none-any.whl → 1.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of RubigramClient might be problematic. Click here for more details.
- rubigram/__init__.py +2 -2
- rubigram/client.py +101 -45
- rubigram/filters.py +149 -97
- rubigram/method.py +88 -66
- rubigram/network.py +53 -25
- rubigram/types.py +222 -336
- {rubigramclient-1.4.0.dist-info → rubigramclient-1.4.2.dist-info}/METADATA +1 -1
- rubigramclient-1.4.2.dist-info/RECORD +11 -0
- rubigramclient-1.4.0.dist-info/RECORD +0 -11
- {rubigramclient-1.4.0.dist-info → rubigramclient-1.4.2.dist-info}/WHEEL +0 -0
- {rubigramclient-1.4.0.dist-info → rubigramclient-1.4.2.dist-info}/licenses/LICENSE +0 -0
- {rubigramclient-1.4.0.dist-info → rubigramclient-1.4.2.dist-info}/top_level.txt +0 -0
rubigram/method.py
CHANGED
|
@@ -1,86 +1,79 @@
|
|
|
1
|
-
from
|
|
1
|
+
from .network import Network
|
|
2
2
|
from typing import Literal, Optional
|
|
3
|
-
from
|
|
3
|
+
from .types import Bot, Chat, Keypad, MessageId, Updates
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
class Method(Network):
|
|
6
7
|
def __init__(self, token: str):
|
|
7
8
|
super().__init__(token)
|
|
8
|
-
|
|
9
|
+
|
|
9
10
|
async def get_me(self) -> "Bot":
|
|
10
11
|
response = await self.request("getMe", {})
|
|
11
|
-
return Bot.
|
|
12
|
+
return Bot.from_dict(response["bot"])
|
|
12
13
|
|
|
13
14
|
async def get_chat(self, chat_id: str) -> "Chat":
|
|
14
15
|
response = await self.request("getChat", {"chat_id": chat_id})
|
|
15
|
-
return Chat.
|
|
16
|
-
|
|
17
|
-
async def get_update(self, limit: int = 1, offset_id: Optional[int] = None) ->
|
|
16
|
+
return Chat.from_dict(response["chat"])
|
|
17
|
+
|
|
18
|
+
async def get_update(self, limit: int = 1, offset_id: Optional[int] = None) -> "Updates":
|
|
18
19
|
response = await self.request("getUpdates", {"limit": limit, "offset_id": offset_id})
|
|
19
|
-
return
|
|
20
|
-
|
|
20
|
+
return Updates.from_dict(response)
|
|
21
|
+
|
|
21
22
|
async def get_file(self, file_id: str) -> str:
|
|
22
23
|
response = await self.request("getFile", {"file_id": file_id})
|
|
23
|
-
return response["
|
|
24
|
-
|
|
24
|
+
return response["download_url"]
|
|
25
|
+
|
|
25
26
|
async def set_command(self, command: list):
|
|
26
27
|
response = await self.request("setCommands", {"bot_commands": command})
|
|
27
28
|
return response
|
|
28
|
-
|
|
29
|
+
|
|
29
30
|
async def update_bot_endpoint(self, url: str, type: Literal["ReceiveUpdate", "ReceiveInlineMessage", "ReceiveQuery", "GetSelectionItem", "SearchSelectionItems"]):
|
|
30
31
|
response = await self.request("updateBotEndpoints", {"url": url, "type": type})
|
|
31
32
|
return response
|
|
32
|
-
|
|
33
|
-
async def request_send_file(self, type: str):
|
|
34
|
-
response = await self.request("requestSendFile", {"type": type})
|
|
35
|
-
return response["data"]["upload_url"]
|
|
36
|
-
|
|
37
|
-
async def upload_file(self, path: str, name: str, type: str):
|
|
38
|
-
upload_url = await self.request_send_file(type)
|
|
39
|
-
response = await self.request_upload_file(upload_url, path, name)
|
|
40
|
-
return response
|
|
41
|
-
|
|
42
|
-
async def forward_message(self, from_chat_id: str, message_id: str, to_chat_id: str, disable_notification: bool = False) -> "MessageId":
|
|
43
|
-
data = {"from_chat_id": from_chat_id, "message_id": message_id, "to_chat_id": to_chat_id, "disable_notification": disable_notification}
|
|
44
|
-
response = await self.request("forwardMessage", data)
|
|
45
|
-
return MessageId.read(response["data"])
|
|
46
|
-
|
|
33
|
+
|
|
47
34
|
async def delete_message(self, chat_id: str, message_id: str):
|
|
48
35
|
await self.request("deleteMessage", {"chat_id": chat_id, "message_id": message_id})
|
|
49
|
-
|
|
36
|
+
|
|
50
37
|
async def remove_chat_keypad(self, chat_id: str):
|
|
51
38
|
await self.request("editChatKeypad", {"chat_id": chat_id, "chat_keypad_type": "Remove"})
|
|
52
|
-
|
|
39
|
+
|
|
53
40
|
async def edit_chat_keypad(self, chat_id: str, chat_keypad):
|
|
54
41
|
await self.request("editChatKeypad", {"chat_id": chat_id, "chat_keypad_type": "New", "chat_keypad": chat_keypad})
|
|
55
42
|
|
|
56
43
|
async def edit_message_keypad(self, chat_id: str, message_id: str, inline_keypad):
|
|
57
44
|
await self.request("editMessageKeypad", {"chat_id": chat_id, "message_id": message_id, "inline_keypad": inline_keypad})
|
|
58
|
-
|
|
45
|
+
|
|
59
46
|
async def edit_message_text(self, chat_id: str, message_id: str, text: str):
|
|
60
47
|
await self.request("editMessageText", {"chat_id": chat_id, "message_id": message_id, "text": text})
|
|
61
|
-
|
|
48
|
+
|
|
49
|
+
async def forward_message(self, from_chat_id: str, message_id: str, to_chat_id: str, disable_notification: bool = False) -> "MessageId":
|
|
50
|
+
data = {"from_chat_id": from_chat_id, "message_id": message_id,
|
|
51
|
+
"to_chat_id": to_chat_id, "disable_notification": disable_notification}
|
|
52
|
+
response = await self.request("forwardMessage", data)
|
|
53
|
+
return MessageId.from_dict(response)
|
|
54
|
+
|
|
62
55
|
async def send_message(
|
|
63
56
|
self,
|
|
64
57
|
chat_id: str,
|
|
65
58
|
text: str,
|
|
66
59
|
chat_keypad: Keypad = None,
|
|
67
|
-
inline_keypad: Keypad= None,
|
|
60
|
+
inline_keypad: Keypad = None,
|
|
68
61
|
chat_keypad_type: Literal["New", "Remove"] = None,
|
|
69
62
|
disable_notification: bool = None,
|
|
70
|
-
reply_to_message_id
|
|
63
|
+
reply_to_message_id=None
|
|
71
64
|
) -> "MessageId":
|
|
72
65
|
data = {
|
|
73
66
|
"chat_id": chat_id,
|
|
74
67
|
"text": text,
|
|
75
|
-
"chat_keypad": chat_keypad.
|
|
76
|
-
"inline_keypad": inline_keypad.
|
|
68
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
69
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
77
70
|
"chat_keypad_type": chat_keypad_type,
|
|
78
71
|
"disable_notification": disable_notification,
|
|
79
72
|
"reply_to_message_id": reply_to_message_id
|
|
80
73
|
}
|
|
81
74
|
response = await self.request("sendMessage", data)
|
|
82
|
-
return MessageId.
|
|
83
|
-
|
|
75
|
+
return MessageId.from_dict(response)
|
|
76
|
+
|
|
84
77
|
async def send_poll(
|
|
85
78
|
self,
|
|
86
79
|
chat_id: str,
|
|
@@ -96,15 +89,15 @@ class Method(NetWork):
|
|
|
96
89
|
"chat_id": chat_id,
|
|
97
90
|
"question": question,
|
|
98
91
|
"options": options,
|
|
99
|
-
"chat_keypad": chat_keypad.
|
|
100
|
-
"inline_keypad": inline_keypad.
|
|
92
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
93
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
101
94
|
"disable_notification": disable_notification,
|
|
102
95
|
"reply_to_message_id": reply_to_message_id,
|
|
103
96
|
"chat_keypad_type": chat_keypad_type
|
|
104
97
|
}
|
|
105
98
|
response = await self.request("sendPoll", data)
|
|
106
|
-
return MessageId.
|
|
107
|
-
|
|
99
|
+
return MessageId.from_dict(response)
|
|
100
|
+
|
|
108
101
|
async def send_location(
|
|
109
102
|
self,
|
|
110
103
|
chat_id: str,
|
|
@@ -120,15 +113,15 @@ class Method(NetWork):
|
|
|
120
113
|
"chat_id": chat_id,
|
|
121
114
|
"latitude": latitude,
|
|
122
115
|
"longitude": longitude,
|
|
123
|
-
"chat_keypad": chat_keypad.
|
|
124
|
-
"inline_keypad": inline_keypad.
|
|
116
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
117
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
125
118
|
"disable_notification": disable_notification,
|
|
126
119
|
"reply_to_message_id": reply_to_message_id,
|
|
127
120
|
"chat_keypad_type": chat_keypad_type
|
|
128
121
|
}
|
|
129
122
|
response = await self.request("sendLocation", data)
|
|
130
|
-
return MessageId.
|
|
131
|
-
|
|
123
|
+
return MessageId.from_dict(response)
|
|
124
|
+
|
|
132
125
|
async def send_contact(
|
|
133
126
|
self,
|
|
134
127
|
chat_id: str,
|
|
@@ -146,15 +139,15 @@ class Method(NetWork):
|
|
|
146
139
|
"first_name": first_name,
|
|
147
140
|
"last_name": last_name,
|
|
148
141
|
"phone_number": phone_number,
|
|
149
|
-
"chat_keypad": chat_keypad.
|
|
150
|
-
"inline_keypad": inline_keypad.
|
|
142
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
143
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
151
144
|
"disable_notification": disable_notification,
|
|
152
145
|
"reply_to_message_id": reply_to_message_id,
|
|
153
146
|
"chat_keypad_type": chat_keypad_type
|
|
154
147
|
}
|
|
155
148
|
response = await self.request("sendContact", data)
|
|
156
|
-
return MessageId.
|
|
157
|
-
|
|
149
|
+
return MessageId.from_dict(response)
|
|
150
|
+
|
|
158
151
|
async def send_sticker(
|
|
159
152
|
self,
|
|
160
153
|
chat_id: str,
|
|
@@ -168,41 +161,70 @@ class Method(NetWork):
|
|
|
168
161
|
data = {
|
|
169
162
|
"chat_id": chat_id,
|
|
170
163
|
"sticker_id": sticker_id,
|
|
171
|
-
"chat_keypad": chat_keypad.
|
|
172
|
-
"inline_keypad": inline_keypad.
|
|
164
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
165
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
173
166
|
"disable_notification": disable_notification,
|
|
174
167
|
"reply_to_message_id": reply_to_message_id,
|
|
175
168
|
"chat_keypad_type": chat_keypad_type
|
|
176
169
|
}
|
|
170
|
+
|
|
177
171
|
response = await self.request("sendSticker", data)
|
|
178
|
-
return MessageId.
|
|
179
|
-
|
|
172
|
+
return MessageId.from_dict(response)
|
|
173
|
+
|
|
174
|
+
async def request_send_file(self, type: str):
|
|
175
|
+
response = await self.request("requestSendFile", {"type": type})
|
|
176
|
+
return response["upload_url"]
|
|
177
|
+
|
|
178
|
+
async def upload_file(self, file: str, name: str, type: str):
|
|
179
|
+
upload_url = await self.request_send_file(type)
|
|
180
|
+
response = await self.request_upload_file(upload_url, file, name)
|
|
181
|
+
return response
|
|
182
|
+
|
|
183
|
+
async def download_file(self, file_id: str, file_name: str):
|
|
184
|
+
download_url = await self.get_file(file_id)
|
|
185
|
+
response = await self.request_download_file(download_url, file_name)
|
|
186
|
+
return response
|
|
187
|
+
|
|
180
188
|
async def send_file(
|
|
181
189
|
self,
|
|
182
190
|
chat_id: str,
|
|
183
|
-
|
|
191
|
+
file: str,
|
|
184
192
|
file_name: str,
|
|
185
|
-
type: Literal["File", "Image", "Voice",
|
|
193
|
+
type: Literal["File", "Image", "Voice",
|
|
194
|
+
"Music", "Gif", "Video"] = "File",
|
|
186
195
|
chat_keypad: Keypad = None,
|
|
187
196
|
inline_keypad: Keypad = None,
|
|
197
|
+
chat_keypad_type: Literal["New", "Remove"] = None,
|
|
188
198
|
disable_notification: bool = False,
|
|
189
199
|
reply_to_message_id: str = None,
|
|
190
|
-
chat_keypad_type: Literal["New", "Remove"] = None,
|
|
191
200
|
) -> "MessageId":
|
|
192
|
-
file_id = await self.upload_file(
|
|
201
|
+
file_id = await self.upload_file(file, file_name, type)
|
|
193
202
|
data = {
|
|
194
203
|
"chat_id": chat_id,
|
|
195
204
|
"file_id": file_id,
|
|
196
|
-
"chat_keypad": chat_keypad.
|
|
197
|
-
"inline_keypad": inline_keypad.
|
|
205
|
+
"chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
|
|
206
|
+
"inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
|
|
198
207
|
"disable_notification": disable_notification,
|
|
199
208
|
"reply_to_message_id": reply_to_message_id,
|
|
200
209
|
"chat_keypad_type": chat_keypad_type,
|
|
201
210
|
}
|
|
202
211
|
response = await self.request("sendFile", data)
|
|
203
|
-
return MessageId.
|
|
204
|
-
|
|
205
|
-
async def
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
212
|
+
return MessageId.from_dict(response)
|
|
213
|
+
|
|
214
|
+
async def send_document(self, chat_id: str, document: str, name: str, **kwargs):
|
|
215
|
+
return await self.send_file(chat_id, document, name, "File", **kwargs)
|
|
216
|
+
|
|
217
|
+
async def send_photo(self, chat_id: str, photo: str, name: str, **kwargs):
|
|
218
|
+
return await self.send_file(chat_id, photo, name, "Image", **kwargs)
|
|
219
|
+
|
|
220
|
+
async def send_video(self, chat_id: str, video: str, name: str, **kwargs):
|
|
221
|
+
return await self.send_file(chat_id, video, name, "Video", **kwargs)
|
|
222
|
+
|
|
223
|
+
async def send_gif(self, chat_id: str, gif: str, name: str, **kwargs):
|
|
224
|
+
return await self.send_file(chat_id, gif, name, "Gif", **kwargs)
|
|
225
|
+
|
|
226
|
+
async def send_music(self, chat_id: str, music: str, name: str, **kwargs):
|
|
227
|
+
return await self.send_file(chat_id, music, name, "Music", **kwargs)
|
|
228
|
+
|
|
229
|
+
async def send_voice(self, chat_id: str, voice: str, name: str, **kwargs):
|
|
230
|
+
return await self.send_file(chat_id, voice, name, "Voice", **kwargs)
|
rubigram/network.py
CHANGED
|
@@ -1,30 +1,58 @@
|
|
|
1
1
|
from aiohttp import ClientSession, FormData
|
|
2
|
+
from typing import Any, Optional, Dict, Union
|
|
2
3
|
import aiofiles
|
|
4
|
+
import re
|
|
5
|
+
import os
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
self.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
|
|
8
|
+
class Network:
|
|
9
|
+
def __init__(self, token: str) -> None:
|
|
10
|
+
self.token: str = token
|
|
11
|
+
self.session: Optional[ClientSession] = None
|
|
12
|
+
self.api: str = f"https://botapi.rubika.ir/v3/{self.token}/"
|
|
13
|
+
|
|
14
|
+
async def get_session(self) -> ClientSession:
|
|
15
|
+
if self.session is None or self.session.closed:
|
|
16
|
+
self.session = ClientSession()
|
|
17
|
+
return self.session
|
|
18
|
+
|
|
19
|
+
async def close(self) -> None:
|
|
20
|
+
if self.session and not self.session.closed:
|
|
21
|
+
await self.session.close()
|
|
22
|
+
|
|
23
|
+
async def request(self, method: str, json: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
24
|
+
session = await self.get_session()
|
|
25
|
+
async with session.post(self.api + method, json=json) as response:
|
|
26
|
+
response.raise_for_status()
|
|
27
|
+
data: dict = await response.json()
|
|
28
|
+
return data.get("data")
|
|
29
|
+
|
|
30
|
+
async def request_bytes_file(self, url: str) -> bytes:
|
|
31
|
+
session = await self.get_session()
|
|
32
|
+
async with session.get(url) as response:
|
|
33
|
+
response.raise_for_status()
|
|
34
|
+
return await response.read()
|
|
35
|
+
|
|
36
|
+
async def request_upload_file(self, upload_url: str, file: Union[str], name: str) -> str:
|
|
37
|
+
session = await self.get_session()
|
|
38
|
+
|
|
39
|
+
if isinstance(file, str) and re.match(r"^https?://", file):
|
|
40
|
+
file = await self.request_bytes_file(file)
|
|
14
41
|
|
|
15
|
-
|
|
42
|
+
elif isinstance(file, str) and os.path.isfile(file):
|
|
43
|
+
async with aiofiles.open(file, "rb") as f:
|
|
44
|
+
file = await f.read()
|
|
45
|
+
|
|
16
46
|
form = FormData()
|
|
17
|
-
form.add_field("file",
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
async def request_download_file(self, url: str, name: str):
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
await file.write(await response.read())
|
|
30
|
-
return {"status": True, "file": name}
|
|
47
|
+
form.add_field("file", file, filename=name, content_type="application/octet-stream")
|
|
48
|
+
|
|
49
|
+
async with session.post(upload_url, data=form) as response:
|
|
50
|
+
response.raise_for_status()
|
|
51
|
+
data = await response.json()
|
|
52
|
+
return data["data"]["file_id"]
|
|
53
|
+
|
|
54
|
+
async def request_download_file(self, url: str, name: str) -> dict[str, Union[str, bool]]:
|
|
55
|
+
file = await self.request_bytes_file(url)
|
|
56
|
+
async with aiofiles.open(name, "wb") as f:
|
|
57
|
+
await f.write(file)
|
|
58
|
+
return {"status": True, "file": name}
|