RubigramClient 1.3.9__py3-none-any.whl → 1.4.0__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/client.py CHANGED
@@ -1,72 +1,90 @@
1
+ from __future__ import annotations
2
+ from typing import Callable, Awaitable, Optional
3
+ from aiohttp import web
4
+ from functools import wraps
1
5
  from rubigram.types import Update, InlineMessage
2
6
  from rubigram.method import Method
3
- from aiohttp import web
7
+ import asyncio
8
+ import logging
9
+
10
+ logger = logging.getLogger(__name__)
4
11
 
5
12
 
6
13
  class Client(Method):
7
- def __init__(self, token: str, endpoint: str = None, host: str = "0.0.0.0", port: int = 8000):
14
+ def __init__(
15
+ self,
16
+ token: str,
17
+ endpoint: Optional[str] = None,
18
+ host: str = "0.0.0.0",
19
+ port: int = 8000
20
+ ):
8
21
  self.token = token
9
22
  self.port = port
10
23
  self.host = host
11
24
  self.endpoint = endpoint
12
- self.messages_handler = []
13
- self.inlines_handler = []
25
+ self.messages_handler: list[Callable[[Client, Update], Awaitable]] = []
26
+ self.inlines_handler: list[Callable[[Client, InlineMessage], Awaitable]] = []
14
27
  self.routes = web.RouteTableDef()
15
- self.api = f"https://botapi.rubika.ir/v3/{self.token}/"
16
28
  super().__init__(token)
17
29
 
18
-
19
- def on_message(self, *filters):
20
- def decorator(func):
21
- async def wrapper(client, update):
22
- if all(f(update) for f in filters):
23
- await func(client, update)
30
+ def on_message(self, *filters: Callable[[Update], bool]):
31
+ def decorator(func: Callable[[Client, Update], Awaitable]):
32
+ @wraps(func)
33
+ async def wrapper(client: Client, update: Update):
34
+ try:
35
+ if all(f(update) for f in filters):
36
+ await func(client, update)
37
+ except Exception as e:
38
+ logger.exception(f"Error in message handler {func.__name__}: {e}")
24
39
  self.messages_handler.append(wrapper)
25
40
  return func
26
41
  return decorator
27
-
28
- def on_inline_message(self, *filters):
29
- def decorator(func):
30
- async def wrapper(client, update):
31
- if all(f(update) for f in filters):
32
- await func(client, update)
42
+
43
+ def on_inline_message(self, *filters: Callable[[InlineMessage], bool]):
44
+ def decorator(func: Callable[[Client, InlineMessage], Awaitable]):
45
+ @wraps(func)
46
+ async def wrapper(client: Client, update: InlineMessage):
47
+ try:
48
+ if all(f(update) for f in filters):
49
+ await func(client, update)
50
+ except Exception as e:
51
+ logger.exception(f"Error in inline handler {func.__name__}: {e}")
33
52
  self.inlines_handler.append(wrapper)
34
53
  return func
35
54
  return decorator
36
-
37
- async def update(self, data: dict):
55
+
56
+ async def handle_update(self, data: dict):
38
57
  if "inline_message" in data:
39
58
  event = InlineMessage.read(data["inline_message"])
40
- for handler in self.inlines_handler:
41
- await handler(self, event)
42
- else:
59
+ await asyncio.gather(*(h(self, event) for h in self.inlines_handler))
60
+ elif "update" in data:
43
61
  event = Update.read(data["update"], self)
44
- for handler in self.messages_handler:
45
- await handler(self, event)
46
-
62
+ await asyncio.gather(*(h(self, event) for h in self.messages_handler))
63
+
47
64
  async def set_endpoints(self):
48
- await self.update_bot_endpoint(self.endpoint + "/ReceiveUpdate", "ReceiveUpdate")
49
- await self.update_bot_endpoint(self.endpoint + "/ReceiveInlineMessage", "ReceiveInlineMessage")
65
+ if not self.endpoint:
66
+ return
67
+ await self.update_bot_endpoint(f"{self.endpoint}/ReceiveUpdate", "ReceiveUpdate")
68
+ await self.update_bot_endpoint(f"{self.endpoint}/ReceiveInlineMessage", "ReceiveInlineMessage")
50
69
 
51
70
  def run(self):
52
71
  @self.routes.post("/ReceiveUpdate")
