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
This commit is contained in:
Dan 2021-05-05 14:38:29 +02:00
parent 97bd544333
commit 1d940b96a3

View File

@ -106,16 +106,17 @@ class Message(Object, Update):
The message is empty. The message is empty.
A message can be empty in case it was deleted or you tried to retrieve a message that doesn't exist yet. A message can be empty in case it was deleted or you tried to retrieve a message that doesn't exist yet.
service (``bool``, *optional*): service (``str``, *optional*):
The message is a service message. The message is a service message. This field will contain the name of the service message.
A service message has one and only one of these fields set: left_chat_member, new_chat_title, A service message has one and only one of these fields set: new_chat_members, left_chat_member,
new_chat_photo, delete_chat_photo, group_chat_created, supergroup_chat_created, channel_chat_created, new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, channel_chat_created,
migrate_to_chat_id, migrate_from_chat_id, pinned_message. migrate_to_chat_id, migrate_from_chat_id, pinned_message, game_high_score, voice_chat_started,
voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited.
media (``bool``, *optional*): media (``str``, *optional*):
The message is a media message. The message is a media message. This field will contain the name of the media message.
A media message has one and only one of these fields set: audio, document, photo, sticker, video, animation, A media message has one and only one of these fields set: audio, document, photo, sticker, video, animation,
voice, video_note, contact, location, venue. voice, video_note, contact, location, venue, poll, web_page, dice, game.
edit_date (``int``, *optional*): edit_date (``int``, *optional*):
Date the message was last edited in Unix time. Date the message was last edited in Unix time.
@ -304,10 +305,10 @@ class Message(Object, Update):
reply_to_message: "Message" = None, reply_to_message: "Message" = None,
mentioned: bool = None, mentioned: bool = None,
empty: bool = None, empty: bool = None,
service: bool = None, service: str = None,
scheduled: bool = None, scheduled: bool = None,
from_scheduled: bool = None, from_scheduled: bool = None,
media: bool = None, media: str = None,
edit_date: int = None, edit_date: int = None,
media_group_id: str = None, media_group_id: str = None,
author_signature: str = None, author_signature: str = None,
@ -452,35 +453,51 @@ class Message(Object, Update):
voice_chat_ended = None voice_chat_ended = None
voice_chat_members_invited = None voice_chat_members_invited = None
service_type = None
if isinstance(action, raw.types.MessageActionChatAddUser): if isinstance(action, raw.types.MessageActionChatAddUser):
new_chat_members = [types.User._parse(client, users[i]) for i in action.users] new_chat_members = [types.User._parse(client, users[i]) for i in action.users]
service_type = "new_chat_members"
elif isinstance(action, raw.types.MessageActionChatJoinedByLink): elif isinstance(action, raw.types.MessageActionChatJoinedByLink):
new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])] new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])]
service_type = "new_chat_members"
elif isinstance(action, raw.types.MessageActionChatDeleteUser): elif isinstance(action, raw.types.MessageActionChatDeleteUser):
left_chat_member = types.User._parse(client, users[action.user_id]) left_chat_member = types.User._parse(client, users[action.user_id])
service_type = "left_chat_member"
elif isinstance(action, raw.types.MessageActionChatEditTitle): elif isinstance(action, raw.types.MessageActionChatEditTitle):
new_chat_title = action.title new_chat_title = action.title
service_type = "new_chat_title"
elif isinstance(action, raw.types.MessageActionChatDeletePhoto): elif isinstance(action, raw.types.MessageActionChatDeletePhoto):
delete_chat_photo = True delete_chat_photo = True
service_type = "delete_chat_photo"
elif isinstance(action, raw.types.MessageActionChatMigrateTo): elif isinstance(action, raw.types.MessageActionChatMigrateTo):
migrate_to_chat_id = action.channel_id migrate_to_chat_id = action.channel_id
service_type = "migrate_to_chat_id"
elif isinstance(action, raw.types.MessageActionChannelMigrateFrom): elif isinstance(action, raw.types.MessageActionChannelMigrateFrom):
migrate_from_chat_id = action.chat_id migrate_from_chat_id = action.chat_id
service_type = "migrate_from_chat_id"
elif isinstance(action, raw.types.MessageActionChatCreate): elif isinstance(action, raw.types.MessageActionChatCreate):
group_chat_created = True group_chat_created = True
service_type = "group_chat_created"
elif isinstance(action, raw.types.MessageActionChannelCreate): elif isinstance(action, raw.types.MessageActionChannelCreate):
channel_chat_created = True channel_chat_created = True
service_type = "channel_chat_created"
elif isinstance(action, raw.types.MessageActionChatEditPhoto): elif isinstance(action, raw.types.MessageActionChatEditPhoto):
new_chat_photo = types.Photo._parse(client, action.photo) new_chat_photo = types.Photo._parse(client, action.photo)
service_type = "new_chat_photo"
elif isinstance(action, raw.types.MessageActionGroupCallScheduled): elif isinstance(action, raw.types.MessageActionGroupCallScheduled):
voice_chat_scheduled = types.VoiceChatScheduled._parse(action) voice_chat_scheduled = types.VoiceChatScheduled._parse(action)
service_type = "voice_chat_scheduled"
elif isinstance(action, raw.types.MessageActionGroupCall): elif isinstance(action, raw.types.MessageActionGroupCall):
if action.duration: if action.duration:
voice_chat_ended = types.VoiceChatEnded._parse(action) voice_chat_ended = types.VoiceChatEnded._parse(action)
service_type = "voice_chat_ended"
else: else:
voice_chat_started = types.VoiceChatStarted() voice_chat_started = types.VoiceChatStarted()
service_type = "voice_chat_started"
elif isinstance(action, raw.types.MessageActionInviteToGroupCall): elif isinstance(action, raw.types.MessageActionInviteToGroupCall):
voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users) voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users)
service_type = "voice_chat_members_invited"
user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id) user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id)
from_user = types.User._parse(client, users.get(user, None)) from_user = types.User._parse(client, users.get(user, None))
@ -492,7 +509,7 @@ class Message(Object, Update):
chat=types.Chat._parse(client, message, users, chats), chat=types.Chat._parse(client, message, users, chats),
from_user=from_user, from_user=from_user,
sender_chat=sender_chat, sender_chat=sender_chat,
service=True, service=service_type,
new_chat_members=new_chat_members, new_chat_members=new_chat_members,
left_chat_member=left_chat_member, left_chat_member=left_chat_member,
new_chat_title=new_chat_title, new_chat_title=new_chat_title,
@ -502,11 +519,11 @@ class Message(Object, Update):
migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None, migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None,
group_chat_created=group_chat_created, group_chat_created=group_chat_created,
channel_chat_created=channel_chat_created, channel_chat_created=channel_chat_created,
client=client,
voice_chat_scheduled=voice_chat_scheduled, voice_chat_scheduled=voice_chat_scheduled,
voice_chat_started=voice_chat_started, voice_chat_started=voice_chat_started,
voice_chat_ended=voice_chat_ended, voice_chat_ended=voice_chat_ended,
voice_chat_members_invited=voice_chat_members_invited voice_chat_members_invited=voice_chat_members_invited,
client=client
# TODO: supergroup_chat_created # TODO: supergroup_chat_created
) )
@ -517,6 +534,8 @@ class Message(Object, Update):
reply_to_message_ids=message.id, reply_to_message_ids=message.id,
replies=0 replies=0
) )
parsed_message.service = "pinned_message"
except MessageIdsEmpty: except MessageIdsEmpty:
pass pass
@ -530,9 +549,13 @@ class Message(Object, Update):
reply_to_message_ids=message.id, reply_to_message_ids=message.id,
replies=0 replies=0
) )
parsed_message.service = "game_high_score"
except MessageIdsEmpty: except MessageIdsEmpty:
pass pass
return parsed_message return parsed_message
if isinstance(message, raw.types.Message): if isinstance(message, raw.types.Message):
@ -581,18 +604,24 @@ class Message(Object, Update):
dice = None dice = None
media = message.media media = message.media
media_type = None
if media: if media:
if isinstance(media, raw.types.MessageMediaPhoto): if isinstance(media, raw.types.MessageMediaPhoto):
photo = types.Photo._parse(client, media.photo, media.ttl_seconds) photo = types.Photo._parse(client, media.photo, media.ttl_seconds)
media_type = "photo"
elif isinstance(media, raw.types.MessageMediaGeo): elif isinstance(media, raw.types.MessageMediaGeo):
location = types.Location._parse(client, media.geo) location = types.Location._parse(client, media.geo)
media_type = "location"
elif isinstance(media, raw.types.MessageMediaContact): elif isinstance(media, raw.types.MessageMediaContact):
contact = types.Contact._parse(client, media) contact = types.Contact._parse(client, media)
media_type = "contact"
elif isinstance(media, raw.types.MessageMediaVenue): elif isinstance(media, raw.types.MessageMediaVenue):
venue = types.Venue._parse(client, media) venue = types.Venue._parse(client, media)
media_type = "venue"
elif isinstance(media, raw.types.MessageMediaGame): elif isinstance(media, raw.types.MessageMediaGame):
game = types.Game._parse(client, message) game = types.Game._parse(client, message)
media_type = "game"
elif isinstance(media, raw.types.MessageMediaDocument): elif isinstance(media, raw.types.MessageMediaDocument):
doc = media.document doc = media.document
@ -610,20 +639,23 @@ class Message(Object, Update):
if audio_attributes.voice: if audio_attributes.voice:
voice = types.Voice._parse(client, doc, audio_attributes) voice = types.Voice._parse(client, doc, audio_attributes)
media_type = "voice"
else: else:
audio = types.Audio._parse(client, doc, audio_attributes, file_name) audio = types.Audio._parse(client, doc, audio_attributes, file_name)
media_type = "audio"
elif raw.types.DocumentAttributeAnimated in attributes: elif raw.types.DocumentAttributeAnimated in attributes:
video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None) video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None)
animation = types.Animation._parse(client, doc, video_attributes, file_name) animation = types.Animation._parse(client, doc, video_attributes, file_name)
media_type = "animation"
elif raw.types.DocumentAttributeVideo in attributes: elif raw.types.DocumentAttributeVideo in attributes:
video_attributes = attributes[raw.types.DocumentAttributeVideo] video_attributes = attributes[raw.types.DocumentAttributeVideo]
if video_attributes.round_message: if video_attributes.round_message:
video_note = types.VideoNote._parse(client, doc, video_attributes) video_note = types.VideoNote._parse(client, doc, video_attributes)
media_type = "video_note"
else: else:
video = types.Video._parse(client, doc, video_attributes, file_name, video = types.Video._parse(client, doc, video_attributes, file_name, media.ttl_seconds)
media.ttl_seconds) media_type = "video"
elif raw.types.DocumentAttributeSticker in attributes: elif raw.types.DocumentAttributeSticker in attributes:
sticker = await types.Sticker._parse( sticker = await types.Sticker._parse(
client, doc, client, doc,
@ -631,17 +663,22 @@ class Message(Object, Update):
attributes[raw.types.DocumentAttributeSticker], attributes[raw.types.DocumentAttributeSticker],
file_name file_name
) )
media_type = "sticker"
else: else:
document = types.Document._parse(client, doc, file_name) document = types.Document._parse(client, doc, file_name)
media_type = "document"
elif isinstance(media, raw.types.MessageMediaWebPage): elif isinstance(media, raw.types.MessageMediaWebPage):
if isinstance(media.webpage, raw.types.WebPage): if isinstance(media.webpage, raw.types.WebPage):
web_page = types.WebPage._parse(client, media.webpage) web_page = types.WebPage._parse(client, media.webpage)
media_type = "web_page"
else: else:
media = None media = None
elif isinstance(media, raw.types.MessageMediaPoll): elif isinstance(media, raw.types.MessageMediaPoll):
poll = types.Poll._parse(client, media) poll = types.Poll._parse(client, media)
media_type = "poll"
elif isinstance(media, raw.types.MessageMediaDice): elif isinstance(media, raw.types.MessageMediaDice):
dice = types.Dice._parse(client, media) dice = types.Dice._parse(client, media)
media_type = "dice"
else: else:
media = None media = None
@ -699,7 +736,7 @@ class Message(Object, Update):
mentioned=message.mentioned, mentioned=message.mentioned,
scheduled=is_scheduled, scheduled=is_scheduled,
from_scheduled=message.from_scheduled, from_scheduled=message.from_scheduled,
media=bool(media) or None, media=media_type,
edit_date=message.edit_date, edit_date=message.edit_date,
media_group_id=message.grouped_id, media_group_id=message.grouped_id,
photo=photo, photo=photo,