Skip to content

Commit 1d940b9

Browse files
committed
Turn boolean .media and .service fields into strings
This way they can hold more info about the kind of media and service messages. For example: - message.media == "document" <-> message.document - message.service == "new_chat_title" <-> message.new_chat_title
1 parent 97bd544 commit 1d940b9

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

pyrogram/types/messages_and_media/message.py

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,17 @@ class Message(Object, Update):
106106
The message is empty.
107107
A message can be empty in case it was deleted or you tried to retrieve a message that doesn't exist yet.
108108
109-
service (``bool``, *optional*):
110-
The message is a service message.
111-
A service message has one and only one of these fields set: left_chat_member, new_chat_title,
112-
new_chat_photo, delete_chat_photo, group_chat_created, supergroup_chat_created, channel_chat_created,
113-
migrate_to_chat_id, migrate_from_chat_id, pinned_message.
114-
115-
media (``bool``, *optional*):
116-
The message is a media message.
109+
service (``str``, *optional*):
110+
The message is a service message. This field will contain the name of the service message.
111+
A service message has one and only one of these fields set: new_chat_members, left_chat_member,
112+
new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, channel_chat_created,
113+
migrate_to_chat_id, migrate_from_chat_id, pinned_message, game_high_score, voice_chat_started,
114+
voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited.
115+
116+
media (``str``, *optional*):
117+
The message is a media message. This field will contain the name of the media message.
117118
A media message has one and only one of these fields set: audio, document, photo, sticker, video, animation,
118-
voice, video_note, contact, location, venue.
119+
voice, video_note, contact, location, venue, poll, web_page, dice, game.
119120
120121
edit_date (``int``, *optional*):
121122
Date the message was last edited in Unix time.
@@ -304,10 +305,10 @@ def __init__(
304305
reply_to_message: "Message" = None,
305306
mentioned: bool = None,
306307
empty: bool = None,
307-
service: bool = None,
308+
service: str = None,
308309
scheduled: bool = None,
309310
from_scheduled: bool = None,
310-
media: bool = None,
311+
media: str = None,
311312
edit_date: int = None,
312313
media_group_id: str = None,
313314
author_signature: str = None,
@@ -452,35 +453,51 @@ async def _parse(
452453
voice_chat_ended = None
453454
voice_chat_members_invited = None
454455

456+
service_type = None
457+
455458
if isinstance(action, raw.types.MessageActionChatAddUser):
456459
new_chat_members = [types.User._parse(client, users[i]) for i in action.users]
460+
service_type = "new_chat_members"
457461
elif isinstance(action, raw.types.MessageActionChatJoinedByLink):
458462
new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])]
463+
service_type = "new_chat_members"
459464
elif isinstance(action, raw.types.MessageActionChatDeleteUser):
460465
left_chat_member = types.User._parse(client, users[action.user_id])
466+
service_type = "left_chat_member"
461467
elif isinstance(action, raw.types.MessageActionChatEditTitle):
462468
new_chat_title = action.title
469+
service_type = "new_chat_title"
463470
elif isinstance(action, raw.types.MessageActionChatDeletePhoto):
464471
delete_chat_photo = True
472+
service_type = "delete_chat_photo"
465473
elif isinstance(action, raw.types.MessageActionChatMigrateTo):
466474
migrate_to_chat_id = action.channel_id
475+
service_type = "migrate_to_chat_id"
467476
elif isinstance(action, raw.types.MessageActionChannelMigrateFrom):
468477
migrate_from_chat_id = action.chat_id
478+
service_type = "migrate_from_chat_id"
469479
elif isinstance(action, raw.types.MessageActionChatCreate):
470480
group_chat_created = True
481+
service_type = "group_chat_created"
471482
elif isinstance(action, raw.types.MessageActionChannelCreate):
472483
channel_chat_created = True
484+
service_type = "channel_chat_created"
473485
elif isinstance(action, raw.types.MessageActionChatEditPhoto):
474486
new_chat_photo = types.Photo._parse(client, action.photo)
487+
service_type = "new_chat_photo"
475488
elif isinstance(action, raw.types.MessageActionGroupCallScheduled):
476489
voice_chat_scheduled = types.VoiceChatScheduled._parse(action)
490+
service_type = "voice_chat_scheduled"
477491
elif isinstance(action, raw.types.MessageActionGroupCall):
478492
if action.duration:
479493
voice_chat_ended = types.VoiceChatEnded._parse(action)
494+
service_type = "voice_chat_ended"
480495
else:
481496
voice_chat_started = types.VoiceChatStarted()
497+
service_type = "voice_chat_started"
482498
elif isinstance(action, raw.types.MessageActionInviteToGroupCall):
483499
voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users)
500+
service_type = "voice_chat_members_invited"
484501