53
- async def receive_update(request):
72
+ async def receive_update(request: web.Request):
54
73
  data = await request.json()
55
- await self.update(data)
74
+ await self.handle_update(data)
56
75
  return web.json_response({"status": "OK"})
57
76
 
58
77
  @self.routes.post("/ReceiveInlineMessage")
59
- async def receive_inline_message(request):
78
+ async def receive_inline_message(request: web.Request):
60
79
  data = await request.json()
61
- await self.update(data)
62
- return web.json_response({"status": "ok"})
80
+ await self.handle_update(data)
81
+ return web.json_response({"status": "OK"})
63
82
 
64
83
  app = web.Application()
65
84
  app.add_routes(self.routes)
66
85
 
67
- async def on_startup(app):
68
- if self.endpoint:
69
- await self.set_endpoints()
86
+ async def on_startup(_):
87
+ await self.set_endpoints()
70
88
 
71
89
  app.on_startup.append(on_startup)
72
- web.run_app(app, host = self.host, port = self.port)
90
+ web.run_app(app, host=self.host, port=self.port)
rubigram/filters.py CHANGED
@@ -1,14 +1,22 @@
1
1
  from rubigram.types import Update, InlineMessage
2
+ # from rubigram.state import state_manager
2
3
  from typing import Union
3
4
  import re
4
5
 
5
- def command(commands: Union[str, list[str]], prefixe: str = "/"):
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
12
+
13
+ def command(commands: Union[str, list[str]], prefix: str = "/"):
6
14
  def filter(message: Update):
7
- if isinstance(message, Update) and message.type == "NewMessage" and message.new_message.text:
8
- text = message.new_message.text
9
- COMMANDS = commands if isinstance(commands, list) else [commands]
10
- for cmd in COMMANDS:
11
- if text.lower().startswith(prefixe + cmd):
15
+ if isinstance(message, Update) and message.new_message and message.new_message.text:
16
+ text = message.new_message.text.strip()
17
+ cmds = commands if isinstance(commands, list) else [commands]
18
+ for cmd in cmds:
19
+ if text.lower().startswith(prefix + cmd.lower()):
12
20
  return True
13
21
  return False
