telegrinder 0.1.dev158__py3-none-any.whl → 0.1.dev159__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.

@@ -10,69 +10,462 @@ from telegrinder.msgspec_utils import Nothing, Option, decoder
10
10
  from telegrinder.types import (
11
11
  CallbackQuery,
12
12
  InlineKeyboardMarkup,
13
- Message,
13
+ InputFile,
14
+ InputMedia,
15
+ LinkPreviewOptions,
14
16
  MessageEntity,
17
+ MessageId,
18
+ ReplyParameters,
15
19
  User,
16
20
  )
17
21
 
18
- from .base import BaseCute
22
+ from .base import BaseCute, compose_method_params, shortcut
23
+ from .message import MediaType, MessageCute, ReplyMarkup, execute_method_edit
19
24
 
20
25
 
21
- class CallbackQueryCute(BaseCute[CallbackQuery], CallbackQuery, kw_only=True, dict=True):
26
+ class CallbackQueryCute(
27
+ BaseCute[CallbackQuery], CallbackQuery, kw_only=True, dict=True
28
+ ):
22
29
  api: ABCAPI
23
30
 
24
31
  @property
25
32
  def from_user(self) -> User:
26
33
  return self.from_
27
34
 
28
- def decode_callback_data(self, *, strict: bool = True) -> Option[dict]:
35
+ @property
36
+ def chat_id(self) -> Option[int]:
37
+ """Optional. Message from chat ID. This will be present if the message is sent
38
+ by the bot with the callback button that originated the query."""
39
+
40
+ return self.message.map(lambda m: m.v.chat.id)
41
+
42
+ @property
43
+ def is_topic_message(self) -> Option[bool]:
44
+ """Optional. True, if the message is a topic message with a name,
45
+ color and icon. This will be present if the message is sent
46
+ by the bot with the callback button that originated the query."""
47
+
48
+ return self.message.map(
49
+ lambda m: m.only().map(
50
+ lambda m: m.is_topic_message.unwrap_or(False)
51
+ ).unwrap_or(False)
52
+ )
53
+
54
+ @property
55
+ def message_thread_id(self) -> Option[int]:
56
+ """Optional. Unique identifier of the target message thread (for forum supergroups only).
57
+ This will be present if the message is sent
58
+ by the bot with the callback button that originated the query."""
59
+
60
+ return self.message.map(
61
+ lambda m: m.only().map(
62
+ lambda m: m.message_thread_id,
63
+ ).unwrap_or(Nothing)
64
+ ).unwrap_or(Nothing)
65
+
66
+ @property
67
+ def message_id(self) -> Option[int]:
68
+ """Optional. Unique message identifier inside this chat. This will be present
69
+ if the message is sent by the bot with the callback button that originated the query.
70
+ """
71
+
72
+ return self.message.map(lambda m: m.v.message_id)
73
+
74
+ def decode_callback_data(self, *, strict: bool = True) -> Option[dict[str, typing.Any]]:
29
75
  if "cached_callback_data" in self.__dict__:
30
76
  return self.__dict__["cached_callback_data"]
31
-
32
77
  data = Nothing
33
78
  with suppress(msgspec.ValidationError):
34
- data = Some(decoder.decode(self.data.unwrap()))
35
-
79
+ data = Some(decoder.decode(self.data.unwrap(), strict=strict))
36
80
  self.__dict__["cached_callback_data"] = data
37
81
  return data
38
82
 
83
+ @shortcut("answer_callback_query")
39
84
  async def answer(
40
85
  self,
41
86
  text: str | Option[str] = Nothing,
87
+ callback_query_id: str | Option[str] = Nothing,
42
88
  show_alert: bool | Option[bool] = Nothing,
43
89
  url: str | Option[str] = Nothing,
44
90
  cache_time: int | Option[int] = Nothing,
45
91
  **other: typing.Any,
46
92
  ) -> Result[bool, APIError]:
