botapi-py 1.4.7__tar.gz → 1.4.9__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: botapi-py
3
- Version: 1.4.7
3
+ Version: 1.4.9
4
4
  Summary: A simple and flexible Python library to conveniently build fast Telegram bots using the Bot API.
5
5
  Requires-Dist: httpx>=0.28.1
6
6
  Requires-Dist: pydantic>=2.12.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "botapi-py"
3
- version = "1.4.7"
3
+ version = "1.4.9"
4
4
  description = "A simple and flexible Python library to conveniently build fast Telegram bots using the Bot API."
5
5
  # readme = "README.md"
6
6
  # authors = [
@@ -10,6 +10,8 @@ from . import core
10
10
 
11
11
  from . import enums
12
12
 
13
+ from .shortcuts import BotShortcuts
14
+
13
15
  from .types import (
14
16
  Update,
15
17
  WebhookInfo,
@@ -319,8 +321,6 @@ from .types import (
319
321
  BoolAdapter,
320
322
  )
321
323
 
322
- from .shortcuts import BotShortcuts
323
-
324
324
  class Bot(core.BaseBot, BotShortcuts):
325
325
  '''
326
326
  Creates an instance of a new Bot API client.
@@ -0,0 +1,245 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Optional, Union, List, cast
4
+
5
+ from . import enums
6
+ from . import types
7
+
8
+ from . import methods
9
+
10
+
11
+ class BotShortcuts:
12
+ '''
13
+ Mixin class containing shortcut bound methods for the `Bot` class.
14
+ These methods provide convenient wrappers around standard API methods.
15
+ '''
16
+
17
+ async def send_media(
18
+ self,
19
+ chat_id: Union[int, str],
20
+ media: Union[types.InputFile, str, None],
21
+ file_type: Optional[enums.MediaType],
22
+ caption: Optional[str] = None,
23
+ parse_mode: Optional[enums.ParseMode] = enums.ParseMode.HTML,
24
+ caption_entities: Optional[List[types.MessageEntity]] = None,
25
+ show_caption_above_media: Optional[bool] = None,
26
+ has_spoiler: Optional[bool] = None,
27
+ disable_notification: Optional[bool] = None,
28
+ protect_content: Optional[bool] = None,
29
+ reply_parameters: Optional[types.ReplyParameters] = None,
30
+ reply_markup: Optional[Union[types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply]] = None,
31
+ message_effect_id: Optional[str] = None,
32
+ allow_paid_broadcast: Optional[bool] = None,
33
+ business_connection_id: Optional[str] = None,
34
+ message_thread_id: Optional[int] = None,
35
+ disable_content_type_detection: Optional[bool] = None, # Documents only
36
+ ) -> types.Message:
37
+ '''
38
+ Method that sends common types of media by automatically routing to the correct `send_<type>` method.
39
+ Also supports send_message with text only for convenience.
40
+
41
+ :param chat_id: Unique identifier for the target chat or username of the target channel.
42
+ :param media: File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
43
+ :param file_type: The type of media to send. Use `enums.MediaType`.
44
+ :param caption: Media caption, 0-1024 characters.
45
+ :param parse_mode: Mode for parsing entities in the media caption.
46
+ :param caption_entities: A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode.
47
+ :param show_caption_above_media: Pass True, if the caption must be shown above the message media.
48
+ :param has_spoiler: Pass True if the media needs to be covered with a spoiler animation.
49
+ :param disable_notification: Sends the message silently. Users will receive a notification with no sound.
50
+ :param protect_content: Protects the contents of the sent message from forwarding and saving.
51
+ :param reply_parameters: Description of the message to reply to.
52
+ :param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user.
53
+ :param message_effect_id: Unique identifier of the message effect to be added to the message.
54
+ :param allow_paid_broadcast: Pass True to allow up to 1000 messages per second, ignoring broadcasting limits for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance.
55
+ :param business_connection_id: Unique identifier of the business connection on behalf of which the message will be sent.
56
+ :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum supergroups only.
57
+ :param disable_content_type_detection: Disables automatic server-side content type detection.
58
+ :return: On success, the sent Message is returned.
59
+ '''
60
+ self = cast(methods.Bot, self)
61
+
62
+ # Plain text message
63
+ if not media or not file_type:
64
+ if not caption:
65
+ raise ValueError('Either media or caption must be provided.')
66
+
67
+ return await self.send_message(
68
+ chat_id=chat_id,
69
+ # =media,
70
+ text=caption,
71
+ parse_mode=parse_mode,
72
+ entities=caption_entities,
73
+ disable_notification=disable_notification,
74
+ protect_content=protect_content,
75
+ reply_parameters=reply_parameters,
76
+ reply_markup=reply_markup,
77
+ message_effect_id=message_effect_id,
78
+ allow_paid_broadcast=allow_paid_broadcast,
79
+ business_connection_id=business_connection_id,
80
+ message_thread_id=message_thread_id,
81
+ # has_spoiler=has_spoiler,
82
+ # show_caption_above_media=show_caption_above_media
83
+ )
84
+
85
+ match file_type:
86
+
87
+ case enums.MediaType.PHOTO:
88
+ return await self.send_photo(
89
+ chat_id=chat_id,
90
+ photo=media,
91
+ caption=caption,
92
+ parse_mode=parse_mode,
93
+ caption_entities=caption_entities,
94
+ disable_notification=disable_notification,
95
+ protect_content=protect_content,
96
+ reply_parameters=reply_parameters,
97
+ reply_markup=reply_markup,
98
+ message_effect_id=message_effect_id,
99
+ allow_paid_broadcast=allow_paid_broadcast,
100
+ business_connection_id=business_connection_id,
101
+ message_thread_id=message_thread_id,
102
+ has_spoiler=has_spoiler,
103
+ show_caption_above_media=show_caption_above_media
104
+ )
105
+
106
+ case enums.MediaType.VIDEO:
107
+ return await self.send_video(
108
+ chat_id=chat_id,
109
+ video=media,
110
+ caption=caption,
111
+ parse_mode=parse_mode,
112
+ caption_entities=caption_entities,
113
+ disable_notification=disable_notification,
114
+ protect_content=protect_content,
115
+ reply_parameters=reply_parameters,
116
+ reply_markup=reply_markup,
117
+ message_effect_id=message_effect_id,
118
+ allow_paid_broadcast=allow_paid_broadcast,
119
+ business_connection_id=business_connection_id,
120
+ message_thread_id=message_thread_id,
121
+ has_spoiler=has_spoiler,
122
+ show_caption_above_media=show_caption_above_media
123
+ )
124
+
125
+ case enums.MediaType.ANIMATION:
126
+ return await self.send_animation(
127
+ chat_id=chat_id,
128
+ animation=media,
129
+ caption=caption,
130
+ parse_mode=parse_mode,
131
+ caption_entities=caption_entities,
132
+ disable_notification=disable_notification,
133
+ protect_content=protect_content,
134
+ reply_parameters=reply_parameters,
135
+ reply_markup=reply_markup,
136
+ message_effect_id=message_effect_id,
137
+ allow_paid_broadcast=allow_paid_broadcast,
138
+ business_connection_id=business_connection_id,
139
+ message_thread_id=message_thread_id,
140
+ has_spoiler=has_spoiler,
141
+ show_caption_above_media=show_caption_above_media
142
+ )
143
+
144
+ case enums.MediaType.AUDIO:
145
+ return await self.send_audio(
146
+ chat_id=chat_id,
147
+ audio=media,
148
+ caption=caption,
149
+ parse_mode=parse_mode,
150
+ caption_entities=caption_entities,
151
+ disable_notification=disable_notification,
152
+ protect_content=protect_content,
153
+ reply_parameters=reply_parameters,
154
+ reply_markup=reply_markup,
155
+ message_effect_id=message_effect_id,
156
+ allow_paid_broadcast=allow_paid_broadcast,
157
+ business_connection_id=business_connection_id,
158
+ message_thread_id=message_thread_id,
159
+ # has_spoiler=has_spoiler,
160
+ # show_caption_above_media=show_caption_above_media
161
+ )
162
+
163
+ case enums.MediaType.DOCUMENT:
164
+ return await self.send_document(
165
+ chat_id=chat_id,
166
+ document=media,
167
+ caption=caption,
168
+ parse_mode=parse_mode,
169
+ caption_entities=caption_entities,
170
+ disable_notification=disable_notification,
171
+ protect_content=protect_content,
172
+ reply_parameters=reply_parameters,
173
+ reply_markup=reply_markup,
174
+ message_effect_id=message_effect_id,
175
+ allow_paid_broadcast=allow_paid_broadcast,
176
+ business_connection_id=business_connection_id,
177
+ message_thread_id=message_thread_id,
178
+ # has_spoiler=has_spoiler,
179
+ # show_caption_above_media=show_caption_above_media,
180
+ disable_content_type_detection=disable_content_type_detection
181
+ )
182
+
183
+ case enums.MediaType.VOICE:
184
+ return await self.send_voice(
185
+ chat_id=chat_id,
186
+ voice=media,
187
+ caption=caption,
188
+ parse_mode=parse_mode,
189
+ caption_entities=caption_entities,
190
+ disable_notification=disable_notification,
191
+ protect_content=protect_content,
192
+ reply_parameters=reply_parameters,
193
+ reply_markup=reply_markup,
194
+ message_effect_id=message_effect_id,
195
+ allow_paid_broadcast=allow_paid_broadcast,
196
+ business_connection_id=business_connection_id,
197
+ message_thread_id=message_thread_id,
198
+ # has_spoiler=has_spoiler,
199
+ # show_caption_above_media=show_caption_above_media
200
+ )
201
+
202
+ case enums.MediaType.VIDEO_NOTE:
203
+ return await self.send_video_note(
204
+ chat_id=chat_id,
205
+ video_note=media,
206
+ # caption=caption,
207
+ # parse_mode=parse_mode,
208
+ # caption_entities=caption_entities,
209
+ disable_notification=disable_notification,
210
+ protect_content=protect_content,
211
+ reply_parameters=reply_parameters,
212
+ reply_markup=reply_markup,
213
+ message_effect_id=message_effect_id,
214
+ allow_paid_broadcast=allow_paid_broadcast,
215
+ business_connection_id=business_connection_id,
216
+ message_thread_id=message_thread_id,
217
+ # has_spoiler=has_spoiler,
218
+ # show_caption_above_media=show_caption_above_media
219
+ )
220
+
221
+ case enums.MediaType.STICKER:
222
+ return await self.send_sticker(
223
+ chat_id=chat_id,
224
+ sticker=media,
225
+ # caption=caption,
226
+ # parse_mode=parse_mode,
227
+ # caption_entities=caption_entities,
228
+ disable_notification=disable_notification,
229
+ protect_content=protect_content,
230
+ reply_parameters=reply_parameters,
231
+ reply_markup=reply_markup,
232
+ message_effect_id=message_effect_id,
233
+ allow_paid_broadcast=allow_paid_broadcast,
234
+ business_connection_id=business_connection_id,
235
+ message_thread_id=message_thread_id,
236
+ # has_spoiler=has_spoiler,
237
+ # show_caption_above_media=show_caption_above_media
238
+ )
239
+
240
+ case enums.MediaType.STORY, enums.MediaType.PAID_MEDIA:
241
+ raise ValueError(f'Unsupported media type: {file_type}')
242
+
243
+ case _:
244
+ raise ValueError(f'Unknown media type: {file_type}')
245
+
@@ -1,141 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from typing import Optional, Union, List, cast
4
-
5
- from . import enums
6
- from . import types
7
-
8
- from . import methods
9
-
10
-
11
- class BotShortcuts:
12
- '''
13
- Mixin class containing shortcut bound methods for the `Bot` class.
14
- These methods provide convenient wrappers around standard API methods.
15
- '''
16
-
17
- async def send_media(
18
- self,
19
- chat_id: Union[int, str],
20
- media: Union[types.InputFile, str],
21
- file_type: enums.MediaType,
22
- caption: Optional[str] = None,
23
- parse_mode: Optional[Union[str, enums.ParseMode]] = enums.ParseMode.HTML,
24
- caption_entities: Optional[List[types.MessageEntity]] = None,
25
- show_caption_above_media: Optional[bool] = None,
26
- has_spoiler: Optional[bool] = None,
27
- disable_notification: Optional[bool] = None,
28
- protect_content: Optional[bool] = None,
29
- reply_parameters: Optional[types.ReplyParameters] = None,
30
- reply_markup: Optional[Union[types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply]] = None,
31
- message_effect_id: Optional[str] = None,
32
- allow_paid_broadcast: Optional[bool] = None,
33
- business_connection_id: Optional[str] = None,
34
- message_thread_id: Optional[int] = None,
35
- ) -> types.Message:
36
- '''
37
- Method that sends common types of media by automatically routing to the correct `send_<type>` method.
38
-
39
- :param chat_id: Unique identifier for the target chat or username of the target channel.
40
- :param media: File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
41
- :param file_type: The type of media to send. Use `enums.MediaType`.
42
- :param caption: Media caption, 0-1024 characters.
43
- :param parse_mode: Mode for parsing entities in the media caption.
44
- :param caption_entities: A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode.
45
- :param show_caption_above_media: Pass True, if the caption must be shown above the message media.
46
- :param has_spoiler: Pass True if the media needs to be covered with a spoiler animation.
47
- :param disable_notification: Sends the message silently. Users will receive a notification with no sound.
48
- :param protect_content: Protects the contents of the sent message from forwarding and saving.
49
- :param reply_parameters: Description of the message to reply to.
50
- :param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user.
51
- :param message_effect_id: Unique identifier of the message effect to be added to the message.
52
- :param allow_paid_broadcast: Pass True to allow up to 1000 messages per second, ignoring broadcasting limits for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance.
53
- :param business_connection_id: Unique identifier of the business connection on behalf of which the message will be sent.
54
- :param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum supergroups only.
55
- :param kwargs: Additional arguments specific to the target method (e.g. `duration`, `width`, `height`, `thumbnail`, `performer`, `title`).
56
- :return: On success, the sent Message is returned.
57
- '''
58
- bot = cast(methods.Bot, self)
59
-
60
- call_args: dict = {
61
- 'chat_id': chat_id,
62
- 'caption': caption,
63
- 'parse_mode': parse_mode,
64
- 'caption_entities': caption_entities,
65
- 'disable_notification': disable_notification,
66
- 'protect_content': protect_content,
67
- 'reply_parameters': reply_parameters,
68
- 'reply_markup': reply_markup,
69
- 'message_effect_id': message_effect_id,
70
- 'allow_paid_broadcast': allow_paid_broadcast,
71
- 'business_connection_id': business_connection_id,
72
- 'message_thread_id': message_thread_id,
73
- 'has_spoiler': has_spoiler,
74
- 'show_caption_above_media': show_caption_above_media,
75
- }
76
-
77
- match file_type:
78
-
79
- case enums.MediaType.PHOTO:
80
- call_args['photo'] = media
81
- return await bot.send_photo(**call_args)
82
-
83
- case enums.MediaType.VIDEO:
84
- call_args['video'] = media
85
- return await bot.send_video(**call_args)
86
-
87
- case enums.MediaType.ANIMATION:
88
- call_args['animation'] = media
89
- return await bot.send_animation(**call_args)
90
-
91
- case enums.MediaType.AUDIO:
92
- call_args['audio'] = media
93
- # Audio doesn't support has_spoiler or show_caption_above_media in some versions,
94
- # but check API. core.telegram.org/bots/api#sendaudio says no spoiler, no caption above.
95
- # We should probably filter them out to avoid errors if the underlying method relies on strict matching?
96
- # generated methods typically accept **kwargs only if defined? No, generated methods have explicit args.
97
- # If I pass 'has_spoiler' to send_audio and it's not in signature, it might fail if I use **call_args matching a signature.
98
- # Python's **kwargs unpacking into a function call fails if keys don't match arguments (unless function has **kwargs).
99
- # The generated methods DO NOT have **kwargs. They have explicit arguments.
100
- # So I MUST NOT pass extra arguments.
101
-
102
- call_args.pop('has_spoiler', None)
103
- call_args.pop('show_caption_above_media', None)
104
- return await bot.send_audio(**call_args)
105
-
106
- case enums.MediaType.DOCUMENT:
107
- call_args['document'] = media
108
- # Document supports disable_content_type_detection
109
- call_args.pop('has_spoiler', None)
110
- call_args.pop('show_caption_above_media', None)
111
- return await bot.send_document(**call_args)
112
-
113
- case enums.MediaType.VOICE:
114
- call_args['voice'] = media
115
- call_args.pop('has_spoiler', None)
116
- call_args.pop('show_caption_above_media', None)
117
- return await bot.send_voice(**call_args)
118
-
119
- case enums.MediaType.VIDEO_NOTE:
120
- call_args['video_note'] = media
121
- call_args.pop('has_spoiler', None)
122
- call_args.pop('show_caption_above_media', None)
123
- call_args.pop('caption', None) # Video notes don't have captions
124
- call_args.pop('parse_mode', None)
125
- call_args.pop('caption_entities', None)
126
- return await bot.send_video_note(**call_args)
127
-
128
- case enums.MediaType.STICKER:
129
- call_args['sticker'] = media
130
- # Sticker is quite different
131
- call_args.pop('caption', None)
132
- call_args.pop('parse_mode', None)
133
- call_args.pop('caption_entities', None)
134
- call_args.pop('has_spoiler', None)
135
- call_args.pop('show_caption_above_media', None)
136
- return await bot.send_sticker(**call_args)
137
-
138
- case _:
139
- # Fallback or Error
140
- raise ValueError(f"Unsupported or unknown media type: {file_type}")
141
-
File without changes
File without changes
File without changes
File without changes