RubigramClient 1.3.9__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.

@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.4
2
+ Name: RubigramClient
3
+ Version: 1.4.1
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,55 @@
1
+ # Rubigram
2
+ A lightweight Python library to build Rubika bots easily.
3
+
4
+ ## Installation
5
+ ```bash
6
+ pip install RubigramClient
7
+ ```
8
+
9
+ ## Send Message
10
+ ```python
11
+ from rubigram import Client, filters
12
+ from rubigram.types import Update
13
+
14
+ bot = Client("your_bot_token", "you_endpoint_url")
15
+
16
+ @bot.on_message(filters.command("start"))
17
+ async def start_handler(client, message: Update):
18
+ await message.reply("Hi, WELCOME TO RUBIGRAM")
19
+
20
+ bot.run()
21
+ ```
22
+
23
+ ## Send Message & Get receiveInlineMessage
24
+ ```python
25
+ from rubigram import Client, filters
26
+ from rubigram.types import Update, Button, Keypad, KeypadRow, InlineMessage
27
+
28
+
29
+ bot = Client(token="bot_token", endpoint="endpoint_url")
30
+
31
+
32
+ @bot.on_message(filters.command("start"))
33
+ async def start(_, message: Update):
34
+ inline = Keypad(
35
+ rows=[
36
+ KeypadRow(
37
+ buttons=[
38
+ Button("1", "Simple", "Button 1"),
39
+ Button("2", "Simple", "Button 2")
40
+ ]
41
+ )
42
+ ]
43
+ )
44
+ await bot.send_message(message.chat_id, "Hi", inline_keypad=inline)
45
+
46
+
47
+ @bot.on_inline_message(filters.button(["1", "2"]))
48
+ async def button(_, message: InlineMessage):
49
+ if message.aux_data.button_id == "1":
50
+ await bot.send_message(message.chat_id, "You Click Button 1")
51
+ elif message.aux_data.button_id == "2":
52
+ await bot.send_message(message.chat_id, "You Click Button 2")
53
+
54
+ bot.run()
55
+ ```
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.4
2
+ Name: RubigramClient
3
+ Version: 1.4.1
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
+ ```
@@ -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.3.9"
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
@@ -0,0 +1,92 @@
1
+ from __future__ import annotations
2
+ from typing import Callable, Awaitable, Optional
3
+ from aiohttp import web
4
+ from functools import wraps
5
+ from rubigram.types import Update, InlineMessage
6
+ from rubigram.method import Method
7
+ import asyncio
8
+ import logging
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ class Client(Method):
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
+ ):
21
+ self.token = token
22
+ self.port = port
23
+ self.host = host
24
+ self.endpoint = endpoint
25
+ self.messages_handler: list[Callable[[Client, Update], Awaitable]] = []
26
+ self.inlines_handler: list[Callable[[Client, InlineMessage], Awaitable]] = []
27
+ self.routes = web.RouteTableDef()
28
+ super().__init__(token)
29
+
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}")
39
+ self.messages_handler.append(wrapper)
40
+ return func
41
+ return decorator
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}")
52
+ self.inlines_handler.append(wrapper)
53
+ return func
54
+ return decorator
55
+
56
+ async def handle_update(self, data: dict):
57
+ if "inline_message" in data:
58
+ event = InlineMessage.from_dict(data["inline_message"])
59
+ await asyncio.gather(*(h(self, event) for h in self.inlines_handler))
60
+ elif "update" in data:
61
+ event = Update.from_dict(data["update"])
62
+ event.client = self
63
+ await asyncio.gather(*(h(self, event) for h in self.messages_handler))
64
+
65
+
66
+ async def set_endpoints(self):
67
+ if not self.endpoint:
68
+ return
69
+ await self.update_bot_endpoint(f"{self.endpoint}/ReceiveUpdate", "ReceiveUpdate")
70
+ await self.update_bot_endpoint(f"{self.endpoint}/ReceiveInlineMessage", "ReceiveInlineMessage")
71
+
72
+ def run(self):
73
+ @self.routes.post("/ReceiveUpdate")
74
+ async def receive_update(request: web.Request):
75
+ data = await request.json()
76
+ await self.handle_update(data)
77
+ return web.json_response({"status": "OK"})
78
+
79
+ @self.routes.post("/ReceiveInlineMessage")
80
+ async def receive_inline_message(request: web.Request):
81
+ data = await request.json()
82
+ await self.handle_update(data)
83
+ return web.json_response({"status": "OK"})
84
+
85
+ app = web.Application()
86
+ app.add_routes(self.routes)
87
+
88
+ async def on_startup(_):
89
+ await self.set_endpoints()
90
+
91
+ app.on_startup.append(on_startup)
92
+ web.run_app(app, host=self.host, port=self.port)
@@ -1,14 +1,29 @@
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 edited():
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.updated_message:
16
+ return message.updated_message.is_edited
17
+ return False
18
+ return filter
19
+
20
+ def command(commands: Union[str, list[str]], prefix: str = "/"):
21
+ def filter(message: Update):
22
+ if isinstance(message, Update) and message.new_message and message.new_message.text:
23
+ text = message.new_message.text.strip()
24
+ cmds = commands if isinstance(commands, list) else [commands]
25
+ for cmd in cmds:
26
+ if text.lower().startswith(prefix + cmd.lower()):
12
27
  return True
13
28
  return False
14
29
  return filter
@@ -62,6 +77,7 @@ def private():
62
77
  return False
63
78
  return filter
64
79
 
80
+
65
81
  def forward():
66
82
  def filter(message: Update):
67
83
  if isinstance(message, Update) and message.type == "NewMessage":
@@ -69,6 +85,30 @@ def forward():
69
85
  return False
70
86
  return filter
71
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
+
72
112
  def location():
73
113
  def filter(message: Update):
74
114
  if isinstance(message, Update) and message.type == "NewMessage":