47
- params = get_params(locals())
48
- return await self.ctx_api.answer_callback_query(self.id, **params)
93
+ """Shortcut `API.answer_callback_query()`, see the [documentation](https://core.telegram.org/bots/api#answercallbackquery)
94
+
95
+ Use this method to send answers to callback queries sent from inline keyboards.
96
+ The answer will be displayed to the user as a notification at the top of the
97
+ chat screen or as an alert. On success, True is returned.
98
+
99
+ :param callback_query_id: Unique identifier for the query to be answered.
100
+
101
+ :param text: Text of the notification. If not specified, nothing will be shown to the \
102
+ user, 0-200 characters.
103
+
104
+ :param show_alert: If True, an alert will be shown by the client instead of a notification at \
105
+ the top of the chat screen. Defaults to false.
106
+
107
+ :param url: URL that will be opened by the user's client. If you have created a Game and \
108
+ accepted the conditions via @BotFather, specify the URL that opens your \
109
+ game - note that this will only work if the query comes from a callback_game \
110
+ button. Otherwise, you may use links like t.me/your_bot?start=XXXX that \
111
+ open your bot with a parameter.
112
+
113
+ :param cache_time: The maximum amount of time in seconds that the result of the callback query \
114
+ may be cached client-side. Telegram apps will support caching starting \
115
+ in version 3.14. Defaults to 0."""
116
+
117
+ params = compose_method_params(
118
+ get_params(locals()), self, default_params={("callback_query_id", "id")}
119
+ )
120
+ return await self.ctx_api.answer_callback_query(**params)
121
+
122
+ @shortcut(
123
+ "copy_message",
124
+ custom_params={"reply_parameters", "message_thread_id"},
125
+ )
126
+ async def copy(
127
+ self,
128
+ chat_id: int | str | Option[int | str] = Nothing,
129
+ from_chat_id: int | str | Option[int | str] = Nothing,
130
+ message_id: int | Option[int] = Nothing,
131
+ message_thread_id: Option[int] | int = Nothing,
132
+ caption: Option[str] | str = Nothing,
133
+ parse_mode: Option[str] | str = Nothing,
134
+ caption_entities: Option[list[MessageEntity]] | list[MessageEntity] = Nothing,
135
+ disable_notification: Option[bool] | bool = Nothing,
136
+ protect_content: Option[bool] | bool = Nothing,
137
+ reply_parameters: Option[ReplyParameters | dict[str, typing.Any]]
138
+ | ReplyParameters
139
+ | dict[str, typing.Any] = Nothing,
140
+ reply_markup: Option[ReplyMarkup] | ReplyMarkup = Nothing,
141
+ **other: typing.Any,
142
+ ) -> Result[MessageId, APIError]:
143
+ """Shortcut `API.copy_message()`, see the [documentation](https://core.telegram.org/bots/api#copymessage)
144
+
145
+ Use this method to copy messages of any kind. Service messages, giveaway
146
+ messages, giveaway winners messages, and invoice messages can't be copied.
147
+ A quiz poll can be copied only if the value of the field correct_option_id
148
+ is known to the bot. The method is analogous to the method forwardMessage,
149
+ but the copied message doesn't have a link to the original message. Returns
150
+ the MessageId of the sent message on success.
151
+
152
+ :param chat_id: Unique identifier for the target chat or username of the target channel \
153
+ (in the format @channelusername).
154
+
155
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
156
+ forum supergroups only.
49
157
 
158
+ :param from_chat_id: Unique identifier for the chat where the original message was sent (or channel \
159
+ username in the format @channelusername).
160
+
161
+ :param message_id: Message identifier in the chat specified in from_chat_id.
162
+
163
+ :param caption: New caption for media, 0-1024 characters after entities parsing. If not \
164
+ specified, the original caption is kept.
165
+
166
+ :param parse_mode: Mode for parsing entities in the new caption. See formatting options for \
167
+ more details.
168
+
169
+ :param caption_entities: A JSON-serialized list of special entities that appear in the new caption, \
170
+ which can be specified instead of parse_mode.
171
+
172
+ :param disable_notification: Sends the message silently. Users will receive a notification with no sound. \
173
+
174
+ :param protect_content: Protects the contents of the sent message from forwarding and saving.
175
+
176
+ :param reply_parameters: Description of the message to reply to.
177
+
178
+ :param reply_markup: Additional interface options. A JSON-serialized object for an inline \
179
+ keyboard, custom reply keyboard, instructions to remove reply keyboard \
180
+ or to force a reply from the user.
181
+ """
182
+
183
+ return await MessageCute.copy(self, **get_params(locals())) # type: ignore
184
+
185
+ @shortcut("delete_message", custom_params={"message_thread_id"})
186
+ async def delete(
187
+ self,
188
+ chat_id: int | Option[int] = Nothing,
189
+ message_id: int | Option[int] = Nothing,
190
+ message_thread_id: int | Option[int] = Nothing,
191
+ **other: typing.Any,
192
+ ) -> Result[bool, APIError]:
193
+ """Shortcut `API.delete_message()`, see the [documentation](https://core.telegram.org/bots/api#deletemessage)
194
+
195
+ Use this method to delete a message, including service messages, with the
196
+ following limitations: - A message can only be deleted if it was sent less
197
+ than 48 hours ago. - Service messages about a supergroup, channel, or forum
198
+ topic creation can't be deleted. - A dice message in a private chat can only
199
+ be deleted if it was sent more than 24 hours ago. - Bots can delete outgoing
200
+ messages in private chats, groups, and supergroups. - Bots can delete incoming
201
+ messages in private chats. - Bots granted can_post_messages permissions
202
+ can delete outgoing messages in channels. - If the bot is an administrator
203
+ of a group, it can delete any message there. - If the bot has can_delete_messages
204
+ permission in a supergroup or a channel, it can delete any message there.
205
+ Returns True on success.
206
+
207
+ :param chat_id: Unique identifier for the target chat or username of the target channel \
208
+ (in the format @channelusername).
209
+
210
+ :param message_id: Identifier of the message to delete.
211
+
212
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
213
+ forum supergroups only."""
214
+
215
+ return await MessageCute.delete(self, **get_params(locals())) # type: ignore
216
+
217
+ @shortcut(
218
+ "edit_message_text",
219
+ executor=execute_method_edit,
220
+ custom_params={"message_thread_id", "link_preview_options"}
221
+ )
50
222
  async def edit_text(
51
223
  self,
52
224
  text: str | Option[str],
225
+ inline_message_id: int | Option[int] = Nothing,
226
+ chat_id: Option[int | str] | int | str = Nothing,
227
+ message_id: Option[int] | int = Nothing,
228
+ message_thread_id: int | Option[int] = Nothing,
53
229
  parse_mode: str | Option[str] = Nothing,
54
230
  entities: list[MessageEntity] | Option[list[MessageEntity]] = Nothing,
55
- disable_web_page_preview: bool | Option[bool] = Nothing,
56
- reply_markup: InlineKeyboardMarkup
57
- | Option[InlineKeyboardMarkup]
58
- = Nothing,
231
+ link_preview_options: Option[LinkPreviewOptions | dict[str, typing.Any]]
232
+ | LinkPreviewOptions
233
+ | dict[str, typing.Any] = Nothing,
234
+ reply_markup: InlineKeyboardMarkup | Option[InlineKeyboardMarkup] = Nothing,
59
235
  **other: typing.Any,
60
- ) -> Result[Variative[Message, bool], APIError]:
61
- params = get_params(locals())
62
-
63
- if message := self.message.map(lambda message: message.only().unwrap_or_none()).unwrap_or_none():
64
- if message.message_thread_id and "message_thread_id" not in params:
65
- params["message_thread_id"] = message.message_thread_id.unwrap()
66
- return await self.ctx_api.edit_message_text(
67
- chat_id=message.chat.id,
68
- message_id=message.message_id,
69
- **params,
70
- )
236
+ ) -> Result[Variative[MessageCute, bool], APIError]:
237
+ """Shortcut `API.edit_message_text()`, see the [documentation](https://core.telegram.org/bots/api#editmessagetext)
238
+
239
+ Use this method to edit text and game messages. On success, if the edited
240
+ message is not an inline message, the edited Message is returned, otherwise
241
+ True is returned.
242
+
243
+ :param inline_message_id: Required if chat_id and message_id are not specified. Identifier of the \
244
+ inline message.
245
+
246
+ :param chat_id: Required if inline_message_id is not specified. Unique identifier for \
247
+ the target chat or username of the target channel (in the format @channelusername). \
248
+
249
+ :param message_id: Required if inline_message_id is not specified. Identifier of the message \
250
+ to edit.
251
+
252
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
253
+ forum supergroups only.
254
+
255
+ :param text: New text of the message, 1-4096 characters after entities parsing.
256
+
257
+ :param parse_mode: Mode for parsing entities in the message text. See formatting options for \
258
+ more details.
259
+
260
+ :param entities: A JSON-serialized list of special entities that appear in message text, \
261
+ which can be specified instead of parse_mode.
262
+
263
+ :param link_preview_options: Link preview generation options for the message.
264
+
265
+ :param reply_markup: A JSON-serialized object for an inline keyboard."""
266
+
267
+ ...
268
+
269
+ @shortcut(
270
+ "edit_message_live_location",
271
+ executor=execute_method_edit,
272
+ custom_params={"message_thread_id"},
273
+ )
274
+ async def edit_live_location(
275
+ self,
276
+ latitude: float,
277
+ longitude: float,
278
+ inline_message_id: Option[str] | str = Nothing,
279
+ message_thread_id: int | Option[int] = Nothing,
280
+ chat_id: Option[int | str] | int | str = Nothing,
281
+ message_id: Option[int] | int = Nothing,
282
+ horizontal_accuracy: Option[float] | float = Nothing,
283
+ heading: Option[int] | int = Nothing,
284
+ proximity_alert_radius: Option[int] | int = Nothing,
285
+ reply_markup: Option[InlineKeyboardMarkup] | InlineKeyboardMarkup = Nothing,
286
+ **other: typing.Any,
287
+ ) -> Result[Variative[MessageCute, bool], APIError]:
288
+ """Shortcut `API.edit_message_live_location()`, see the [documentation](https://core.telegram.org/bots/api#editmessagelivelocation)
289
+
290
+ Use this method to edit live location messages. A location can be edited
291
+ until its live_period expires or editing is explicitly disabled by a call
292
+ to stopMessageLiveLocation. On success, if the edited message is not an
293
+ inline message, the edited Message is returned, otherwise True is returned.
294
+
295
+ :param inline_message_id: Required if chat_id and message_id are not specified. Identifier of the \
296
+ inline message.
297
+
298
+ :param chat_id: Required if inline_message_id is not specified. Unique identifier for \
299
+ the target chat or username of the target channel (in the format @channelusername). \
300
+
301
+ :param message_id: Required if inline_message_id is not specified. Identifier of the message \
302
+ to edit.
303
+
304
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
305
+ forum supergroups only.
306
+
307
+ :param latitude: Latitude of new location.
308
+
309
+ :param longitude: Longitude of new location.
310
+
311
+ :param horizontal_accuracy: The radius of uncertainty for the location, measured in meters; 0-1500. \
312
+
313
+ :param heading: Direction in which the user is moving, in degrees. Must be between 1 and 360 \
314
+ if specified.
315
+
316
+ :param proximity_alert_radius: The maximum distance for proximity alerts about approaching another chat \
317
+ member, in meters. Must be between 1 and 100000 if specified.
318
+
319
+ :param reply_markup: A JSON-serialized object for a new inline keyboard."""
320
+
321
+ ...
322
+
323
+ @shortcut(
324
+ "edit_message_caption",
325
+ executor=execute_method_edit,
326
+ custom_params={"message_thread_id"},
327
+ )
328
+ async def edit_caption(
329
+ self,
330
+ caption: Option[str] | str,
331
+ chat_id: Option[int | str] | int | str = Nothing,
332
+ message_id: Option[int] | int = Nothing,
333
+ message_thread_id: int | Option[int] = Nothing,
334
+ inline_message_id: Option[str] | str = Nothing,
335
+ parse_mode: Option[str] | str = Nothing,
336
+ caption_entities: Option[list[MessageEntity]] | list[MessageEntity] = Nothing,
337
+ reply_markup: Option[InlineKeyboardMarkup] | InlineKeyboardMarkup = Nothing,
338
+ **other: typing.Any,
339
+ ) -> Result[Variative[MessageCute, bool], APIError]:
340
+ """Shortcut `API.edit_message_caption()`, see the [documentation](https://core.telegram.org/bots/api#editmessagecaption)
341
+
342
+ Use this method to edit captions of messages. On success, if the edited message
343
+ is not an inline message, the edited Message is returned, otherwise True
344
+ is returned.
345
+
346
+ :param chat_id: Required if inline_message_id is not specified. Unique identifier for \
347
+ the target chat or username of the target channel (in the format @channelusername). \
348
+
349
+ :param message_id: Required if inline_message_id is not specified. Identifier of the message \
350
+ to edit.
351
+
352
+ :param inline_message_id: Required if chat_id and message_id are not specified. Identifier of the \
353
+ inline message.
354
+
355
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
356
+ forum supergroups only.
357
+
358
+ :param caption: New caption of the message, 0-1024 characters after entities parsing. \
359
+
360
+ :param parse_mode: Mode for parsing entities in the message caption. See formatting options \
361
+ for more details.
362
+
363
+ :param caption_entities: A JSON-serialized list of special entities that appear in the caption, \
364
+ which can be specified instead of parse_mode.
365
+
366
+ :param reply_markup: A JSON-serialized object for an inline keyboard."""
367
+
368
+ ...
369
+
370
+ @shortcut(
371
+ "edit_message_media",
372
+ custom_params={
373
+ "media",
374
+ "type",
375
+ "message_thread_id",
376
+ "caption",
377
+ "parse_mode",
378
+ "caption_entities",
379
+ },
380
+ )
381
+ async def edit_media(
382
+ self,
383
+ media: str | InputFile | InputMedia,
384
+ type: MediaType | Option[MediaType] = Nothing,
385
+ caption: Option[str] | str = Nothing,
386
+ parse_mode: Option[str] | str = Nothing,
387
+ caption_entities: Option[list[MessageEntity]] | list[MessageEntity] = Nothing,
388
+ inline_message_id: Option[str] | str = Nothing,
389
+ chat_id: Option[int | str] | int | str = Nothing,
390
+ message_id: Option[int] | int = Nothing,
391
+ message_thread_id: Option[int] | int = Nothing,
392
+ reply_markup: Option[InlineKeyboardMarkup] | InlineKeyboardMarkup = Nothing,
393
+ **other: typing.Any,
394
+ ) -> Result[Variative[MessageCute, bool], APIError]:
395
+ """Shortcut `API.edit_message_media()`, see the [documentation](https://core.telegram.org/bots/api#editmessagemedia)
396
+
397
+ Use this method to edit animation, audio, document, photo, or video messages.
398
+ If a message is part of a message album, then it can be edited only to an audio
399
+ for audio albums, only to a document for document albums and to a photo or
400
+ a video otherwise. When an inline message is edited, a new file can't be uploaded;
401
+ use a previously uploaded file via its file_id or specify a URL. On success,
402
+ if the edited message is not an inline message, the edited Message is returned,
403
+ otherwise True is returned.
404
+
405
+ :param chat_id: Required if inline_message_id is not specified. Unique identifier for \
406
+ the target chat or username of the target channel (in the format @channelusername). \
407
+
408
+ :param message_id: Required if inline_message_id is not specified. Identifier of the message \
409
+ to edit.
410
+
411
+ :param inline_message_id: Required if chat_id and message_id are not specified. Identifier of the \
412
+ inline message.
413
+
414
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
415
+ forum supergroups only.
416
+
417
+ :param media: A JSON-serialized object for a new media content of the message.
418
+
419
+ :param caption: Audio caption, 0-1024 characters after entities parsing.
420
+
421
+ :param parse_mode: Mode for parsing entities in the audio caption. See formatting options \
422
+ for more details.
423
+
424
+ :param caption_entities: A JSON-serialized list of special entities that appear in the caption, \
425
+ which can be specified instead of parse_mode.
71
426
 
72
- return await self.ctx_api.edit_message_text(
73
- inline_message_id=self.inline_message_id,
74
- **params,
75
- )
427
+ :param type: Required if media is not an `str | InputMedia` object. Type of the media, \
428
+ must be one of `photo`, `video`, `animation`, `audio`, `document`.
429
+
430
+ :param reply_markup: A JSON-serialized object for a new inline keyboard."""
431
+
432
+ return await MessageCute.edit_media(self, **get_params(locals())) # type: ignore
433
+
434
+ @shortcut(
435
+ "edit_message_reply_markup",
436
+ executor=execute_method_edit,
437
+ custom_params={"message_thread_id"},
438
+ )
439
+ async def edit_reply_markup(
440
+ self,
441
+ inline_message_id: Option[str] | str = Nothing,
442
+ message_id: Option[int] | int = Nothing,
443
+ message_thread_id: Option[int] | int = Nothing,
444
+ chat_id: Option[int | str] | int | str = Nothing,
445
+ reply_markup: Option[InlineKeyboardMarkup] | InlineKeyboardMarkup = Nothing,
446
+ **other: typing.Any,
447
+ ) -> Result[Variative[MessageCute, bool], APIError]:
448
+ """Shortcut `API.edit_message_reply_markup()`, see the [documentation](https://core.telegram.org/bots/api#editmessagereplymarkup)
449
+
450
+ Use this method to edit only the reply markup of messages. On success, if
451
+ the edited message is not an inline message, the edited Message is returned,
452
+ otherwise True is returned.
453
+
454
+ :param chat_id: Required if inline_message_id is not specified. Unique identifier for \
455
+ the target chat or username of the target channel (in the format @channelusername). \
456
+
457
+ :param message_id: Required if inline_message_id is not specified. Identifier of the message \
458
+ to edit.
459
+
460
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for \
461
+ forum supergroups only.
462
+
463
+ :param inline_message_id: Required if chat_id and message_id are not specified. Identifier of the \
464
+ inline message.
465
+
466
+ :param reply_markup: A JSON-serialized object for an inline keyboard."""
467
+
468
+ ...
76
469
 
