Skip to content

Commit 75c0dae

Browse files
committed
More friendly
1 parent 43552aa commit 75c0dae

File tree

12 files changed

+329
-102
lines changed

12 files changed

+329
-102
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = telegrambots-custom
3-
version = 0.0.6-preview
3+
version = 0.0.6rc1
44
author = immmdreza
55
author_email = [email protected]
66
description = A custom extension packages for telegrambots.

src/telegrambots/custom/contexts/_contexts/callback_query_context.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515

1616
class CallbackQueryContext(GenericContext[CallbackQuery]):
1717
def __init__(
18-
self, dp: "Dispatcher", update: Update, handler_tag: str, **kwargs: Any
18+
self,
19+
dp: "Dispatcher",
20+
update: Update,
21+
handler_tag: str,
22+
*args: Any,
23+
**kwargs: Any,
1924
) -> None:
2025
super().__init__(
2126
dp,
22-
update=update,
23-
update_type=CallbackQuery,
24-
handler_tag=handler_tag,
25-
**kwargs
27+
update,
28+
CallbackQuery,
29+
handler_tag,
30+
*args,
31+
**kwargs,
2632
)
2733

2834
@final
@@ -32,13 +38,31 @@ def __extractor__(self, update: Update) -> CallbackQuery:
3238
return c
3339
raise ValueError("Update has no callback query")
3440

41+
@final
42+
@property
43+
def data(self) -> Optional[str]:
44+
"""Returns the data of the callback query."""
45+
return self.update.data
46+
47+
@final
48+
@property
49+
def message(self):
50+
"""The message of callback query."""
51+
return self.update.message
52+
53+
@final
54+
@property
55+
def from_user(self):
56+
"""The sender of callback query."""
57+
return self.update.from_user
58+
3559
async def answer(
3660
self,
3761
text: str,
3862
*,
3963
show_alert: bool = False,
4064
url: Optional[str] = None,
41-
cache_time: int = 0
65+
cache_time: int = 0,
4266
) -> None:
4367
"""Answers a callback query.
4468

src/telegrambots/custom/contexts/_contexts/context_template.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ def __init__(
2727
update: Update,
2828
update_type: type[Any],
2929
handler_tag: str,
30+
*args: Any,
31+
**kwargs: Any,
3032
):
3133
self.__dp = dp
3234
self.__update = update
3335
self.__update_type = update_type
3436
self.__handler_tag = handler_tag
37+
self.__args = args
38+
self.__kwargs = kwargs
3539

3640
# extensions
3741
self.__propagation: Optional[PropagationExtension] = None
@@ -66,6 +70,16 @@ def handler_tag(self) -> str:
6670
def wrapper_update(self) -> Update:
6771
return self.__update
6872

73+
@final
74+
@property
75+
def args(self):
76+
return self.__args
77+
78+
@final
79+
@property
80+
def kwargs(self):
81+
return self.__kwargs
82+
6983
@final
7084
@property
7185
def propagation(self) -> PropagationExtension:
@@ -92,9 +106,10 @@ def __init__(
92106
update: Update,
93107
update_type: type[Any],
94108
handler_tag: str,
109+
*args: Any,
95110
**kwargs: Any,
96111
) -> None:
97-
super().__init__(dp, update, update_type, handler_tag)
112+
super().__init__(dp, update, update_type, handler_tag, *args, **kwargs)
98113
self._metadata: dict[str, Any] = kwargs
99114

100115
def __getitem__(self, name: str):
@@ -124,9 +139,10 @@ def __init__(
124139
update: Update,
125140
update_type: type[Any],
126141
handler_tag: str,
142+
*args: Any,
127143
**kwargs: Any,
128144
) -> None:
129-
super().__init__(dp, update, update_type, handler_tag, **kwargs)
145+
super().__init__(dp, update, update_type, handler_tag, *args, **kwargs)
130146
self.__extractor = _exctractor
131147

132148
@final

src/telegrambots/custom/contexts/_contexts/message_context.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@
1818

1919
class MessageContext(GenericContext[Message]):
2020
def __init__(
21-
self, dp: "Dispatcher", update: Update, handler_tag: str, **kwargs: Any
21+
self,
22+
dp: "Dispatcher",
23+
update: Update,
24+
handler_tag: str,
25+
*args: Any,
26+
**kwargs: Any,
2227
) -> None:
2328
super().__init__(
24-
dp, update=update, update_type=Message, handler_tag=handler_tag, **kwargs
29+
dp,
30+
update,
31+
Message,
32+
handler_tag,
33+
*args,
34+
**kwargs,
2535
)
2636

2737
@final

src/telegrambots/custom/dispatcher.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def handler_tag_exists(self, tag: str, update_type: type[Any]):
101101
return False
102102
return tag in self._handlers[update_type]
103103

104-
def add_handler(self, tag: str, handler: HandlerTemplate):
104+
def add_handler(self, handler: HandlerTemplate):
105105
"""Adds a handler to the dispatcher.
106106
107107
Args:
@@ -114,11 +114,13 @@ def add_handler(self, tag: str, handler: HandlerTemplate):
114114
)
115115
self._handlers[handler.update_type] = {}
116116

