RubigramClient 1.4.0__tar.gz → 1.4.1__tar.gz

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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: RubigramClient
3
- Version: 1.4.0
3
+ Version: 1.4.1
4
4
  Summary: A simple and flexible Python library for building advanced Rubika bots with powerful message handling, inline buttons, and custom filters.
5
5
  Author-email: Javad RZ <Javad.Py1385@gmail.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: RubigramClient
3
- Version: 1.4.0
3
+ Version: 1.4.1
4
4
  Summary: A simple and flexible Python library for building advanced Rubika bots with powerful message handling, inline buttons, and custom filters.
5
5
  Author-email: Javad RZ <Javad.Py1385@gmail.com>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -11,4 +11,5 @@ rubigram/client.py
11
11
  rubigram/filters.py
12
12
  rubigram/method.py
13
13
  rubigram/network.py
14
+ rubigram/state.py
14
15
  rubigram/types.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "RubigramClient"
3
- version = "1.4.0"
3
+ version = "1.4.1"
4
4
  description = "A simple and flexible Python library for building advanced Rubika bots with powerful message handling, inline buttons, and custom filters."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.7"
@@ -15,5 +15,6 @@ classifiers = [
15
15
  ]
16
16
  dependencies = [
17
17
  "aiohttp",
18
- "aiofiles"
18
+ "aiofiles",
19
+
19
20
  ]
@@ -0,0 +1,5 @@
1
+ from .network import Network
2
+ from .method import Method
3
+ from .client import Client
4
+ from . import filters
5
+ from . import state
@@ -55,11 +55,13 @@ class Client(Method):
55
55
 
56
56
  async def handle_update(self, data: dict):
57
57
  if "inline_message" in data:
58
- event = InlineMessage.read(data["inline_message"])
58
+ event = InlineMessage.from_dict(data["inline_message"])
59
59
  await asyncio.gather(*(h(self, event) for h in self.inlines_handler))
60
60
  elif "update" in data:
61
- event = Update.read(data["update"], self)
61
+ event = Update.from_dict(data["update"])
62
+ event.client = self
62
63
  await asyncio.gather(*(h(self, event) for h in self.messages_handler))
64
+
63
65
 
64
66
  async def set_endpoints(self):
65
67
  if not self.endpoint:
@@ -1,14 +1,21 @@
1
1
  from rubigram.types import Update, InlineMessage
2
- # from rubigram.state import state_manager
2
+ from rubigram.state import state_manager
3
3
  from typing import Union
4
4
  import re
5
5
 
6
6
 
7
- # def state(states: Union[str, list[str]]):
8
- # def filter(message: Union[Update, InlineMessage]):
9
- # user_state = state_manager.get_state(message.chat_id)
10
- # return user_state in states if isinstance(states, list) else user_state == states
11
- # return filter
7
+ def state(states: Union[str, list[str]]):
8
+ def filter(message: Union[Update, InlineMessage]):
9
+ user_state = state_manager.get_state(message.chat_id)
10
+ return user_state in states if isinstance(states, list) else user_state == states
11
+ return filter
12
+
13
+ def edited():
14
+ def filter(message: Update):
15
+ if isinstance(message, Update) and message.updated_message:
16
+ return message.updated_message.is_edited
17
+ return False
18
+ return filter
12
19
 
13
20
  def command(commands: Union[str, list[str]], prefix: str = "/"):
14
21
  def filter(message: Update):
@@ -70,6 +77,7 @@ def private():
70
77
  return False
71
78
  return filter
72
79
 
80
+
73
81
  def forward():
74
82
  def filter(message: Update):
75
83
  if isinstance(message, Update) and message.type == "NewMessage":
@@ -77,6 +85,30 @@ def forward():
77
85
  return False
78
86
  return filter
79
87
 