77
470
 
78
471
  __all__ = ("CallbackQueryCute",)
@@ -3,8 +3,14 @@ import typing
3
3
  from fntypes.result import Result
4
4
 
5
5
  from telegrinder.api import ABCAPI, APIError
6
+ from telegrinder.model import get_params
6
7
  from telegrinder.msgspec_utils import Nothing, Option
7
- from telegrinder.types import InlineQuery, InlineQueryResult, User
8
+ from telegrinder.types import (
9
+ InlineQuery,
10
+ InlineQueryResult,
11
+ InlineQueryResultsButton,
12
+ User,
13
+ )
8
14
 
9
15
  from .base import BaseCute
10
16
 
@@ -19,23 +25,17 @@ class InlineQueryCute(BaseCute[InlineQuery], InlineQuery, kw_only=True):
19
25
  async def answer(
20
26
  self,
21
27
  results: InlineQueryResult | list[InlineQueryResult],
28
+ inline_query_id: str | Option[str] = Nothing,
22
29
  cache_time: int | Option[int] = Nothing,
23
30
  is_personal: bool | Option[bool] = Nothing,
24
31
  next_offset: str | Option[str] = Nothing,
25
- switch_pm_text: str | Option[str] = Nothing,
26
- switch_pm_parameter: str | Option[str] = Nothing,
32
+ button: Option[InlineQueryResultsButton] | InlineQueryResultsButton = Nothing,
27
33
  **other: typing.Any,
28
34
  ) -> Result[bool, APIError]:
29
- return await self.ctx_api.answer_inline_query(
30
- self.id,
31
- results=[results] if not isinstance(results, list) else results,
32
- cache_time=cache_time,
33
- is_personal=is_personal,
34
- next_offset=next_offset,
35
- switch_pm_text=switch_pm_text,
36
- switch_pm_parameter=switch_pm_parameter,
37
- **other,
38
- )
35
+ params = get_params(locals())
36
+ params["results"] = [results] if not isinstance(results, list) else results
37
+ params.setdefault("inline_query_id", self.id)
38
+ return await self.ctx_api.answer_inline_query(**params)
39
39
 
40
40
 
41
41
  __all__ = ("InlineQueryCute",)