485502
user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id)
486503
from_user = types.User._parse(client, users.get(user, None))
@@ -492,7 +509,7 @@ async def _parse(
492509
chat=types.Chat._parse(client, message, users, chats),
493510
from_user=from_user,
494511
sender_chat=sender_chat,
495-
service=True,
512+
service=service_type,
496513
new_chat_members=new_chat_members,
497514
left_chat_member=left_chat_member,
498515
new_chat_title=new_chat_title,
@@ -502,11 +519,11 @@ async def _parse(
502519
migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None,
503520
group_chat_created=group_chat_created,
504521
channel_chat_created=channel_chat_created,
505-
client=client,
506522
voice_chat_scheduled=voice_chat_scheduled,
507523
voice_chat_started=voice_chat_started,
508524
voice_chat_ended=voice_chat_ended,
509-
voice_chat_members_invited=voice_chat_members_invited
525+
voice_chat_members_invited=voice_chat_members_invited,
526+
client=client
510527
# TODO: supergroup_chat_created
511528
)
512529

@@ -517,6 +534,8 @@ async def _parse(
517534
reply_to_message_ids=message.id,
518535
replies=0
519536
)
537+
538+
parsed_message.service = "pinned_message"
520539
except MessageIdsEmpty:
521540
pass
522541

@@ -530,9 +549,13 @@ async def _parse(
530549
reply_to_message_ids=message.id,
531550
replies=0
532551
)
552+
553+
parsed_message.service = "game_high_score"
533554
except MessageIdsEmpty:
534555
pass
535556

557+
558+
536559
return parsed_message
537560

538561
if isinstance(message, raw.types.Message):
@@ -581,18 +604,24 @@ async def _parse(
581604
dice = None
582605

583606
media = message.media
607+
media_type = None
584608

585609
if media:
586610
if isinstance(media, raw.types.MessageMediaPhoto):
587611
photo = types.Photo._parse(client, media.photo, media.ttl_seconds)
612+
media_type = "photo"
588613
elif isinstance(media, raw.types.MessageMediaGeo):
589614
location = types.Location._parse(client, media.geo)
615+
media_type = "location"
590616
elif isinstance(media, raw.types.MessageMediaContact):
591617
contact = types.Contact._parse(client, media)
618+
media_type = "contact"
592619
elif isinstance(media, raw.types.MessageMediaVenue):
593620
venue = types.Venue._parse(client, media)
621+
media_type = "venue"
594622
elif isinstance(media, raw.types.MessageMediaGame):
595623
game = types.Game._parse(client, message)
624+
media_type = "game"
596625
elif isinstance(media, raw.types.MessageMediaDocument):
597626
doc = media.document
598627

@@ -610,38 +639,46 @@ async def _parse(
610639

611640
if audio_attributes.voice:
612641
voice = types.Voice._parse(client, doc, audio_attributes)
642+
media_type = "voice"
613643
else:
614644
audio = types.Audio._parse(client, doc, audio_attributes, file_name)
645+
media_type = "audio"
615646
elif raw.types.DocumentAttributeAnimated in attributes:
616647
video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None)
617-
618648
animation = types.Animation._parse(client, doc, video_attributes, file_name)
649+
media_type = "animation"
619650
elif raw.types.DocumentAttributeVideo in attributes:
620651
video_attributes = attributes[raw.types.DocumentAttributeVideo]
621652

622653
if video_attributes.round_message:
623654
video_note = types.VideoNote._parse(client, doc, video_attributes)
655+
media_type = "video_note"
624656
else:
625-
video = types.Video._parse(client, doc, video_attributes, file_name,
626-
media.ttl_seconds)
657+
video = types.Video._parse(client, doc, video_attributes, file_name, media.ttl_seconds)
658+
media_type = "video"
627659
elif raw.types.DocumentAttributeSticker in attributes:
628660
sticker = await types.Sticker._parse(
629661
client, doc,
630662
attributes.get(raw.types.DocumentAttributeImageSize, None),
631663
attributes[raw.types.DocumentAttributeSticker],
632664
file_name
633665
)
666+
media_type = "sticker"
634667
else:
635668
document = types.Document._parse(client, doc, file_name)
669+
media_type = "document"
636670
elif isinstance(media, raw.types.MessageMediaWebPage):
637671
if isinstance(media.webpage, raw.types.WebPage):
638672
web_page = types.WebPage._parse(client, media.webpage)
673+
media_type = "web_page"
639674
else:
640675
media = None
641676
elif isinstance(media, raw.types.MessageMediaPoll):
642677
poll = types.Poll._parse(client, media)
678+
media_type = "poll"
643679
elif isinstance(media, raw.types.MessageMediaDice):
644680
dice = types.Dice._parse(client, media)
681+
media_type = "dice"
645682
else:
646683
media = None
647684

@@ -699,7 +736,7 @@ async def _parse(
699736
mentioned=message.mentioned,
700737
scheduled=is_scheduled,
701738
from_scheduled=message.from_scheduled,
702-
media=bool(media) or None,
739+
media=media_type,
703740
edit_date=message.edit_date,
704741
media_group_id=message.grouped_id,
705742
photo=photo,

0 commit comments

Comments
 (0)