telegrinder 0.1.dev20__py3-none-any.whl → 0.1.dev158__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 telegrinder might be problematic. Click here for more details.
- telegrinder/__init__.py +129 -22
- telegrinder/api/__init__.py +11 -2
- telegrinder/api/abc.py +25 -9
- telegrinder/api/api.py +29 -24
- telegrinder/api/error.py +14 -4
- telegrinder/api/response.py +11 -7
- telegrinder/bot/__init__.py +68 -7
- telegrinder/bot/bot.py +30 -24
- telegrinder/bot/cute_types/__init__.py +11 -1
- telegrinder/bot/cute_types/base.py +47 -0
- telegrinder/bot/cute_types/callback_query.py +64 -14
- telegrinder/bot/cute_types/inline_query.py +22 -16
- telegrinder/bot/cute_types/message.py +145 -53
- telegrinder/bot/cute_types/update.py +23 -0
- telegrinder/bot/dispatch/__init__.py +56 -3
- telegrinder/bot/dispatch/abc.py +9 -7
- telegrinder/bot/dispatch/composition.py +74 -0
- telegrinder/bot/dispatch/context.py +71 -0
- telegrinder/bot/dispatch/dispatch.py +86 -49
- telegrinder/bot/dispatch/handler/__init__.py +3 -0
- telegrinder/bot/dispatch/handler/abc.py +11 -5
- telegrinder/bot/dispatch/handler/func.py +41 -32
- telegrinder/bot/dispatch/handler/message_reply.py +46 -0
- telegrinder/bot/dispatch/middleware/__init__.py +2 -0
- telegrinder/bot/dispatch/middleware/abc.py +10 -4
- telegrinder/bot/dispatch/process.py +53 -49
- telegrinder/bot/dispatch/return_manager/__init__.py +19 -0
- telegrinder/bot/dispatch/return_manager/abc.py +95 -0
- telegrinder/bot/dispatch/return_manager/callback_query.py +19 -0
- telegrinder/bot/dispatch/return_manager/inline_query.py +14 -0
- telegrinder/bot/dispatch/return_manager/message.py +25 -0
- telegrinder/bot/dispatch/view/__init__.py +14 -2
- telegrinder/bot/dispatch/view/abc.py +121 -2
- telegrinder/bot/dispatch/view/box.py +38 -0
- telegrinder/bot/dispatch/view/callback_query.py +13 -39
- telegrinder/bot/dispatch/view/inline_query.py +11 -39
- telegrinder/bot/dispatch/view/message.py +11 -47
- telegrinder/bot/dispatch/waiter_machine/__init__.py +9 -0
- telegrinder/bot/dispatch/waiter_machine/machine.py +116 -0
- telegrinder/bot/dispatch/waiter_machine/middleware.py +76 -0
- telegrinder/bot/dispatch/waiter_machine/short_state.py +37 -0
- telegrinder/bot/polling/__init__.py +2 -0
- telegrinder/bot/polling/abc.py +11 -4
- telegrinder/bot/polling/polling.py +89 -40
- telegrinder/bot/rules/__init__.py +91 -5
- telegrinder/bot/rules/abc.py +81 -63
- telegrinder/bot/rules/adapter/__init__.py +11 -0
- telegrinder/bot/rules/adapter/abc.py +21 -0
- telegrinder/bot/rules/adapter/errors.py +5 -0
- telegrinder/bot/rules/adapter/event.py +43 -0
- telegrinder/bot/rules/adapter/raw_update.py +24 -0
- telegrinder/bot/rules/callback_data.py +159 -38
- telegrinder/bot/rules/command.py +116 -0
- telegrinder/bot/rules/enum_text.py +28 -0
- telegrinder/bot/rules/func.py +17 -17
- telegrinder/bot/rules/fuzzy.py +13 -10
- telegrinder/bot/rules/inline.py +61 -0
- telegrinder/bot/rules/integer.py +12 -7
- telegrinder/bot/rules/is_from.py +148 -7
- telegrinder/bot/rules/markup.py +21 -18
- telegrinder/bot/rules/mention.py +17 -0
- telegrinder/bot/rules/message_entities.py +33 -0
- telegrinder/bot/rules/regex.py +27 -19
- telegrinder/bot/rules/rule_enum.py +74 -0
- telegrinder/bot/rules/start.py +25 -13
- telegrinder/bot/rules/text.py +23 -14
- telegrinder/bot/scenario/__init__.py +2 -0
- telegrinder/bot/scenario/abc.py +12 -5
- telegrinder/bot/scenario/checkbox.py +48 -30
- telegrinder/bot/scenario/choice.py +16 -10
- telegrinder/client/__init__.py +2 -0
- telegrinder/client/abc.py +8 -21
- telegrinder/client/aiohttp.py +30 -21
- telegrinder/model.py +68 -37
- telegrinder/modules.py +189 -21
- telegrinder/msgspec_json.py +14 -0
- telegrinder/msgspec_utils.py +207 -0
- telegrinder/node/__init__.py +31 -0
- telegrinder/node/attachment.py +71 -0
- telegrinder/node/base.py +93 -0
- telegrinder/node/composer.py +71 -0
- telegrinder/node/container.py +22 -0
- telegrinder/node/message.py +18 -0
- telegrinder/node/rule.py +56 -0
- telegrinder/node/source.py +31 -0
- telegrinder/node/text.py +13 -0
- telegrinder/node/tools/__init__.py +3 -0
- telegrinder/node/tools/generator.py +40 -0
- telegrinder/node/update.py +12 -0
- telegrinder/rules.py +1 -1
- telegrinder/tools/__init__.py +165 -4
- telegrinder/tools/buttons.py +75 -51
- telegrinder/tools/error_handler/__init__.py +8 -0
- telegrinder/tools/error_handler/abc.py +30 -0
- telegrinder/tools/error_handler/error_handler.py +156 -0
- telegrinder/tools/formatting/__init__.py +81 -3
- telegrinder/tools/formatting/html.py +283 -37
- telegrinder/tools/formatting/links.py +32 -0
- telegrinder/tools/formatting/spec_html_formats.py +121 -0
- telegrinder/tools/global_context/__init__.py +12 -0
- telegrinder/tools/global_context/abc.py +66 -0
- telegrinder/tools/global_context/global_context.py +451 -0
- telegrinder/tools/global_context/telegrinder_ctx.py +25 -0
- telegrinder/tools/i18n/__init__.py +12 -0
- telegrinder/tools/i18n/base.py +31 -0
- telegrinder/tools/i18n/middleware/__init__.py +3 -0
- telegrinder/tools/i18n/middleware/base.py +26 -0
- telegrinder/tools/i18n/simple.py +48 -0
- telegrinder/tools/inline_query.py +684 -0
- telegrinder/tools/kb_set/__init__.py +2 -0
- telegrinder/tools/kb_set/base.py +3 -0
- telegrinder/tools/kb_set/yaml.py +28 -17
- telegrinder/tools/keyboard.py +84 -62
- telegrinder/tools/loop_wrapper/__init__.py +4 -0
- telegrinder/tools/loop_wrapper/abc.py +18 -0
- telegrinder/tools/loop_wrapper/loop_wrapper.py +132 -0
- telegrinder/tools/magic.py +48 -23
- telegrinder/tools/parse_mode.py +1 -2
- telegrinder/types/__init__.py +1 -0
- telegrinder/types/enums.py +651 -0
- telegrinder/types/methods.py +3920 -1251
- telegrinder/types/objects.py +4702 -1718
- {telegrinder-0.1.dev20.dist-info → telegrinder-0.1.dev158.dist-info}/LICENSE +2 -1
- telegrinder-0.1.dev158.dist-info/METADATA +108 -0
- telegrinder-0.1.dev158.dist-info/RECORD +126 -0
- {telegrinder-0.1.dev20.dist-info → telegrinder-0.1.dev158.dist-info}/WHEEL +1 -1
- telegrinder/bot/dispatch/waiter.py +0 -38
- telegrinder/result.py +0 -38
- telegrinder/tools/formatting/abc.py +0 -52
- telegrinder/tools/formatting/markdown.py +0 -57
- telegrinder-0.1.dev20.dist-info/METADATA +0 -22
- telegrinder-0.1.dev20.dist-info/RECORD +0 -71
|
@@ -1,58 +1,304 @@
|
|
|
1
|
-
|
|
1
|
+
import html
|
|
2
|
+
import string
|
|
2
3
|
import typing
|
|
4
|
+
from contextlib import suppress
|
|
5
|
+
|
|
3
6
|
from telegrinder.tools.parse_mode import ParseMode
|
|
7
|
+
from telegrinder.types.enums import ProgrammingLanguage
|
|
8
|
+
|
|
9
|
+
from .links import (
|
|
10
|
+
get_channel_boost_link,
|
|
11
|
+
get_invite_chat_link,
|
|
12
|
+
get_mention_link,
|
|
13
|
+
get_resolve_domain_link,
|
|
14
|
+
get_start_bot_link,
|
|
15
|
+
get_start_group_link,
|
|
16
|
+
)
|
|
17
|
+
from .spec_html_formats import SpecialFormat, is_spec_format
|
|
4
18
|
|
|
19
|
+
TAG_FORMAT = "<{tag}{data}>{content}</{tag}>"
|
|
5
20
|
QUOT_MARK = '"'
|
|
6
21
|
|
|
7
22
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for k, v in data.items():
|
|
14
|
-
s += f" {k}={v}"
|
|
15
|
-
s += ">" + content + "</" + tag_name + ">"
|
|
16
|
-
return HTMLFormatter(s)
|
|
23
|
+
class StringFormatter(string.Formatter):
|
|
24
|
+
"""String formatter, using substitutions from args and kwargs.
|
|
25
|
+
The substitutions are identified by braces ('{' and '}') with
|
|
26
|
+
specifiers: `bold`, `italic`, etc.
|
|
27
|
+
"""
|
|
17
28
|
|
|
29
|
+
__formats__: typing.ClassVar[tuple[str, ...]] = (
|
|
30
|
+
"blockquote",
|
|
31
|
+
"bold",
|
|
32
|
+
"code_inline",
|
|
33
|
+
"italic",
|
|
34
|
+
"spoiler",
|
|
35
|
+
"strike",
|
|
36
|
+
"underline",
|
|
37
|
+
)
|
|
18
38
|
|
|
19
|
-
|
|
20
|
-
|
|
39
|
+
def is_html_format(self, value: typing.Any, fmt: str) -> str:
|
|
40
|
+
if not fmt:
|
|
41
|
+
raise ValueError("Formats union should be: format+format.")
|
|
42
|
+
if fmt not in self.__formats__:
|
|
43
|
+
raise ValueError(
|
|
44
|
+
"Unknown format {!r} for object of type {!r}.".format(
|
|
45
|
+
fmt,
|
|
46
|
+
type(value).__name__,
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
return fmt
|
|
21
50
|
|
|
22
|
-
def
|
|
23
|
-
return
|
|
24
|
-
|
|
51
|
+
def get_spec_formatter(self, value: SpecialFormat) -> typing.Callable[..., "TagFormat"]:
|
|
52
|
+
return globals()[value.__formatter_name__]
|
|
53
|
+
|
|
54
|
+
def check_formats(self, value: typing.Any, fmts: list[str]) -> "TagFormat":
|
|
55
|
+
if is_spec_format(value):
|
|
56
|
+
value = value.string
|
|
57
|
+
|
|
58
|
+
current_format = globals()[fmts.pop(0)](
|
|
59
|
+
str(value)
|
|
60
|
+
if isinstance(value, TagFormat)
|
|
61
|
+
else escape(FormatString(value) if not isinstance(value, str) else value)
|
|
62
|
+
)
|
|
63
|
+
for fmt in fmts:
|
|
64
|
+
current_format = globals()[fmt](current_format)
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
TagFormat(
|
|
68
|
+
current_format,
|
|
69
|
+
tag=value.tag,
|
|
70
|
+
**value.data,
|
|
71
|
+
)
|
|
72
|
+
if isinstance(value, TagFormat)
|
|
73
|
+
else current_format
|
|
25
74
|
)
|
|
26
75
|
|
|
27
|
-
def
|
|
28
|
-
|
|
76
|
+
def format_field(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
|
|
77
|
+
with suppress(ValueError):
|
|
78
|
+
return HTMLFormatter(
|
|
79
|
+
format(
|
|
80
|
+
value.formatting()
|
|
81
|
+
if isinstance(value, TagFormat)
|
|
82
|
+
else self.get_spec_formatter(value)(**value.__dict__).formatting()
|
|
83
|
+
if is_spec_format(value)
|
|
84
|
+
else value,
|
|
85
|
+
fmt,
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
return self.format_raw_value(value, fmt)
|
|
29
89
|
|
|
30
|
-
def
|
|
31
|
-
|
|
90
|
+
def format_raw_value(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
|
|
91
|
+
fmts = list(map(lambda fmt: self.is_html_format(value, fmt), fmt.split("+")))
|
|
92
|
+
tag_format = self.check_formats(value, fmts)
|
|
32
93
|
|
|
33
|
-
|
|
34
|
-
|
|
94
|
+
if is_spec_format(value):
|
|
95
|
+
value.string = tag_format
|
|
96
|
+
tag_format = self.get_spec_formatter(value)(**value.__dict__)
|
|
35
97
|
|
|
36
|
-
|
|
37
|
-
return wrap_tag("s", self)
|
|
98
|
+
return tag_format.formatting()
|
|
38
99
|
|
|
39
|
-
def
|
|
40
|
-
return
|
|
100
|
+
def format(self, __string: str, *args: object, **kwargs: object) -> "HTMLFormatter":
|
|
101
|
+
return HTMLFormatter(super().format(__string, *args, **kwargs))
|
|
41
102
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
103
|
+
|
|
104
|
+
class FormatString(str):
|
|
105
|
+
string_formatter = StringFormatter()
|
|
106
|
+
|
|
107
|
+
def __new__(cls, string: str) -> typing.Self:
|
|
108
|
+
if isinstance(string, TagFormat):
|
|
109
|
+
return super().__new__(cls, string.formatting())
|
|
110
|
+
return super().__new__(cls, string)
|
|
111
|
+
|
|
112
|
+
def __add__(self, value: str) -> "HTMLFormatter":
|
|
113
|
+
return HTMLFormatter(
|
|
114
|
+
str.__add__(
|
|
115
|
+
escape(self),
|
|
116
|
+
value.formatting() if isinstance(value, TagFormat) else escape(value),
|
|
117
|
+
)
|
|
47
118
|
)
|
|
48
119
|
|
|
49
|
-
def
|
|
50
|
-
|
|
120
|
+
def __radd__(self, value: str) -> "HTMLFormatter":
|
|
121
|
+
"""Return value+self."""
|
|
122
|
+
return HTMLFormatter(FormatString.__add__(FormatString(value), self).as_str())
|
|
123
|
+
|
|
124
|
+
def as_str(self) -> str:
|
|
125
|
+
"""Return self as a standart string."""
|
|
126
|
+
return self.__str__()
|
|
127
|
+
|
|
128
|
+
def format(self, *args: object, **kwargs: object) -> "HTMLFormatter":
|
|
129
|
+
return self.string_formatter.format(self, *args, **kwargs)
|
|
130
|
+
|
|
51
131
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
132
|
+
class EscapedString(FormatString):
|
|
133
|
+
@property
|
|
134
|
+
def former_string(self) -> str:
|
|
135
|
+
return html.unescape(self)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class TagFormat(FormatString):
|
|
139
|
+
tag: str
|
|
140
|
+
data: dict[str, typing.Any]
|
|
141
|
+
|
|
142
|
+
def __new__(
|
|
143
|
+
cls,
|
|
144
|
+
string: str,
|
|
145
|
+
*,
|
|
146
|
+
tag: str,
|
|
147
|
+
**data: typing.Any,
|
|
148
|
+
) -> typing.Self:
|
|
149
|
+
if isinstance(string, TagFormat):
|
|
150
|
+
string = string.formatting()
|
|
151
|
+
elif not isinstance(
|
|
152
|
+
string,
|
|
153
|
+
(
|
|
154
|
+
EscapedString,
|
|
155
|
+
HTMLFormatter,
|
|
156
|
+
),
|
|
157
|
+
):
|
|
158
|
+
string = escape(string)
|
|
159
|
+
obj = super().__new__(cls, string)
|
|
160
|
+
obj.tag = tag
|
|
161
|
+
obj.data = data
|
|
162
|
+
return obj
|
|
163
|
+
|
|
164
|
+
def get_tag_data(self) -> str:
|
|
165
|
+
return "".join(f" {k}={v}" for k, v in self.data.items())
|
|
166
|
+
|
|
167
|
+
def formatting(self) -> "HTMLFormatter":
|
|
168
|
+
return HTMLFormatter(
|
|
169
|
+
TAG_FORMAT.format(
|
|
170
|
+
tag=self.tag,
|
|
171
|
+
data=self.get_tag_data(),
|
|
172
|
+
content=self,
|
|
173
|
+
)
|
|
55
174
|
)
|
|
56
175
|
|
|
57
|
-
|
|
58
|
-
|
|
176
|
+
|
|
177
|
+
class HTMLFormatter(FormatString):
|
|
178
|
+
"""
|
|
179
|
+
>>> HTMLFormatter(bold("Hello, World"))
|
|
180
|
+
'<b>Hello, World</b>'
|
|
181
|
+
HTMLFormatter("Hi, {name:italic}").format(name="Max")
|
|
182
|
+
'Hi, <i>Max</i>'
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
PARSE_MODE = ParseMode.HTML
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def escape(string: str) -> EscapedString:
|
|
189
|
+
if isinstance(string, EscapedString | HTMLFormatter):
|
|
190
|
+
return EscapedString(string)
|
|
191
|
+
return EscapedString(html.escape(string, quote=False))
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def block_quote(string: str) -> TagFormat:
|
|
195
|
+
return TagFormat(string, tag="blockquote")
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def bold(string: str) -> TagFormat:
|
|
199
|
+
return TagFormat(string, tag="b")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def channel_boost_link(channel_username: str, string: str | None = None):
|
|
203
|
+
return link(
|
|
204
|
+
get_channel_boost_link(channel_username),
|
|
205
|
+
string or f"t.me/{channel_username}?boost",
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def code_inline(string: str) -> TagFormat:
|
|
210
|
+
return TagFormat(string, tag="code")
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def italic(string: str) -> TagFormat:
|
|
214
|
+
return TagFormat(string, tag="i")
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def link(href: str, string: str | None = None) -> TagFormat:
|
|
218
|
+
return TagFormat(
|
|
219
|
+
string or href,
|
|
220
|
+
tag="a",
|
|
221
|
+
href=QUOT_MARK + href + QUOT_MARK,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def pre_code(string: str, lang: str | ProgrammingLanguage | None = None) -> TagFormat:
|
|
226
|
+
if lang is None:
|
|
227
|
+
return TagFormat(string, tag="pre")
|
|
228
|
+
lang = lang.value if isinstance(lang, ProgrammingLanguage) else lang
|
|
229
|
+
return pre_code(TagFormat(string, tag="code", **{"class": f"language-{lang}"}))
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def spoiler(string: str) -> TagFormat:
|
|
233
|
+
return TagFormat(string, tag="tg-spoiler")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def start_bot_link(bot_username: str, data: str, string: str | None = None) -> TagFormat:
|
|
237
|
+
return link(
|
|
238
|
+
get_start_bot_link(bot_username, data),
|
|
239
|
+
string or f"t.me/{bot_username}?start={data}"
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def start_group_link(bot_username: str, data: str, string: str | None = None) -> TagFormat:
|
|
244
|
+
return link(get_start_group_link(bot_username, data), string)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def strike(string: str) -> TagFormat:
|
|
248
|
+
return TagFormat(string, tag="s")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def mention(string: str, user_id: int) -> TagFormat:
|
|
252
|
+
return link(get_mention_link(user_id), string)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def tg_emoji(string: str, emoji_id: int) -> TagFormat:
|
|
256
|
+
return TagFormat(string, tag="tg-emoji", **{"emoji-id": emoji_id})
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def invite_chat_link(invite_link: str, string: str | None = None) -> TagFormat:
|
|
260
|
+
return link(
|
|
261
|
+
get_invite_chat_link(invite_link),
|
|
262
|
+
string or f"https://t.me/joinchat/{invite_link}",
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def resolve_domain(username: str, string: str | None = None) -> TagFormat:
|
|
267
|
+
return link(
|
|
268
|
+
get_resolve_domain_link(username),
|
|
269
|
+
string or f"t.me/{username}",
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def underline(string: str) -> TagFormat:
|
|
274
|
+
return TagFormat(string, tag="u")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
__all__ = (
|
|
278
|
+
"FormatString",
|
|
279
|
+
"HTMLFormatter",
|
|
280
|
+
"SpecialFormat",
|
|
281
|
+
"block_quote",
|
|
282
|
+
"bold",
|
|
283
|
+
"channel_boost_link",
|
|
284
|
+
"code_inline",
|
|
285
|
+
"escape",
|
|
286
|
+
"get_channel_boost_link",
|
|
287
|
+
"get_invite_chat_link",
|
|
288
|
+
"get_mention_link",
|
|
289
|
+
"get_resolve_domain_link",
|
|
290
|
+
"get_start_bot_link",
|
|
291
|
+
"get_start_group_link",
|
|
292
|
+
"invite_chat_link",
|
|
293
|
+
"italic",
|
|
294
|
+
"link",
|
|
295
|
+
"mention",
|
|
296
|
+
"pre_code",
|
|
297
|
+
"resolve_domain",
|
|
298
|
+
"spoiler",
|
|
299
|
+
"start_bot_link",
|
|
300
|
+
"start_group_link",
|
|
301
|
+
"strike",
|
|
302
|
+
"tg_emoji",
|
|
303
|
+
"underline",
|
|
304
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
def get_mention_link(user_id: int) -> str:
|
|
2
|
+
return f"tg://user?id={user_id}"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def get_resolve_domain_link(username: str) -> str:
|
|
6
|
+
return f"tg://resolve?domain={username}"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_start_bot_link(bot_username: str, data: str) -> str:
|
|
10
|
+
return get_resolve_domain_link(bot_username) + f"&start={data}"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_start_group_link(bot_username: str, data: str) -> str:
|
|
14
|
+
return get_resolve_domain_link(bot_username) + f"&startgroup={data}"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_channel_boost_link(channel_username: str) -> str:
|
|
18
|
+
return get_resolve_domain_link(channel_username) + "&boost"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_invite_chat_link(invite_link: str) -> str:
|
|
22
|
+
return f"tg://join?invite={invite_link}"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = (
|
|
26
|
+
"get_channel_boost_link",
|
|
27
|
+
"get_invite_chat_link",
|
|
28
|
+
"get_mention_link",
|
|
29
|
+
"get_resolve_domain_link",
|
|
30
|
+
"get_start_bot_link",
|
|
31
|
+
"get_start_group_link",
|
|
32
|
+
)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from telegrinder.types.enums import ProgrammingLanguage
|
|
5
|
+
|
|
6
|
+
SpecialFormat = typing.Union[
|
|
7
|
+
"ChannelBoostLink",
|
|
8
|
+
"Mention",
|
|
9
|
+
"Link",
|
|
10
|
+
"PreCode",
|
|
11
|
+
"TgEmoji",
|
|
12
|
+
"StartBotLink",
|
|
13
|
+
"StartGroupLink",
|
|
14
|
+
"ResolveDomain",
|
|
15
|
+
"InviteChatLink",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def is_spec_format(obj: typing.Any) -> typing.TypeGuard[SpecialFormat]:
|
|
20
|
+
return (
|
|
21
|
+
dataclasses.is_dataclass(obj)
|
|
22
|
+
and hasattr(obj, "__formatter_name__")
|
|
23
|
+
and isinstance(obj, BaseSpecFormat)
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclasses.dataclass(repr=False)
|
|
28
|
+
class BaseSpecFormat:
|
|
29
|
+
__formatter_name__: typing.ClassVar[str]
|
|
30
|
+
|
|
31
|
+
def __repr__(self) -> str:
|
|
32
|
+
return f"<{self.__class__.__name__!r}: {self.__formatter_name__!r}>"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclasses.dataclass
|
|
36
|
+
class ChannelBoostLink(BaseSpecFormat):
|
|
37
|
+
__formatter_name__ = "channel_boost_link"
|
|
38
|
+
|
|
39
|
+
channel_username: str
|
|
40
|
+
string: str | None = None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclasses.dataclass
|
|
44
|
+
class InviteChatLink(BaseSpecFormat):
|
|
45
|
+
__formatter_name__ = "invite_chat_link"
|
|
46
|
+
|
|
47
|
+
invite_link: str
|
|
48
|
+
string: str | None = None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclasses.dataclass
|
|
52
|
+
class Mention(BaseSpecFormat):
|
|
53
|
+
__formatter_name__ = "mention"
|
|
54
|
+
|
|
55
|
+
string: str
|
|
56
|
+
user_id: int
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclasses.dataclass
|
|
60
|
+
class Link(BaseSpecFormat):
|
|
61
|
+
__formatter_name__ = "link"
|
|
62
|
+
|
|
63
|
+
href: str
|
|
64
|
+
string: str | None = None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclasses.dataclass
|
|
68
|
+
class PreCode(BaseSpecFormat):
|
|
69
|
+
__formatter_name__ = "pre_code"
|
|
70
|
+
|
|
71
|
+
string: str
|
|
72
|
+
lang: str | ProgrammingLanguage | None = None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclasses.dataclass
|
|
76
|
+
class TgEmoji(BaseSpecFormat):
|
|
77
|
+
__formatter_name__ = "tg_emoji"
|
|
78
|
+
|
|
79
|
+
string: str
|
|
80
|
+
emoji_id: int
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclasses.dataclass
|
|
84
|
+
class StartBotLink(BaseSpecFormat):
|
|
85
|
+
__formatter_name__ = "start_bot_link"
|
|
86
|
+
|
|
87
|
+
bot_username: str
|
|
88
|
+
data: str
|
|
89
|
+
string: str | None
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclasses.dataclass
|
|
93
|
+
class StartGroupLink(BaseSpecFormat):
|
|
94
|
+
__formatter_name__ = "start_group_link"
|
|
95
|
+
|
|
96
|
+
bot_username: str
|
|
97
|
+
data: str
|
|
98
|
+
string: str | None = None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclasses.dataclass
|
|
102
|
+
class ResolveDomain(BaseSpecFormat):
|
|
103
|
+
__formatter_name__ = "resolve_domain"
|
|
104
|
+
|
|
105
|
+
username: str
|
|
106
|
+
string: str | None = None
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
__all__ = (
|
|
110
|
+
"BaseSpecFormat",
|
|
111
|
+
"ChannelBoostLink",
|
|
112
|
+
"InviteChatLink",
|
|
113
|
+
"Link",
|
|
114
|
+
"Mention",
|
|
115
|
+
"PreCode",
|
|
116
|
+
"ResolveDomain",
|
|
117
|
+
"SpecialFormat",
|
|
118
|
+
"StartBotLink",
|
|
119
|
+
"StartGroupLink",
|
|
120
|
+
"TgEmoji",
|
|
121
|
+
)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .abc import ABCGlobalContext, CtxVar, GlobalCtxVar
|
|
2
|
+
from .global_context import GlobalContext, ctx_var
|
|
3
|
+
from .telegrinder_ctx import TelegrinderCtx
|
|
4
|
+
|
|
5
|
+
__all__ = (
|
|
6
|
+
"ABCGlobalContext",
|
|
7
|
+
"CtxVar",
|
|
8
|
+
"GlobalContext",
|
|
9
|
+
"GlobalCtxVar",
|
|
10
|
+
"TelegrinderCtx",
|
|
11
|
+
"ctx_var",
|
|
12
|
+
)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
|
|
6
|
+
import typing_extensions as typing
|
|
7
|
+
|
|
8
|
+
T = typing.TypeVar("T", default=typing.Any)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclasses.dataclass(repr=False, frozen=True)
|
|
12
|
+
class CtxVar(typing.Generic[T]):
|
|
13
|
+
value: T
|
|
14
|
+
const: bool = dataclasses.field(default=False, kw_only=True)
|
|
15
|
+
|
|
16
|
+
def __repr__(self) -> str:
|
|
17
|
+
return "<{}(value={!r})>".format(
|
|
18
|
+
("Const" if self.const else "") + CtxVar.__name__,
|
|
19
|
+
self.value,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclasses.dataclass(repr=False, frozen=True)
|
|
24
|
+
class GlobalCtxVar(typing.Generic[T]):
|
|
25
|
+
name: str
|
|
26
|
+
value: T
|
|
27
|
+
const: bool = dataclasses.field(default=False, kw_only=True)
|
|
28
|
+
|
|
29
|
+
def __repr__(self) -> str:
|
|
30
|
+
return "<{}({}={})>".format(
|
|
31
|
+
self.__class__.__name__,
|
|
32
|
+
self.name,
|
|
33
|
+
repr(CtxVar(self.value, const=self.const)),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def collect(cls, name: str, ctx_value: T | CtxVariable[T]) -> typing.Self:
|
|
38
|
+
ctx_value = CtxVar(ctx_value) if not isinstance(ctx_value, CtxVar | GlobalCtxVar) else ctx_value
|
|
39
|
+
params = ctx_value.__dict__
|
|
40
|
+
params["name"] = name
|
|
41
|
+
return cls(**params)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ABCGlobalContext(ABC, typing.Generic[T]):
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def __getattr__(self, __name: str) -> typing.Any:
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def __setattr__(self, __name: str, __value: T | CtxVariable[T]) -> None:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def __delattr__(self, __name: str) -> None:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
CtxVariable = CtxVar[T] | GlobalCtxVar[T]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
__all__ = (
|
|
62
|
+
"ABCGlobalContext",
|
|
63
|
+
"CtxVar",
|
|
64
|
+
"CtxVariable",
|
|
65
|
+
"GlobalCtxVar",
|
|
66
|
+
)
|