117-
if tag in self._handlers[handler.update_type]:
118-
raise HandlerRegistered(tag, handler.update_type)
117+
if handler.tag in self._handlers[handler.update_type]:
118+
raise HandlerRegistered(handler.tag, handler.update_type)
119119

120-
self._handlers[handler.update_type][tag] = handler
121-
dispatcher_logger.info(f"Added handler {handler.update_type.__name__}:{tag}")
120+
self._handlers[handler.update_type][handler.tag] = handler
121+
dispatcher_logger.info(
122+
f"Added handler {handler.update_type.__name__}:{handler.tag}"
123+
)
122124

123125
def add_exception_handler(self, exception_handler: AbstractExceptionHandler):
124126
"""Adds an exception handler to the dispatcher.
@@ -201,6 +203,7 @@ async def _process_update(self, update: Update):
201203
result.metadata,
202204
*c.args,
203205
**c.kwargs,
206+
continue_with_key=c.keys,
204207
)
205208
self._continuously_handlers.remove(batch)
206209
return # Don't process the update anymore
@@ -241,7 +244,6 @@ async def _do_handling(
241244
await handler.process(
242245
self,
243246
update,
244-
handler_tag,
245247
filter_data,
246248
*args,
247249
**kwargs,

src/telegrambots/custom/extensions/context.py

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def callback_query(
6464
filter: Filter[CallbackQuery],
6565
tag: Optional[str] = None,
6666
other_continue_with: Optional[list[str]] = None,
67+
allow_continue_after_self: bool = False,
6768
*args: Any,
6869
**kwargs: Any,
6970
):
@@ -77,8 +78,11 @@ def decorator(
7778
_function,
7879
filter,
7980
[self._context.handler_tag] + (other_continue_with or []),
81+
allow_continue_after_self,
8082
)
81-
self._context.continue_with.callback_query(_tag, keys, *args, **kwargs)
83+
self._context.continue_with.callback_query(
84+
_tag, keys, *args, **(self._context.kwargs | kwargs)
85+
)
8286

8387
return decorator
8488

@@ -88,6 +92,7 @@ def callback_query_form(
8892
filter: Filter[CallbackQuery],
8993
tag: Optional[str] = None,
9094
other_continue_with: Optional[list[str]] = None,
95+
allow_continue_after_self: bool = False,
9196
*args: Any,
9297
**kwargs: Any,
9398
):
@@ -101,9 +106,13 @@ def decorator(
101106
_function,
102107
filter,
103108
[self._context.handler_tag] + (other_continue_with or []),
109+
allow_continue_after_self,
104110
)
105111
self._context.continue_with.callback_query(
106-
_tag, [CallbackQuerySenderId(user_id)], *args, **kwargs
112+
_tag,
113+
[CallbackQuerySenderId(user_id)],
114+
*args,
115+
**(self._context.kwargs | kwargs),
107116
)
108117

109118
return decorator
@@ -115,6 +124,7 @@ def callback_query_same_message_form(
115124
filter: Filter[CallbackQuery],
116125
tag: Optional[str] = None,
117126
other_continue_with: Optional[list[str]] = None,
127+
allow_continue_after_self: bool = False,
118128
*args: Any,
119129
**kwargs: Any,
120130
):
@@ -128,12 +138,13 @@ def decorator(
128138
_function,
129139
filter,
130140
[self._context.handler_tag] + (other_continue_with or []),
141+
allow_continue_after_self,
131142
)
132143
self._context.continue_with.callback_query(
133144
_tag,
134145
[CallbackQuerySenderId(user_id), CallbackQueryMessageId(message_id)],
135146
*args,
136-
**kwargs,
147+
**(self._context.kwargs | kwargs),
137148
)
138149

139150
return decorator
@@ -144,6 +155,7 @@ def callback_query_same_message(
144155
filter: Filter[CallbackQuery],
145156
tag: Optional[str] = None,
146157
other_continue_with: Optional[list[str]] = None,
158+
allow_continue_after_self: bool = False,
147159
*args: Any,
148160
**kwargs: Any,
149161
):
@@ -157,12 +169,13 @@ def decorator(
157169
_function,
158170
filter,
159171
[self._context.handler_tag] + (other_continue_with or []),
172+
allow_continue_after_self,
160173
)
161174
self._context.continue_with.callback_query(
162175
_tag,
163176
[CallbackQueryMessageId(message_id)],
164177
*args,
165-
**kwargs,
178+
**(self._context.kwargs | kwargs),
166179
)
167180

168181
return decorator
@@ -173,6 +186,7 @@ def message(
173186
filter: "Filter[Message]",
174187
tag: Optional[str] = None,
175188
other_continue_with: Optional[list[str]] = None,
189+
allow_continue_after_self: bool = False,
176190
*args: Any,
177191
**kwargs: Any,
178192
):
@@ -186,8 +200,11 @@ def decorator(
186200
_function,
187201
filter,
188202
[self._context.handler_tag] + (other_continue_with or []),
203+
allow_continue_after_self,
189204
)
190-
self._context.continue_with.message(_tag, keys, *args, **kwargs)
205+
self._context.continue_with.message(
206+
_tag, keys, *args, **(self._context.kwargs | kwargs)
207+
)
191208

192209
return decorator
193210

@@ -197,6 +214,7 @@ def message_from(
197214
filter: "Filter[Message]",
198215
tag: Optional[str] = None,
199216
other_continue_with: Optional[list[str]] = None,
217+
allow_continue_after_self: bool = False,
200218
*args: Any,
201219
**kwargs: Any,
202220
):
@@ -210,9 +228,13 @@ def decorator(
210228
_function,
211229
filter,
212230
[self._context.handler_tag] + (other_continue_with or []),
231+
allow_continue_after_self,
213232
)
214233
self._context.continue_with.message(
215-
_tag, [MessageSenderId(user_id)], *args, **kwargs
234+
_tag,
235+
[MessageSenderId(user_id)],
236+
*args,
237+
**(self._context.kwargs | kwargs),
216238
)
217239

218240
return decorator
@@ -222,6 +244,7 @@ def text_input_from(
222244
user_id: int,
223245
tag: Optional[str] = None,
224246
other_continue_with: Optional[list[str]] = None,
247+
allow_continue_after_self: bool = False,
225248
*args: Any,
226249
**kwargs: Any,
227250
):
@@ -232,12 +255,16 @@ def decorator(
232255
if not self._context.dp.handler_tag_exists(_tag, Message):
233256
self._context.dp.add.handlers.message(
234257
_tag,
235-
_function, # type: ignore
258+
_function,
236259
mf.text_message,
237260
[self._context.handler_tag] + (other_continue_with or []),
261+
allow_continue_after_self,
238262
)
239263
self._context.continue_with.message(
240-
_tag, [MessageSenderId(user_id)], *args, **kwargs
264+
_tag,
265+
[MessageSenderId(user_id)],
266+
*args,
267+
**(self._context.kwargs | kwargs),
241268
)
242269

243270
return decorator
@@ -344,3 +371,30 @@ def callback_query(
344371
*args,
345372
**kwargs,
346373
)
374+
375+
def self(
376+
self,
377+
keys: Sequence[AbstractKeyResolver[CallbackQuery, Any]],
378+
*args: Any,
379+
**kwargs: Any,
380+
) -> NoReturn:
381+
self.any(
382+
self._context.handler_tag,
383+
self._context.update_type,
384+
keys,
385+
*args,
386+
**kwargs,
387+
)
388+
389+
def self_shared_keys(
390+
self,
391+
*args: Any,
392+
**kwargs: Any,
393+
) -> NoReturn:
394+
self.any(
395+
self._context.handler_tag,
396+
self._context.update_type,
397+
self._context.kwargs["continue_with_key"],
398+
*args,
399+
**kwargs,
400+
)

0 commit comments

Comments
 (0)