88
+ def forward_channel():
89
+ def filter(message: Update):
90
+ if isinstance(message, Update) and message.type == "NewMessage" and message.new_message.forwarded_from:
91
+ return message.new_message.forwarded_from.from_sender_id == "Channel"
92
+ return False
93
+ return filter
94
+
95
+
96
+ def forward_user():
97
+ def filter(message: Update):
98
+ if isinstance(message, Update) and message.type == "NewMessage" and message.new_message.forwarded_from:
99
+ return message.new_message.forwarded_from.from_sender_id == "User"
100
+ return False
101
+ return filter
102
+
103
+
104
+ def forward_bot():
105
+ def filter(message: Update):
106
+ if isinstance(message, Update) and message.type == "NewMessage" and message.new_message.forwarded_from:
107
+ return message.new_message.forwarded_from.from_sender_id == "Bot"
108
+ return False
109
+ return filter
110
+
111
+
80
112
  def location():
81
113
  def filter(message: Update):
82
114
  if isinstance(message, Update) and message.type == "NewMessage":
@@ -0,0 +1,355 @@
1
+ from rubigram.network import Network
2
+ from typing import Literal, Optional
3
+ from rubigram.types import Bot, Chat, Update, Keypad, MessageId
4
+
5
+
6
+ class Method(Network):
7
+ def __init__(self, token: str):
8
+ super().__init__(token)
9
+
10
+ async def get_me(self) -> "Bot":
11
+ response = await self.request("getMe", {})
12
+ return Bot.from_dict(response["bot"])
13
+
14
+ async def get_chat(self, chat_id: str) -> "Chat":
15
+ response = await self.request("getChat", {"chat_id": chat_id})
16
+ return Chat.from_dict(response["chat"])
17
+
18
+ async def get_update(self, limit: int = 1, offset_id: Optional[int] = None) -> list[Update]:
19
+ response = await self.request("getUpdates", {"limit": limit, "offset_id": offset_id})
20
+ return [Update.from_dict(update) for update in response["updates"]]
21
+
22
+ async def get_file(self, file_id: str) -> str:
23
+ response = await self.request("getFile", {"file_id": file_id})
24
+ return response["download_url"]
25
+
26
+ async def set_command(self, command: list):
27
+ response = await self.request("setCommands", {"bot_commands": command})
28
+ return response
29
+
30
+ async def update_bot_endpoint(self, url: str, type: Literal["ReceiveUpdate", "ReceiveInlineMessage", "ReceiveQuery", "GetSelectionItem", "SearchSelectionItems"]):
31
+ response = await self.request("updateBotEndpoints", {"url": url, "type": type})
32
+ return response
33
+
34
+ async def forward_message(self, from_chat_id: str, message_id: str, to_chat_id: str, disable_notification: bool = False) -> "MessageId":
35
+ data = {"from_chat_id": from_chat_id, "message_id": message_id, "to_chat_id": to_chat_id, "disable_notification": disable_notification}
36
+ response = await self.request("forwardMessage", data)
37
+ return MessageId.from_dict(response)
38
+
39
+ async def delete_message(self, chat_id: str, message_id: str):
40
+ await self.request("deleteMessage", {"chat_id": chat_id, "message_id": message_id})
41
+
42
+ async def remove_chat_keypad(self, chat_id: str):
43
+ await self.request("editChatKeypad", {"chat_id": chat_id, "chat_keypad_type": "Remove"})
44
+
45
+ async def edit_chat_keypad(self, chat_id: str, chat_keypad):
46
+ await self.request("editChatKeypad", {"chat_id": chat_id, "chat_keypad_type": "New", "chat_keypad": chat_keypad})
47
+
48
+ async def edit_message_keypad(self, chat_id: str, message_id: str, inline_keypad):
49
+ await self.request("editMessageKeypad", {"chat_id": chat_id, "message_id": message_id, "inline_keypad": inline_keypad})
50
+
51
+ async def edit_message_text(self, chat_id: str, message_id: str, text: str):
52
+ await self.request("editMessageText", {"chat_id": chat_id, "message_id": message_id, "text": text})
53
+
54
+ async def send_message(
55
+ self,
56
+ chat_id: str,
57
+ text: str,
58
+ chat_keypad: Keypad = None,
59
+ inline_keypad: Keypad= None,
60
+ chat_keypad_type: Literal["New", "Remove"] = None,
61
+ disable_notification: bool = None,
62
+ reply_to_message_id = None
63
+ ) -> "MessageId":
64
+ data = {
65
+ "chat_id": chat_id,
66
+ "text": text,
67
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
68
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
69
+ "chat_keypad_type": chat_keypad_type,
70
+ "disable_notification": disable_notification,
71
+ "reply_to_message_id": reply_to_message_id
72
+ }
73
+ response = await self.request("sendMessage", data)
74
+ return MessageId.from_dict(response)
75
+
76
+ async def send_poll(
77
+ self,
78
+ chat_id: str,
79
+ question: str,
80
+ options: list[str],
81
+ chat_keypad: Keypad = None,
82
+ inline_keypad: Keypad = None,
83
+ disable_notification: bool = False,
84
+ reply_to_message_id: str = None,
85
+ chat_keypad_type: Literal["New", "Remove"] = None
86
+ ) -> "MessageId":
87
+ data = {
88
+ "chat_id": chat_id,
89
+ "question": question,
90
+ "options": options,
91
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
92
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
93
+ "disable_notification": disable_notification,
94
+ "reply_to_message_id": reply_to_message_id,
95
+ "chat_keypad_type": chat_keypad_type
96
+ }
97
+ response = await self.request("sendPoll", data)
98
+ return MessageId.from_dict(response)
99
+
100
+ async def send_location(
101
+ self,
102
+ chat_id: str,
103
+ latitude: str,
104
+ longitude: str,
105
+ chat_keypad: Keypad = None,
106
+ inline_keypad: Keypad = None,
107
+ disable_notification: bool = False,
108
+ reply_to_message_id: str = None,
109
+ chat_keypad_type: Literal["New", "Remove"] = None
110
+ ) -> "MessageId":
111
+ data = {
112
+ "chat_id": chat_id,
113
+ "latitude": latitude,
114
+ "longitude": longitude,
115
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
116
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
117
+ "disable_notification": disable_notification,
118
+ "reply_to_message_id": reply_to_message_id,
119
+ "chat_keypad_type": chat_keypad_type
120
+ }
121
+ response = await self.request("sendLocation", data)
122
+ return MessageId.from_dict(response)
123
+
124
+ async def send_contact(
125
+ self,
126
+ chat_id: str,
127
+ first_name: str,
128
+ last_name: str,
129
+ phone_number: str,
130
+ chat_keypad: Keypad = None,
131
+ inline_keypad: Keypad = None,
132
+ disable_notification: bool = False,
133
+ reply_to_message_id: str = None,
134
+ chat_keypad_type: Literal["New", "Remove"] = None
135
+ ) -> "MessageId":
136
+ data = {
137
+ "chat_id": chat_id,
138
+ "first_name": first_name,
139
+ "last_name": last_name,
140
+ "phone_number": phone_number,
141
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
142
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
143
+ "disable_notification": disable_notification,
144
+ "reply_to_message_id": reply_to_message_id,
145
+ "chat_keypad_type": chat_keypad_type
146
+ }
147
+ response = await self.request("sendContact", data)
148
+ return MessageId.from_dict(response)
149
+
150
+ async def send_sticker(
151
+ self,
152
+ chat_id: str,
153
+ sticker_id: str,
154
+ chat_keypad: Keypad = None,
155
+ inline_keypad: Keypad = None,
156
+ disable_notification: bool = False,
157
+ reply_to_message_id: str = None,
158
+ chat_keypad_type: Literal["New", "Remove"] = None,
159
+ ) -> "MessageId":
160
+ data = {
161
+ "chat_id": chat_id,
162
+ "sticker_id": sticker_id,
163
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
164
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
165
+ "disable_notification": disable_notification,
166
+ "reply_to_message_id": reply_to_message_id,
167
+ "chat_keypad_type": chat_keypad_type
168
+ }
169
+ response = await self.request("sendSticker", data)
170
+ return MessageId.from_dict(response)
171
+
172
+ async def request_send_file(self, type: str):
173
+ response = await self.request("requestSendFile", {"type": type})
174
+ return response["upload_url"]
175
+
176
+ async def upload_file(self, file: str, name: str, type: str):
177
+ upload_url = await self.request_send_file(type)
178
+ response = await self.request_upload_file(upload_url, file, name)
179
+ return response
180
+
181
+
182
+ async def download_file(self, file_id: str, file_name: str):
183
+ download_url = await self.get_file(file_id)
184
+ response = await self.request_download_file(download_url, file_name)
185
+ return response
186
+
187
+ async def send_file(
188
+ self,
189
+ chat_id: str,
190
+ file: str,
191
+ file_name: str,
192
+ type: Literal["File", "Image", "Voice", "Music", "Gif", "Video"] = "File",
193
+ chat_keypad: Keypad = None,
194
+ inline_keypad: Keypad = None,
195
+ chat_keypad_type: Literal["New", "Remove"] = None,
196
+ disable_notification: bool = False,
197
+ reply_to_message_id: str = None,
198
+ ) -> "MessageId":
199
+ file_id = await self.upload_file(file, file_name, type)
200
+ data = {
201
+ "chat_id": chat_id,
202
+ "file_id": file_id,
203
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
204
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
205
+ "disable_notification": disable_notification,
206
+ "reply_to_message_id": reply_to_message_id,
207
+ "chat_keypad_type": chat_keypad_type,
208
+ }
209
+ response = await self.request("sendFile", data)
210
+ return MessageId.from_dict(response)
211
+
212
+
213
+ async def send_document(
214
+ self,
215
+ chat_id: str,
216
+ document: str,
217
+ name: str,
218
+ chat_keypad: Keypad = None,
219
+ inline_keypad: Keypad = None,
220
+ chat_keypad_type: Literal["New", "Remove"] = None,
221
+ disable_notification: bool = False,
222
+ reply_to_message_id: str = None,
223
+ ) -> "MessageId":
224
+ file_id = await self.upload_file(document, name, "File")
225
+ data = {
226
+ "chat_id": chat_id,
227
+ "file_id": file_id,
228
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
229
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
230
+ "disable_notification": disable_notification,
231
+ "reply_to_message_id": reply_to_message_id,
232
+ "chat_keypad_type": chat_keypad_type,
233
+ }
234
+ response = await self.request("sendFile", data)
235
+ return MessageId.from_dict(response)
236
+
237
+ async def send_photo(
238
+ self,
239
+ chat_id: str,
240
+ photo: str,
241
+ name: str,
242
+ chat_keypad: Keypad = None,
243
+ inline_keypad: Keypad = None,
244
+ chat_keypad_type: Literal["New", "Remove"] = None,
245
+ disable_notification: bool = False,
246
+ reply_to_message_id: str = None,
247
+ ) -> "MessageId":
248
+ file_id = await self.upload_file(photo, name, "Image")
249
+ data = {
250
+ "chat_id": chat_id,
251
+ "file_id": file_id,
252
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
253
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
254
+ "disable_notification": disable_notification,
255
+ "reply_to_message_id": reply_to_message_id,
256
+ "chat_keypad_type": chat_keypad_type,
257
+ }
258
+ response = await self.request("sendFile", data)
259
+ return MessageId.from_dict(response)
260
+
261
+ async def send_video(
262
+ self,
263
+ chat_id: str,
264
+ video: str,
265
+ name: str,
266
+ chat_keypad: Keypad = None,
267
+ inline_keypad: Keypad = None,
268
+ chat_keypad_type: Literal["New", "Remove"] = None,
269
+ disable_notification: bool = False,
270
+ reply_to_message_id: str = None,
271
+ ) -> "MessageId":
272
+ file_id = await self.upload_file(video, name, "Video")
273
+ data = {
274
+ "chat_id": chat_id,
275
+ "file_id": file_id,
276
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
277
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
278
+ "disable_notification": disable_notification,
279
+ "reply_to_message_id": reply_to_message_id,
280
+ "chat_keypad_type": chat_keypad_type,
281
+ }
282
+ response = await self.request("sendFile", data)
283
+ return MessageId.from_dict(response)
284
+
285
+ async def send_gif(
286
+ self,
287
+ chat_id: str,
288
+ gif: str,
289
+ name: str,
290
+ chat_keypad: Keypad = None,
291
+ inline_keypad: Keypad = None,
292
+ chat_keypad_type: Literal["New", "Remove"] = None,
293
+ disable_notification: bool = False,
294
+ reply_to_message_id: str = None,
295
+ ) -> "MessageId":
296
+ file_id = await self.upload_file(gif, name, "Gif")
297
+ data = {
298
+ "chat_id": chat_id,
299
+ "file_id": file_id,
300
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
301
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
302
+ "disable_notification": disable_notification,
303
+ "reply_to_message_id": reply_to_message_id,
304
+ "chat_keypad_type": chat_keypad_type,
305
+ }
306
+ response = await self.request("sendFile", data)
307
+ return MessageId.from_dict(response)
308
+
309
+ async def send_music(
310
+ self,
311
+ chat_id: str,
312
+ music: str,
313
+ name: str,
314
+ chat_keypad: Keypad = None,
315
+ inline_keypad: Keypad = None,
316
+ chat_keypad_type: Literal["New", "Remove"] = None,
317
+ disable_notification: bool = False,
318
+ reply_to_message_id: str = None,
319
+ ) -> "MessageId":
320
+ file_id = await self.upload_file(music, name, "Music")
321
+ data = {
322
+ "chat_id": chat_id,
323
+ "file_id": file_id,
324
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
325
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
326
+ "disable_notification": disable_notification,
327
+ "reply_to_message_id": reply_to_message_id,
328
+ "chat_keypad_type": chat_keypad_type,
329
+ }
330
+ response = await self.request("sendFile", data)
331
+ return MessageId.from_dict(response)
332
+
333
+ async def send_voice(
334
+ self,
335
+ chat_id: str,
336
+ voice: str,
337
+ name: str,
338
+ chat_keypad: Keypad = None,
339
+ inline_keypad: Keypad = None,
340
+ chat_keypad_type: Literal["New", "Remove"] = None,
341
+ disable_notification: bool = False,
342
+ reply_to_message_id: str = None,
343
+ ) -> "MessageId":
344
+ file_id = await self.upload_file(voice, name, "Voice")
345
+ data = {
346
+ "chat_id": chat_id,
347
+ "file_id": file_id,
348
+ "chat_keypad": chat_keypad.to_dict() if chat_keypad else None,
349
+ "inline_keypad": inline_keypad.to_dict() if inline_keypad else None,
350
+ "disable_notification": disable_notification,
351
+ "reply_to_message_id": reply_to_message_id,
352
+ "chat_keypad_type": chat_keypad_type,
353
+ }
354
+ response = await self.request("sendFile", data)
355
+ return MessageId.from_dict(response)
@@ -0,0 +1,58 @@
1
+ from aiohttp import ClientSession, FormData
2
+ from typing import Any, Optional, Dict, Union
3
+ import aiofiles
4
+ import re
5
+ import os
6
+
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)
41
+
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
+
46
+ form = FormData()
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}
@@ -0,0 +1,30 @@
1
+ from typing import Any, Union, Optional
2
+
3
+ class StateManager:
4
+ def __init__(self):
5
+ self.states: dict[str, Any] = {}
6
+ self.datas: dict[str, dict[str, Any]] = {}
7
+
8
+ def set_state(self, user_id: str, state: Any):
9
+ self.states[user_id] = state
10
+
11
+ def get_state(self, user_id: str):
12
+ return self.states.get(user_id)
13
+
14
+ def clear_state(self, user_id: str):
15
+ self.states.pop(user_id, None)
16
+
17
+ def set_data(self, user_id: str, **data):
18
+ if user_id not in self.datas:
19
+ self.datas[user_id] = {}
20
+ self.datas[user_id].update(data)
21
+
22
+ def get_data(self, user_id: str, key: Optional[Union[str, int]] = None):
23
+ if key is not None:
24
+ return self.datas.get(user_id, {}).get(key)
25
+ return self.datas.get(user_id)
26
+
27
+ def clear_data(self, user_id: str):
28
+ self.datas.pop(user_id, None)
29
+
30
+ state_manager = StateManager()