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/method.py CHANGED
@@ -1,86 +1,79 @@
1
- from rubigram.network import NetWork
1
+ from .network import Network
2
2
  from typing import Literal, Optional
3
- from rubigram.types import Bot, Chat, Update, Keypad, MessageId
3
+ from .types import Bot, Chat, Keypad, MessageId, Updates
4
4
 
5
- class Method(NetWork):
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.read(response["data"]["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.read(response["data"]["chat"])
16
-
17
- async def get_update(self, limit: int = 1, offset_id: Optional[int] = None) -> list[Update]:
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 [Update.read(update) for update in response["data"]["updates"]]
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["data"]["download_url"]
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 = None
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._dict() if chat_keypad else None,
76
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
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._dict() if chat_keypad else None,
100
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
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._dict() if chat_keypad else None,
124
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
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._dict() if chat_keypad else None,
150
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
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._dict() if chat_keypad else None,
172
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
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
- path: str,
191
+ file: str,
184
192
  file_name: str,
185
- type: Literal["File", "Image", "Voice", "Music", "Gif", "Video"] = "File",
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(path, file_name, type)
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._dict() if chat_keypad else None,
197
- "inline_keypad": inline_keypad._dict() if inline_keypad else None,
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.read(response["data"])
204
-
205
- async def download_file(self, file_id: str, file_name: str):
206
- download_url = await self.get_file(file_id)
207
- response = await self.request_download_file(download_url, file_name)
208
- return response
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
- class NetWork:
5
- def __init__(self, token: str):
6
- self.token = token
7
- self.api = f"https://botapi.rubika.ir/v3/{self.token}/"
8
-
9
- async def request(self, method: str, json: dict) -> dict:
10
- async with ClientSession() as session:
11
- async with session.post(self.api + method, json=json) as response:
12
- response.raise_for_status()
13
- return await response.json()
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
- async def request_upload_file(self, url: str, path: str, name: str) -> str:
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", open(path, "rb"), filename=name, content_type="application/octet-stream")
18
- async with ClientSession() as session:
19
- async with session.post(url, data=form) as response:
20
- response.raise_for_status()
21
- result = await response.json()
22
- return result["data"]["file_id"]
23
-
24
- async def request_download_file(self, url: str, name: str):
25
- async with ClientSession() as session:
26
- async with session.get(url) as response:
27
- response.raise_for_status()
28
- async with aiofiles.open(name, mode="wb") as file:
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}