/
bot.py
3841 lines (3092 loc) · 198 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import datetime
import pathlib
import typing
import warnings
from .base import BaseBot, api
from .. import types
from ..types import base
from ..utils.deprecated import deprecated
from ..utils.exceptions import ValidationError
from ..utils.mixins import DataMixin, ContextInstanceMixin
from ..utils.payload import generate_payload, prepare_arg, prepare_attachment, prepare_file
class Bot(BaseBot, DataMixin, ContextInstanceMixin):
"""
Base bot class
"""
@property
async def me(self) -> types.User:
"""
Alias for self.get_me() but lazy and with caching.
:return: :class:`aiogram.types.User`
"""
if not hasattr(self, '_me'):
setattr(self, '_me', await self.get_me())
return getattr(self, '_me')
@me.deleter
def me(self):
"""
Reset `me`
.. code-block:: python3
await bot.me
:return: :obj:`aiogram.types.User`
"""
if hasattr(self, '_me'):
delattr(self, '_me')
async def download_file_by_id(
self,
file_id: base.String,
destination: typing.Optional[base.InputFile, pathlib.Path] = None,
timeout: base.Integer = 30,
chunk_size: base.Integer = 65536,
seek: base.Boolean = True,
destination_dir: typing.Optional[typing.Union[str, pathlib.Path]] = None,
make_dirs: typing.Optional[base.Boolean] = True,
):
"""
Download file by file_id to destination file or directory
if You want to automatically create destination (:class:`io.BytesIO`) use default
value of destination and handle result of this method.
At most one of these parameters can be used: :param destination:, :param destination_dir:
:param file_id: str
:param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
:param timeout: int
:param chunk_size: int
:param seek: bool - go to start of file when downloading is finished
:param destination_dir: directory for saving files
:param make_dirs: Make dirs if not exist
:return: destination
"""
file = await self.get_file(file_id)
return await self.download_file(file_path=file.file_path, destination=destination,
timeout=timeout, chunk_size=chunk_size, seek=seek,
destination_dir=destination_dir, make_dirs=make_dirs)
# === Getting updates ===
# https://core.telegram.org/bots/api#getting-updates
async def get_updates(self, offset: typing.Optional[base.Integer] = None,
limit: typing.Optional[base.Integer] = None,
timeout: typing.Optional[base.Integer] = None,
allowed_updates:
typing.Union[typing.List[base.String], None] = None) -> typing.List[types.Update]:
"""
Use this method to receive incoming updates using long polling (wiki).
Notes
1. This method will not work if an outgoing webhook is set up.
2. In order to avoid getting duplicate updates, recalculate offset after each server response.
Source: https://core.telegram.org/bots/api#getupdates
:param offset: Identifier of the first update to be returned
:type offset: :obj:`typing.Optional[base.Integer]`
:param limit: Limits the number of updates to be retrieved
:type limit: :obj:`typing.Optional[base.Integer]`
:param timeout: Timeout in seconds for long polling
:type timeout: :obj:`typing.Optional[base.Integer]`
:param allowed_updates: List the types of updates you want your bot to receive
:type allowed_updates: :obj:`typing.Union[typing.List[base.String], None]`
:return: An Array of Update objects is returned
:rtype: :obj:`typing.List[types.Update]`
"""
allowed_updates = prepare_arg(allowed_updates)
payload = generate_payload(**locals())
result = await self.request(api.Methods.GET_UPDATES, payload)
return [types.Update(**update) for update in result]
async def set_webhook(self,
url: base.String,
certificate: typing.Optional[base.InputFile] = None,
ip_address: typing.Optional[base.String] = None,
max_connections: typing.Optional[base.Integer] = None,
allowed_updates: typing.Optional[typing.List[base.String]] = None,
drop_pending_updates: typing.Optional[base.Boolean] = None,
secret_token: typing.Optional[str] = None,
) -> base.Boolean:
"""
Use this method to specify a url and receive incoming updates via an outgoing
webhook. Whenever there is an update for the bot, we will send an HTTPS POST
request to the specified url, containing a JSON-serialized Update. In case
of an unsuccessful request, we will give up after a reasonable amount of
attempts. Returns True on success.
If you'd like to make sure that the Webhook request comes from Telegram,
we recommend using a secret path in the URL, e.g.
`https://www.example.com/<token>`.
Since nobody else knows your bot's token, you can be pretty sure it's us.
Source: https://core.telegram.org/bots/api#setwebhook
:param url: HTTPS url to send updates to. Use an empty string to remove
webhook integration
:type url: :obj:`base.String`
:param certificate: Upload your public key certificate so that the root
certificate in use can be checked. See our self-signed guide for details:
https://core.telegram.org/bots/self-signed
:type certificate: :obj:`typing.Optional[base.InputFile]`
:param ip_address: The fixed IP address which will be used to send webhook
requests instead of the IP address resolved through DNS
:type ip_address: :obj:`typing.Optional[base.String]`
:param max_connections: Maximum allowed number of simultaneous HTTPS
connections to the webhook for update delivery, 1-100. Defaults to 40.
Use lower values to limit the load on your bot's server, and higher
values to increase your bot's throughput.
:type max_connections: :obj:`typing.Optional[base.Integer]`
:param allowed_updates: A list of the update types you want your bot to
receive. For example, specify [“message”, “edited_channel_post”,
“callback_query”] to only receive updates of these types. See Update for
a complete list of available update types. Specify an empty list to
receive all updates regardless of type (default). If not specified, the
previous setting will be used.
Please note that this parameter doesn't affect updates created before the
call to the setWebhook, so unwanted updates may be received for a short
period of time.
:type allowed_updates: :obj:`typing.Optional[typing.List[base.String]]`
:param drop_pending_updates: Pass True to drop all pending updates
:type drop_pending_updates: :obj:`typing.Optional[base.Boolean]`
:param secret_token: A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token”
in every webhook request, 1-256 characters. Only characters A-Z, a-z, 0-9, _ and - are allowed.
The header is useful to ensure that the request comes from a webhook set by you.
:type secret_token: :obj:`typing.Optional[str]`
:return: Returns true
:rtype: :obj:`base.Boolean`
"""
allowed_updates = prepare_arg(allowed_updates)
payload = generate_payload(**locals(), exclude=['certificate'])
files = {}
prepare_file(payload, files, 'certificate', certificate)
return await self.request(api.Methods.SET_WEBHOOK, payload, files)
async def delete_webhook(self,
drop_pending_updates: typing.Optional[base.Boolean] = None,
) -> base.Boolean:
"""
Use this method to remove webhook integration if you decide to switch back
to getUpdates. Returns True on success.
Source: https://core.telegram.org/bots/api#deletewebhook
:param drop_pending_updates: Pass True to drop all pending updates
:type drop_pending_updates: :obj:`typing.Optional[base.Boolean]`
:return: Returns True on success
:rtype: :obj:`base.Boolean`
"""
payload = generate_payload(**locals())
return await self.request(api.Methods.DELETE_WEBHOOK, payload)
async def get_webhook_info(self) -> types.WebhookInfo:
"""
Use this method to get current webhook status. Requires no parameters.
If the bot is using getUpdates, will return an object with the url field empty.
Source: https://core.telegram.org/bots/api#getwebhookinfo
:return: On success, returns a WebhookInfo object
:rtype: :obj:`types.WebhookInfo`
"""
payload = generate_payload(**locals())
result = await self.request(api.Methods.GET_WEBHOOK_INFO, payload)
return types.WebhookInfo(**result)
# === Base methods ===
# https://core.telegram.org/bots/api#available-methods
async def get_me(self) -> types.User:
"""
A simple method for testing your bot's auth token. Requires no parameters.
Source: https://core.telegram.org/bots/api#getme
:return: Returns basic information about the bot in form of a User object
:rtype: :obj:`types.User`
"""
payload = generate_payload(**locals())
result = await self.request(api.Methods.GET_ME, payload)
return types.User(**result)
async def log_out(self) -> base.Boolean:
"""
Use this method to log out from the cloud Bot API server before launching
the bot locally. You **must** log out the bot before running it locally,
otherwise there is no guarantee that the bot will receive updates.
After a successful call, you will not be able to log in again using the
same token for 10 minutes. Returns True on success. Requires no parameters.
Source: https://core.telegram.org/bots/api#logout
:return: Returns True on success
:rtype: :obj:`base.Boolean`
"""
payload = generate_payload(**locals())
return await self.request(api.Methods.LOG_OUT, payload)
@deprecated("This method will be renamed to `close` in aiogram v3.0")
async def close_bot(self) -> base.Boolean:
"""
Use this method to close the bot instance before moving it from one local
server to another. You need to delete the webhook before calling this method
to ensure that the bot isn't launched again after server restart. The method
will return error 429 in the first 10 minutes after the bot is launched.
Returns True on success. Requires no parameters.
Source: https://core.telegram.org/bots/api#close
:return: Returns True on success
:rtype: :obj:`base.Boolean`
"""
payload = generate_payload(**locals())
return await self.request(api.Methods.CLOSE, payload)
async def send_message(self,
chat_id: typing.Union[base.Integer, base.String],
text: base.String,
parse_mode: typing.Optional[base.String] = None,
entities: typing.Optional[typing.List[types.MessageEntity]] = None,
disable_web_page_preview: typing.Optional[base.Boolean] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.Message:
"""
Use this method to send text messages.
Source: https://core.telegram.org/bots/api#sendmessage
:param chat_id: Unique identifier for the target chat or username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param text: Text of the message to be sent
:type text: :obj:`base.String`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,
fixed-width text or inline URLs in your bot's message.
:type parse_mode: :obj:`typing.Optional[base.String]`
:param entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param disable_web_page_preview: Disables link previews for links in this message
:type disable_web_page_preview: :obj:`typing.Optional[base.Boolean]`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply, None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
entities = prepare_arg(entities)
payload = generate_payload(**locals())
if self.parse_mode and entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.disable_web_page_preview:
payload.setdefault('disable_web_page_preview', self.disable_web_page_preview)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
result = await self.request(api.Methods.SEND_MESSAGE, payload)
return types.Message(**result)
async def forward_message(self,
chat_id: typing.Union[base.Integer, base.String],
from_chat_id: typing.Union[base.Integer, base.String],
message_id: base.Integer,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
) -> types.Message:
"""
Use this method to forward messages of any kind.
Source: https://core.telegram.org/bots/api#forwardmessage
:param chat_id: Unique identifier for the target chat or
username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param from_chat_id: Unique identifier for the chat where the
original message was sent
:type from_chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param disable_notification: Sends the message silently. Users
will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of the forwarded
message from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param message_id: Message identifier in the chat specified in
from_chat_id
:type message_id: :obj:`base.Integer`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
result = await self.request(api.Methods.FORWARD_MESSAGE, payload)
return types.Message(**result)
async def copy_message(self,
chat_id: typing.Union[base.Integer, base.String],
from_chat_id: typing.Union[base.Integer, base.String],
message_id: base.Integer,
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.MessageId:
"""
Use this method to copy messages of any kind. The method is analogous to the
method forwardMessages, but the copied message doesn't have a link to the
original message. Returns the MessageId of the sent message on success.
Source: https://core.telegram.org/bots/api#copymessage
:param chat_id: Unique identifier for the target chat or username of the
target channel (in the format @channelusername)
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param from_chat_id: Unique identifier for the chat where the original
message was sent (or channel username in the format @channelusername)
:type from_chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_id: Message identifier in the chat specified in from_chat_id
:type message_id: :obj:`base.Integer`
:param caption: New caption for media, 0-1024 characters after entities
parsing. If not specified, the original caption is kept
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Mode for parsing entities in the new caption. See
formatting options for more details:
https://core.telegram.org/bots/api#formatting-options
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in the new
caption, which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param disable_notification: Sends the message silently. Users will receive
a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original
message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object
for an inline keyboard, custom reply keyboard, instructions to remove
reply keyboard or to force a reply from the user.
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply,
None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals())
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
result = await self.request(api.Methods.COPY_MESSAGE, payload)
return types.MessageId(**result)
async def send_photo(self,
chat_id: typing.Union[base.Integer, base.String],
photo: typing.Union[base.InputFile, base.String],
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.Message:
"""
Use this method to send photos.
Source: https://core.telegram.org/bots/api#sendphoto
:param chat_id: Unique identifier for the target chat or username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param photo: Photo to send
:type photo: :obj:`typing.Union[base.InputFile, base.String]`
:param caption: Photo caption (may also be used when resending photos by file_id), 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,
fixed-width text or inline URLs in your bot's message.
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply, None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals(), exclude=['photo'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
files = {}
prepare_file(payload, files, 'photo', photo)
result = await self.request(api.Methods.SEND_PHOTO, payload, files)
return types.Message(**result)
async def send_audio(self,
chat_id: typing.Union[base.Integer, base.String],
audio: typing.Union[base.InputFile, base.String],
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
duration: typing.Optional[base.Integer] = None,
performer: typing.Optional[base.String] = None,
title: typing.Optional[base.String] = None,
thumb: typing.Union[base.InputFile, base.String, None] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.Message:
"""
Use this method to send audio files, if you want Telegram clients to display them in the music player.
Your audio must be in the .mp3 format.
For sending voice messages, use the sendVoice method instead.
Source: https://core.telegram.org/bots/api#sendaudio
:param chat_id: Unique identifier for the target chat or username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param audio: Audio file to send
:type audio: :obj:`typing.Union[base.InputFile, base.String]`
:param caption: Audio caption, 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,
fixed-width text or inline URLs in your bot's message.
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param duration: Duration of the audio in seconds
:type duration: :obj:`typing.Optional[base.Integer]`
:param performer: Performer
:type performer: :obj:`typing.Optional[base.String]`
:param title: Track name
:type title: :obj:`typing.Optional[base.String]`
:param thumb: Thumbnail of the file sent
:type thumb: :obj:`typing.Union[base.InputFile, base.String, None]`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove, types.ForceReply, None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals(), exclude=['audio', 'thumb'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
files = {}
prepare_file(payload, files, 'audio', audio)
prepare_attachment(payload, files, 'thumb', thumb)
result = await self.request(api.Methods.SEND_AUDIO, payload, files)
return types.Message(**result)
async def send_document(self,
chat_id: typing.Union[base.Integer, base.String],
document: typing.Union[base.InputFile, base.String],
thumb: typing.Union[base.InputFile, base.String, None] = None,
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
disable_content_type_detection: typing.Optional[base.Boolean] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply,
None] = None,
) -> types.Message:
"""
Use this method to send general files. On success, the sent Message is
returned. Bots can currently send files of any type of up to 50 MB in size,
this limit may be changed in the future.
Source: https://core.telegram.org/bots/api#senddocument
:param chat_id: Unique identifier for the target chat or username of the
target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param document: File to send
:type document: :obj:`typing.Union[base.InputFile, base.String]`
:param thumb: Thumbnail of the file sent
:type thumb: :obj:`typing.Union[base.InputFile, base.String, None]`
:param caption: Document caption (may also be used when resending documents
by file_id), 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param disable_content_type_detection: Disables automatic server-side content
type detection for files uploaded using multipart/form-data
:type disable_content_type_detection: :obj:`typing.Optional[base.Boolean]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show
bold, italic, fixed-width text or inline URLs in your bot's message.
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param disable_notification: Sends the message silently. Users will receive a
notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original
message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object
for an inline keyboard, custom reply keyboard, instructions to remove
reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply],
None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals(), exclude=['document'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
files = {}
prepare_file(payload, files, 'document', document)
prepare_attachment(payload, files, 'thumb', thumb)
result = await self.request(api.Methods.SEND_DOCUMENT, payload, files)
return types.Message(**result)
async def send_video(self, chat_id: typing.Union[base.Integer, base.String],
video: typing.Union[base.InputFile, base.String],
duration: typing.Optional[base.Integer] = None,
width: typing.Optional[base.Integer] = None,
height: typing.Optional[base.Integer] = None,
thumb: typing.Union[base.InputFile, base.String, None] = None,
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
supports_streaming: typing.Optional[base.Boolean] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.Message:
"""
Use this method to send video files, Telegram clients support mp4 videos
(other formats may be sent as Document).
Source: https://core.telegram.org/bots/api#sendvideo
:param chat_id: Unique identifier for the target chat or username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param video: Video to send
:type video: :obj:`typing.Union[base.InputFile, base.String]`
:param duration: Duration of sent video in seconds
:type duration: :obj:`typing.Optional[base.Integer]`
:param width: Video width
:type width: :obj:`typing.Optional[base.Integer]`
:param height: Video height
:type height: :obj:`typing.Optional[base.Integer]`
:param thumb: Thumbnail of the file sent
:type thumb: :obj:`typing.Union[base.InputFile, base.String, None]`
:param caption: Video caption (may also be used when resending videos by file_id), 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,
fixed-width text or inline URLs in your bot's message.
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param supports_streaming: Pass True, if the uploaded video is suitable for streaming
:type supports_streaming: :obj:`typing.Optional[base.Boolean]`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply, None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals(), exclude=['video', 'thumb'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
files = {}
prepare_file(payload, files, 'video', video)
prepare_attachment(payload, files, 'thumb', thumb)
result = await self.request(api.Methods.SEND_VIDEO, payload, files)
return types.Message(**result)
async def send_animation(self,
chat_id: typing.Union[base.Integer, base.String],
animation: typing.Union[base.InputFile, base.String],
duration: typing.Optional[base.Integer] = None,
width: typing.Optional[base.Integer] = None,
height: typing.Optional[base.Integer] = None,
thumb: typing.Union[typing.Union[base.InputFile, base.String], None] = None,
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply], None] = None,
) -> types.Message:
"""
Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound).
On success, the sent Message is returned.
Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
Source https://core.telegram.org/bots/api#sendanimation
:param chat_id: Unique identifier for the target chat or username of the target channel
(in the format @channelusername)
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param animation: Animation to send. Pass a file_id as String to send an animation that exists
on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation
from the Internet, or upload a new animation using multipart/form-data
:type animation: :obj:`typing.Union[base.InputFile, base.String]`
:param duration: Duration of sent animation in seconds
:type duration: :obj:`typing.Optional[base.Integer]`
:param width: Animation width
:type width: :obj:`typing.Optional[base.Integer]`
:param height: Animation height
:type height: :obj:`typing.Optional[base.Integer]`
:param thumb: Thumbnail of the file sent. The thumbnail should be in JPEG format and less than 200 kB in size.
A thumbnail‘s width and height should not exceed 320.
:type thumb: :obj:`typing.Union[typing.Union[base.InputFile, base.String], None]`
:param caption: Animation caption (may also be used when resending animation by file_id), 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,
fixed-width text or inline URLs in the media caption
:type parse_mode: :obj:`typing.Optional[base.String]`
:param caption_entities: List of special entities that appear in message text,
which can be specified instead of parse_mode
:type caption_entities: :obj:`typing.Optional[typing.List[types.MessageEntity]]`
:param disable_notification: Sends the message silently. Users will receive a notification with no sound
:type disable_notification: :obj:`typing.Optional[base.Boolean]`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param reply_to_message_id: If the message is a reply, ID of the original message
:type reply_to_message_id: :obj:`typing.Optional[base.Integer]`
:param allow_sending_without_reply: Pass True, if the message should be sent
even if the specified replied-to message is not found
:type allow_sending_without_reply: :obj:`typing.Optional[base.Boolean]`
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user
:type reply_markup: :obj:`typing.Union[typing.Union[types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove, types.ForceReply], None]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
reply_markup = prepare_arg(reply_markup)
caption_entities = prepare_arg(caption_entities)
payload = generate_payload(**locals(), exclude=["animation", "thumb"])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
files = {}
prepare_file(payload, files, 'animation', animation)
prepare_attachment(payload, files, 'thumb', thumb)
result = await self.request(api.Methods.SEND_ANIMATION, payload, files)
return types.Message(**result)
async def send_voice(self,
chat_id: typing.Union[base.Integer, base.String],
voice: typing.Union[base.InputFile, base.String],
caption: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
caption_entities: typing.Optional[typing.List[types.MessageEntity]] = None,
duration: typing.Optional[base.Integer] = None,
message_thread_id: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
reply_to_message_id: typing.Optional[base.Integer] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[types.InlineKeyboardMarkup,
types.ReplyKeyboardMarkup,
types.ReplyKeyboardRemove,
types.ForceReply, None] = None,
) -> types.Message:
"""
Use this method to send audio files, if you want Telegram clients to display the file
as a playable voice message.
For this to work, your audio must be in an .ogg file encoded with OPUS
(other formats may be sent as Audio or Document).
Source: https://core.telegram.org/bots/api#sendvoice
:param chat_id: Unique identifier for the target chat or username of the target channel
:type chat_id: :obj:`typing.Union[base.Integer, base.String]`
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum
supergroups only
:type message_thread_id: :obj:`typing.Optional[base.Integer]`
:param voice: Audio file to send
:type voice: :obj:`typing.Union[base.InputFile, base.String]`
:param caption: Voice message caption, 0-1024 characters
:type caption: :obj:`typing.Optional[base.String]`
:param parse_mode: Send Markdown or HTML, if you want Telegram apps to show bold, italic,