From 3ff09f7b91aa8fdd097169ec1544ab0b59483a1f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 15 Oct 2020 11:04:54 +0200 Subject: [PATCH] Use inline result mime to infer the result type --- telethon/tl/custom/inlinebuilder.py | 25 ++++++++++++++++++++++++- telethon_generator/data/errors.csv | 1 + telethon_generator/data/methods.csv | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/telethon/tl/custom/inlinebuilder.py b/telethon/tl/custom/inlinebuilder.py index a92f6c7b..949f0b63 100644 --- a/telethon/tl/custom/inlinebuilder.py +++ b/telethon/tl/custom/inlinebuilder.py @@ -3,6 +3,20 @@ import hashlib from .. import functions, types from ... import utils +_TYPE_TO_MIMES = { + 'gif': ['image/gif'], # 'video/mp4' too, but that's used for video + 'article': ['text/html'], + 'audio': ['audio/mpeg'], + 'contact': [], + 'file': ['application/pdf', 'application/zip'], # actually any + 'geo': [], + 'photo': ['image/jpeg'], + 'sticker': ['image/webp', 'application/x-tgsticker'], + 'venue': [], + 'video': ['video/mp4'], # tdlib includes text/html for some reason + 'voice': ['audio/ogg'], +} + class InlineBuilder: """ @@ -239,6 +253,8 @@ class InlineBuilder: type (`str`, optional): The type of the document. May be one of: article, audio, contact, file, geo, gif, photo, sticker, venue, video, voice. + It will be automatically set if ``mime_type`` is specified, + and default to ``'file'`` if no matching mime type is found. include_media (`bool`, optional): Whether the document file used to display the result should be @@ -270,7 +286,14 @@ class InlineBuilder: if type is None: if voice_note: type = 'voice' - else: + elif mime_type: + for ty, mimes in _TYPE_TO_MIMES.items(): + for mime in mimes: + if mime_type == mime: + type = ty + break + + if type is None: type = 'file' try: diff --git a/telethon_generator/data/errors.csv b/telethon_generator/data/errors.csv index 64168f4f..1ca79418 100644 --- a/telethon_generator/data/errors.csv +++ b/telethon_generator/data/errors.csv @@ -9,6 +9,7 @@ ADMIN_RANK_INVALID,400,The given admin title or rank was invalid (possibly large API_ID_INVALID,400,The api_id/api_hash combination is invalid API_ID_PUBLISHED_FLOOD,400,"This API id was published somewhere, you can't use it now" ARTICLE_TITLE_EMPTY,400,The title of the article is empty +AUDIO_TITLE_EMPTY,400,The title attribute of the audio must be non-empty AUTH_BYTES_INVALID,400,The provided authorization is invalid AUTH_KEY_DUPLICATED,406,"The authorization key (session file) was used under two different IP addresses simultaneously, and can no longer be used. Use the same session exclusively, or use different sessions" AUTH_KEY_INVALID,401,The key is invalid diff --git a/telethon_generator/data/methods.csv b/telethon_generator/data/methods.csv index 36830b47..5e94a49f 100644 --- a/telethon_generator/data/methods.csv +++ b/telethon_generator/data/methods.csv @@ -282,7 +282,7 @@ messages.setBotPrecheckoutResults,both,ERROR_TEXT_EMPTY messages.setBotShippingResults,both,QUERY_ID_INVALID messages.setEncryptedTyping,user,CHAT_ID_INVALID messages.setGameScore,bot,PEER_ID_INVALID USER_BOT_REQUIRED -messages.setInlineBotResults,bot,ARTICLE_TITLE_EMPTY BUTTON_DATA_INVALID BUTTON_TYPE_INVALID BUTTON_URL_INVALID MESSAGE_EMPTY PHOTO_CONTENT_URL_EMPTY PHOTO_THUMB_URL_EMPTY QUERY_ID_INVALID REPLY_MARKUP_INVALID RESULT_TYPE_INVALID SEND_MESSAGE_MEDIA_INVALID SEND_MESSAGE_TYPE_INVALID START_PARAM_INVALID STICKER_DOCUMENT_INVALID USER_BOT_INVALID WEBDOCUMENT_URL_INVALID +messages.setInlineBotResults,bot,ARTICLE_TITLE_EMPTY AUDIO_TITLE_EMPTY BUTTON_DATA_INVALID BUTTON_TYPE_INVALID BUTTON_URL_INVALID MESSAGE_EMPTY PHOTO_CONTENT_URL_EMPTY PHOTO_THUMB_URL_EMPTY QUERY_ID_INVALID REPLY_MARKUP_INVALID RESULT_TYPE_INVALID SEND_MESSAGE_MEDIA_INVALID SEND_MESSAGE_TYPE_INVALID START_PARAM_INVALID STICKER_DOCUMENT_INVALID USER_BOT_INVALID WEBDOCUMENT_URL_INVALID messages.setInlineGameScore,bot,MESSAGE_ID_INVALID USER_BOT_REQUIRED messages.setTyping,both,CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ID_INVALID CHAT_WRITE_FORBIDDEN PEER_ID_INVALID USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT messages.startBot,user,BOT_INVALID PEER_ID_INVALID START_PARAM_EMPTY START_PARAM_INVALID