14
22
  return filter
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.4
2
+ Name: RubigramClient
3
+ Version: 1.4.0
4
+ Summary: A simple and flexible Python library for building advanced Rubika bots with powerful message handling, inline buttons, and custom filters.
5
+ Author-email: Javad RZ <Javad.Py1385@gmail.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.7
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: aiohttp
13
+ Requires-Dist: aiofiles
14
+ Dynamic: license-file
15
+
16
+ # Rubigram
17
+ A lightweight Python library to build Rubika bots easily.
18
+
19
+ ## Installation
20
+ ```bash
21
+ pip install RubigramClient
22
+ ```
23
+
24
+ ## Send Message
25
+ ```python
26
+ from rubigram import Client, filters
27
+ from rubigram.types import Update
28
+
29
+ bot = Client("your_bot_token", "you_endpoint_url")
30
+
31
+ @bot.on_message(filters.command("start"))
32
+ async def start_handler(client, message: Update):
33
+ await message.reply("Hi, WELCOME TO RUBIGRAM")
34
+
35
+ bot.run()
36
+ ```
37
+
38
+ ## Send Message & Get receiveInlineMessage
39
+ ```python
40
+ from rubigram import Client, filters
41
+ from rubigram.types import Update, Button, Keypad, KeypadRow, InlineMessage
42
+
43
+
44
+ bot = Client(token="bot_token", endpoint="endpoint_url")
45
+
46
+
47
+ @bot.on_message(filters.command("start"))
48
+ async def start(_, message: Update):
49
+ inline = Keypad(
50
+ rows=[
51
+ KeypadRow(
52
+ buttons=[
53
+ Button("1", "Simple", "Button 1"),
54
+ Button("2", "Simple", "Button 2")
55
+ ]
56
+ )
57
+ ]
58
+ )
59
+ await bot.send_message(message.chat_id, "Hi", inline_keypad=inline)
60
+
61
+
62
+ @bot.on_inline_message(filters.button(["1", "2"]))
63
+ async def button(_, message: InlineMessage):
64
+ if message.aux_data.button_id == "1":
65
+ await bot.send_message(message.chat_id, "You Click Button 1")
66
+ elif message.aux_data.button_id == "2":
67
+ await bot.send_message(message.chat_id, "You Click Button 2")
68
+
69
+ bot.run()
70
+ ```
@@ -0,0 +1,11 @@
1
+ rubigram/__init__.py,sha256=4JZ6q8ukflz_izRmJNYcndcKZFCsyJdYoYVoAPwN5V0,107
2
+ rubigram/client.py,sha256=IKyV-9-1WFuKZzspVrJLVok5NVlTTU7RXkW_rFDfHDY,3506
3
+ rubigram/filters.py,sha256=0ocvO9FGDlRAEbmPegsg40Eo6jHj5prfBL-lImTDGXU,4086
4
+ rubigram/method.py,sha256=nm2qfzkanpzDo3yusIXmjYgCYmLzV6nVOyaxPDDqqz4,9074
5
+ rubigram/network.py,sha256=K7vvGikXStvUBqHX2gogy3hJ1eQjLAJ7kdwLbfFuZIg,1399
6
+ rubigram/types.py,sha256=evelh8GGttOHFQjiwXOzwXYFwLXKJSHit60T0T-9VIE,17035
7
+ rubigramclient-1.4.0.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ rubigramclient-1.4.0.dist-info/METADATA,sha256=ulFp24wnkZUbLvxDfy3uR_CDnpZgDRbwbvj4UezmmDo,2035
9
+ rubigramclient-1.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ rubigramclient-1.4.0.dist-info/top_level.txt,sha256=Mhg5HfkL6rLec5sI4ClGmwoqYUANAZUz8sVa1sT_cas,9
11
+ rubigramclient-1.4.0.dist-info/RECORD,,
@@ -1,35 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: RubigramClient
3
- Version: 1.3.9
4
- Summary: A simple and flexible Python library for building advanced Rubika bots with powerful message handling, inline buttons, and custom filters.
5
- Author-email: Javad RZ <Javad.Py1385@gmail.com>
6
- Classifier: Programming Language :: Python :: 3
7
- Classifier: License :: OSI Approved :: MIT License
8
- Classifier: Operating System :: OS Independent
9
- Requires-Python: >=3.7
10
- Description-Content-Type: text/markdown
11
- License-File: LICENSE
12
- Requires-Dist: aiohttp
13
- Requires-Dist: aiofiles
14
- Dynamic: license-file
15
-
16
- # Rubigram
17
- A lightweight Python library to build Rubika bots easily.
18
-
19
- ## Installation
20
- ```bash
21
- pip install RubigramClient
22
- ```
23
- ## Example
24
- ```python
25
- from rubigram import Client, filters
26
- from rubigram.types import Update
27
-
28
- bot = Client("your_bot_token", "you_endpoint_url")
29
-
30
- @bot.on_message(filters.command("start"))
31
- async def start_handler(client, message: Update):
32
- await message.reply("Hi, WELCOME TO RUBIGRAM")
33
-
34
- bot.run()
35
- ```
@@ -1,11 +0,0 @@
1
- rubigram/__init__.py,sha256=4JZ6q8ukflz_izRmJNYcndcKZFCsyJdYoYVoAPwN5V0,107
2
- rubigram/client.py,sha256=7XKqygP1v41yfJ7txTcLLBlOqB8B7Ovqfjo3La373FI,2641
3
- rubigram/filters.py,sha256=ZOP_3Yum41miESITTFCbWI7XElfmKPHqud2mxTo7NdQ,3759
4
- rubigram/method.py,sha256=nm2qfzkanpzDo3yusIXmjYgCYmLzV6nVOyaxPDDqqz4,9074
5
- rubigram/network.py,sha256=K7vvGikXStvUBqHX2gogy3hJ1eQjLAJ7kdwLbfFuZIg,1399
6
- rubigram/types.py,sha256=evelh8GGttOHFQjiwXOzwXYFwLXKJSHit60T0T-9VIE,17035
7
- rubigramclient-1.3.9.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- rubigramclient-1.3.9.dist-info/METADATA,sha256=Qpp5phDEIUsgO7_1-7JCgVffmACSH7SkdPzgAc_4Nb8,1022
9
- rubigramclient-1.3.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
- rubigramclient-1.3.9.dist-info/top_level.txt,sha256=Mhg5HfkL6rLec5sI4ClGmwoqYUANAZUz8sVa1sT_cas,9
11
- rubigramclient-1.3.9.dist-info/RECORD,,