-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
message.py
3437 lines (2926 loc) · 150 KB
/
message.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 functools
import typing
from . import base, fields
from .animation import Animation
from .audio import Audio
from .chat import Chat, ChatType
from .contact import Contact
from .dice import Dice
from .document import Document
from .force_reply import ForceReply
from .game import Game
from .inline_keyboard import InlineKeyboardMarkup
from .input_media import InputMedia, MediaGroup
from .invoice import Invoice
from .location import Location
from .message_auto_delete_timer_changed import MessageAutoDeleteTimerChanged
from .message_entity import MessageEntity
from .message_id import MessageId
from .passport_data import PassportData
from .photo_size import PhotoSize
from .poll import Poll
from .proximity_alert_triggered import ProximityAlertTriggered
from .reply_keyboard import ReplyKeyboardMarkup, ReplyKeyboardRemove
from .sticker import Sticker
from .successful_payment import SuccessfulPayment
from .user import User
from .venue import Venue
from .video import Video
from .video_chat_ended import VideoChatEnded
from .video_chat_participants_invited import VideoChatParticipantsInvited
from .video_chat_scheduled import VideoChatScheduled
from .video_chat_started import VideoChatStarted
from .video_note import VideoNote
from .voice import Voice
from .voice_chat_ended import VoiceChatEnded
from .voice_chat_participants_invited import VoiceChatParticipantsInvited
from .voice_chat_scheduled import VoiceChatScheduled
from .voice_chat_started import VoiceChatStarted
from .web_app_data import WebAppData
from .forum_topic_created import ForumTopicCreated
from .forum_topic_closed import ForumTopicClosed
from .forum_topic_reopened import ForumTopicReopened
from ..utils import helper
from ..utils import markdown as md
from ..utils.text_decorations import html_decoration, markdown_decoration
class Message(base.TelegramObject):
"""
This object represents a message.
https://core.telegram.org/bots/api#message
"""
message_id: base.Integer = fields.Field()
message_thread_id: base.Integer = fields.Field()
from_user: User = fields.Field(alias="from", base=User)
sender_chat: Chat = fields.Field(base=Chat)
date: datetime.datetime = fields.DateTimeField()
chat: Chat = fields.Field(base=Chat)
forward_from: User = fields.Field(base=User)
forward_from_chat: Chat = fields.Field(base=Chat)
forward_from_message_id: base.Integer = fields.Field()
forward_signature: base.String = fields.Field()
forward_date: datetime.datetime = fields.DateTimeField()
is_topic_message: base.Boolean = fields.Field()
is_automatic_forward: base.Boolean = fields.Field()
reply_to_message: Message = fields.Field(base="Message")
via_bot: User = fields.Field(base=User)
edit_date: datetime.datetime = fields.DateTimeField()
has_protected_content: base.Boolean = fields.Field()
media_group_id: base.String = fields.Field()
author_signature: base.String = fields.Field()
forward_sender_name: base.String = fields.Field()
text: base.String = fields.Field()
entities: typing.List[MessageEntity] = fields.ListField(base=MessageEntity)
caption_entities: typing.List[MessageEntity] = fields.ListField(base=MessageEntity)
audio: Audio = fields.Field(base=Audio)
document: Document = fields.Field(base=Document)
animation: Animation = fields.Field(base=Animation)
game: Game = fields.Field(base=Game)
photo: typing.List[PhotoSize] = fields.ListField(base=PhotoSize)
sticker: Sticker = fields.Field(base=Sticker)
video: Video = fields.Field(base=Video)
voice: Voice = fields.Field(base=Voice)
video_note: VideoNote = fields.Field(base=VideoNote)
caption: base.String = fields.Field()
contact: Contact = fields.Field(base=Contact)
location: Location = fields.Field(base=Location)
venue: Venue = fields.Field(base=Venue)
poll: Poll = fields.Field(base=Poll)
dice: Dice = fields.Field(base=Dice)
new_chat_members: typing.List[User] = fields.ListField(base=User)
left_chat_member: User = fields.Field(base=User)
new_chat_title: base.String = fields.Field()
new_chat_photo: typing.List[PhotoSize] = fields.ListField(base=PhotoSize)
delete_chat_photo: base.Boolean = fields.Field()
group_chat_created: base.Boolean = fields.Field()
supergroup_chat_created: base.Boolean = fields.Field()
channel_chat_created: base.Boolean = fields.Field()
message_auto_delete_timer_changed: MessageAutoDeleteTimerChanged = fields.Field(base=MessageAutoDeleteTimerChanged)
migrate_to_chat_id: base.Integer = fields.Field()
migrate_from_chat_id: base.Integer = fields.Field()
pinned_message: Message = fields.Field(base="Message")
invoice: Invoice = fields.Field(base=Invoice)
successful_payment: SuccessfulPayment = fields.Field(base=SuccessfulPayment)
connected_website: base.String = fields.Field()
passport_data: PassportData = fields.Field(base=PassportData)
proximity_alert_triggered: ProximityAlertTriggered = fields.Field(base=ProximityAlertTriggered)
voice_chat_scheduled: VoiceChatScheduled = fields.Field(base=VoiceChatScheduled)
voice_chat_started: VoiceChatStarted = fields.Field(base=VoiceChatStarted)
voice_chat_ended: VoiceChatEnded = fields.Field(base=VoiceChatEnded)
voice_chat_participants_invited: VoiceChatParticipantsInvited = fields.Field(base=VoiceChatParticipantsInvited)
reply_markup: InlineKeyboardMarkup = fields.Field(base=InlineKeyboardMarkup)
web_app_data: WebAppData = fields.Field(base=WebAppData)
forum_topic_created: ForumTopicCreated = fields.Field(base=ForumTopicCreated)
forum_topic_closed: ForumTopicClosed = fields.Field(base=ForumTopicClosed)
forum_topic_reopened: ForumTopicReopened = fields.Field(base=ForumTopicReopened)
video_chat_scheduled: VideoChatScheduled = fields.Field(base=VideoChatScheduled)
video_chat_started: VideoChatStarted = fields.Field(base=VideoChatStarted)
video_chat_ended: VideoChatEnded = fields.Field(base=VideoChatEnded)
video_chat_participants_invited: VideoChatParticipantsInvited = fields.Field(base=VideoChatParticipantsInvited)
@property
@functools.lru_cache()
def content_type(self):
if self.text:
return ContentType.TEXT
if self.audio:
return ContentType.AUDIO
if self.animation:
return ContentType.ANIMATION
if self.document:
return ContentType.DOCUMENT
if self.game:
return ContentType.GAME
if self.photo:
return ContentType.PHOTO
if self.sticker:
return ContentType.STICKER
if self.video:
return ContentType.VIDEO
if self.video_note:
return ContentType.VIDEO_NOTE
if self.voice:
return ContentType.VOICE
if self.contact:
return ContentType.CONTACT
if self.venue:
return ContentType.VENUE
if self.location:
return ContentType.LOCATION
if self.poll:
return ContentType.POLL
if self.dice:
return ContentType.DICE
if self.new_chat_members:
return ContentType.NEW_CHAT_MEMBERS
if self.left_chat_member:
return ContentType.LEFT_CHAT_MEMBER
if self.invoice:
return ContentType.INVOICE
if self.successful_payment:
return ContentType.SUCCESSFUL_PAYMENT
if self.connected_website:
return ContentType.CONNECTED_WEBSITE
if self.message_auto_delete_timer_changed:
return ContentType.MESSAGE_AUTO_DELETE_TIMER_CHANGED
if self.migrate_from_chat_id:
return ContentType.MIGRATE_FROM_CHAT_ID
if self.migrate_to_chat_id:
return ContentType.MIGRATE_TO_CHAT_ID
if self.pinned_message:
return ContentType.PINNED_MESSAGE
if self.new_chat_title:
return ContentType.NEW_CHAT_TITLE
if self.new_chat_photo:
return ContentType.NEW_CHAT_PHOTO
if self.delete_chat_photo:
return ContentType.DELETE_CHAT_PHOTO
if self.group_chat_created:
return ContentType.GROUP_CHAT_CREATED
if self.passport_data:
return ContentType.PASSPORT_DATA
if self.proximity_alert_triggered:
return ContentType.PROXIMITY_ALERT_TRIGGERED
if self.voice_chat_scheduled:
return ContentType.VOICE_CHAT_SCHEDULED
if self.voice_chat_started:
return ContentType.VOICE_CHAT_STARTED
if self.voice_chat_ended:
return ContentType.VOICE_CHAT_ENDED
if self.voice_chat_participants_invited:
return ContentType.VOICE_CHAT_PARTICIPANTS_INVITED
if self.web_app_data:
return ContentType.WEB_APP_DATA
if self.forum_topic_created:
return ContentType.FORUM_TOPIC_CREATED
if self.forum_topic_closed:
return ContentType.FORUM_TOPIC_CLOSED
if self.forum_topic_reopened:
return ContentType.FORUM_TOPIC_REOPENED
if self.video_chat_scheduled:
return ContentType.VIDEO_CHAT_SCHEDULED
if self.video_chat_started:
return ContentType.VIDEO_CHAT_STARTED
if self.video_chat_ended:
return ContentType.VIDEO_CHAT_ENDED
if self.video_chat_participants_invited:
return ContentType.VIDEO_CHAT_PARTICIPANTS_INVITED
return ContentType.UNKNOWN
def is_forward(self) -> bool:
"""
Check that the message is forwarded.
Only `forward_date` is required to be in forwarded message.
:return: bool
"""
return bool(self.forward_date)
def is_command(self) -> bool:
"""
Check message text is command
:return: bool
"""
text = self.text or self.caption
return text and text.startswith("/")
def get_full_command(self) -> typing.Optional[typing.Tuple[str, str]]:
"""
Split command and args
:return: tuple of (command, args)
"""
if self.is_command():
text = self.text or self.caption
command, *args = text.split(maxsplit=1)
args = args[0] if args else ""
return command, args
def get_command(self, pure=False) -> typing.Optional[str]:
"""
Get command from message
:return:
"""
command = self.get_full_command()
if command:
command = command[0]
if pure:
command, _, _ = command[1:].partition("@")
return command
def get_args(self) -> typing.Optional[str]:
"""
Get arguments
:return:
"""
command = self.get_full_command()
if command:
return command[1]
def parse_entities(self, as_html=True) -> str:
"""
Text or caption formatted as HTML or Markdown.
:return: str
"""
text = self.text or self.caption
if text is None:
raise TypeError("This message doesn't have any text.")
entities = self.entities or self.caption_entities
text_decorator = html_decoration if as_html else markdown_decoration
return text_decorator.unparse(text, entities)
@property
def from_id(self) -> int:
"""
User id if sent by user or chat/channel id if sent on behalf of a channel or chat
:return: int
"""
return self.sender_chat.id if self.sender_chat else self.from_user.id
@property
def md_text(self) -> str:
"""
Text or caption formatted as markdown.
:return: str
"""
return self.parse_entities(False)
@property
def html_text(self) -> str:
"""
Text or caption formatted as HTML
:return: str
"""
return self.parse_entities()
@property
def url(self) -> str:
"""
Get URL for the message
:return: str
"""
if self.chat.type == ChatType.PRIVATE:
raise TypeError("Invalid chat type!")
url = "https://t.me/"
if self.chat.username:
# Generates public link
url += f"{self.chat.username}/"
else:
# Generates private link available for chat members
url += f"c/{self.chat.shifted_id}/"
url += f"{self.message_id}"
if self.is_topic_message and self.message_thread_id:
url += f"?topic={self.message_thread_id}"
return url
def link(self, text, as_html=True) -> str:
"""
Generate URL for using in text messages with HTML or MD parse mode
:param text: link label
:param as_html: generate as HTML
:return: str
"""
try:
url = self.url
except TypeError: # URL is not accessible
if as_html:
return md.quote_html(text)
return md.escape_md(text)
if as_html:
return md.hlink(text, url)
return md.link(text, url)
async def answer(
self,
text: base.String,
parse_mode: typing.Optional[base.String] = None,
entities: typing.Optional[typing.List[MessageEntity]] = None,
disable_web_page_preview: typing.Optional[base.Boolean] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> Message:
"""
Answer to this message
: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[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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
return await self.bot.send_message(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
text=text,
parse_mode=parse_mode,
entities=entities,
disable_web_page_preview=disable_web_page_preview,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_photo(
self,
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[MessageEntity]] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> Message:
"""
Use this method to send photos.
Source: https://core.telegram.org/bots/api#sendphoto
: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[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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
return await self.bot.send_photo(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
photo=photo,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_audio(
self,
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[MessageEntity]] = None,
duration: typing.Optional[base.Integer] = None,
performer: typing.Optional[base.String] = None,
title: typing.Optional[base.String] = None,
thumb: typing.Union[typing.Union[base.InputFile, base.String], None] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> 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 audio: Audio file to send.
:type audio: :obj:`typing.Union[base.InputFile, base.String]`
:param caption: Audio caption, 0-1024 characters after entities parsing
: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[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. 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 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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned.
:rtype: :obj:`types.Message`
"""
return await self.bot.send_audio(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
audio=audio,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
duration=duration,
performer=performer,
title=title,
thumb=thumb,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_animation(
self,
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[MessageEntity]] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> 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 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[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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
return await self.bot.send_animation(
self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
animation=animation,
duration=duration,
width=width,
height=height,
thumb=thumb,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_document(
self,
document: typing.Union[base.InputFile, base.String],
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[MessageEntity]] = None,
disable_content_type_detection: typing.Optional[base.Boolean] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> 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 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[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 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]`
:param reply: True if the message is a reply
:type reply: :obj:`typing.Optional[base.Boolean]`
:return: On success, the sent Message is returned
:rtype: :obj:`types.Message`
"""
return await self.bot.send_document(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
thumb=thumb,
document=document,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
disable_content_type_detection=disable_content_type_detection,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_video(
self,
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[MessageEntity]] = None,
supports_streaming: typing.Optional[base.Boolean] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> 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 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. 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[base.InputFile, base.String, None]`
:param caption: Video caption (may also be used when resending videos by file_id), 0-1024 characters after
entities parsing
: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[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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned.
:rtype: :obj:`types.Message`
"""
return await self.bot.send_video(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
video=video,
duration=duration,
width=width,
height=height,
thumb=thumb,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
supports_streaming=supports_streaming,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_voice(
self,
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[MessageEntity]] = None,
duration: typing.Optional[base.Integer] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> 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 voice: Audio file to send.
:type voice: :obj:`typing.Union[base.InputFile, base.String]`
:param caption: Voice message caption, 0-1024 characters after entities parsing
: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[MessageEntity]]`
:param duration: Duration of the voice message in seconds
:type duration: :obj:`typing.Optional[base.Integer]`
: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 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]`
:param reply: fill 'reply_to_message_id'
:type reply: :obj:`base.Boolean`
:return: On success, the sent Message is returned.
:rtype: :obj:`types.Message`
"""
return await self.bot.send_voice(
chat_id=self.chat.id,
message_thread_id=self.message_thread_id if self.is_topic_message else None,
voice=voice,
caption=caption,
parse_mode=parse_mode,
caption_entities=caption_entities,
duration=duration,
disable_notification=disable_notification,
protect_content=protect_content,
reply_to_message_id=self.message_id if reply else None,
allow_sending_without_reply=allow_sending_without_reply,
reply_markup=reply_markup,
)
async def answer_video_note(
self,
video_note: typing.Union[base.InputFile, base.String],
duration: typing.Optional[base.Integer] = None,
length: typing.Optional[base.Integer] = None,
thumb: typing.Union[typing.Union[base.InputFile, base.String], None] = None,
disable_notification: typing.Optional[base.Boolean] = None,
protect_content: typing.Optional[base.Boolean] = None,
allow_sending_without_reply: typing.Optional[base.Boolean] = None,
reply_markup: typing.Union[
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ForceReply,
None,
] = None,
reply: base.Boolean = False,
) -> Message:
"""
As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long.
Use this method to send video messages.
Source: https://core.telegram.org/bots/api#sendvideonote
:param video_note: Video note to send.
:type video_note: :obj:`typing.Union[base.InputFile, base.String]`
:param duration: Duration of sent video in seconds
:type duration: :obj:`typing.Optional[base.Integer]`
:param length: Video width and height
:type length: :obj:`typing.Optional[base.Integer]`