diff --git a/LICENSE b/LICENSE index 7a430dd1..3f8fcce5 100755 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016-2019 LonamiWebs +Copyright (c) 2016-2020 LonamiWebs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/readthedocs/quick-references/client-reference.rst b/readthedocs/quick-references/client-reference.rst index 91b9bfe2..2331c8d5 100644 --- a/readthedocs/quick-references/client-reference.rst +++ b/readthedocs/quick-references/client-reference.rst @@ -65,6 +65,7 @@ Messages iter_messages get_messages pin_message + unpin_message send_read_acknowledge Uploads diff --git a/telethon/client/users.py b/telethon/client/users.py index b2243a2e..ddd41433 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -94,8 +94,10 @@ class UserMethods: if utils.is_list_like(request): request = request[request_index] - self._flood_waited_requests\ - [request.CONSTRUCTOR_ID] = time.time() + e.seconds + # SLOW_MODE_WAIT is chat-specific, not request-specific + if not isinstance(e, errors.SlowModeWaitError): + self._flood_waited_requests\ + [request.CONSTRUCTOR_ID] = time.time() + e.seconds # In test servers, FLOOD_WAIT_0 has been observed, and sleeping for # such a short amount will cause retries very fast leading to issues. diff --git a/telethon/tl/custom/conversation.py b/telethon/tl/custom/conversation.py index 9bccb382..570707e9 100644 --- a/telethon/tl/custom/conversation.py +++ b/telethon/tl/custom/conversation.py @@ -336,7 +336,12 @@ class Conversation(ChatGetter): future = self._client.loop.create_future() self._custom[counter] = (event, future) - return await self._get_result(future, start_time, timeout, self._custom, counter) + try: + return await self._get_result(future, start_time, timeout, self._custom, counter) + except asyncio.TimeoutError: + # Need to remove it from the dict if it times out, else we may + # try and fail to set the result later (#1618). + del self._custom[counter] async def _check_custom(self, built): for key, (ev, fut) in list(self._custom.items()): diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index 019ab3f8..d7203a03 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -1080,7 +1080,8 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): for row in self.reply_markup.rows: for button in row.buttons: if isinstance(button, types.KeyboardButtonSwitchInline): - if button.same_peer: + # no via_bot_id means the bot sent the message itself (#1619) + if button.same_peer or not self.via_bot_id: bot = self.input_sender if not bot: raise ValueError('No input sender') diff --git a/telethon/utils.py b/telethon/utils.py index 18723dc7..a5f95e9a 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -102,7 +102,7 @@ def get_display_name(entity): else: return '' - elif isinstance(entity, (types.Chat, types.Channel)): + elif isinstance(entity, (types.Chat, types.ChatForbidden, types.Channel)): return entity.title return '' @@ -1009,14 +1009,8 @@ def get_peer_id(peer, add_mark=True): if not add_mark: return peer.channel_id - # Concat -100 through math tricks, .to_supergroup() on - # Madeline IDs will be strictly positive -> log works. - try: - return -(peer.channel_id + pow( - 10, math.floor(math.log10(peer.channel_id) + 3))) - except ValueError: - raise TypeError('Cannot get marked ID of a channel ' - 'unless its ID is strictly positive') from None + # Growing backwards from -100_0000_000_000 indicates it's a channel + return -(1000000000000 + peer.channel_id) def resolve_id(marked_id): @@ -1167,15 +1161,20 @@ def resolve_bot_file_id(file_id): attributes=attributes, file_reference=b'' ) - elif (version == 2 and len(data) == 44) or (version == 4 and len(data) == 49): + elif (version == 2 and len(data) == 44) or (version == 4 and len(data) in (49, 77)): if version == 2: (file_type, dc_id, media_id, access_hash, volume_id, secret, local_id) = struct.unpack('""." PACK_SHORT_NAME_OCCUPIED,400,A stickerpack with this name already exists PARTICIPANTS_TOO_FEW,400,Not enough participants @@ -197,7 +206,7 @@ PASSWORD_MISSING,400,The account must have 2-factor authentication enabled (a pa PASSWORD_REQUIRED,400,The account must have 2-factor authentication enabled (a password) before this method can be used PASSWORD_TOO_FRESH_X,400,The password was added too recently and {seconds} seconds must pass before using the method PAYMENT_PROVIDER_INVALID,400,The payment provider was not recognised or its token was invalid -PEER_FLOOD,,Too many requests +PEER_FLOOD,400,Too many requests PEER_ID_INVALID,400,"An invalid Peer was used. Make sure to pass the right peer type and that the value is valid (for instance, bots cannot start conversations)" PEER_ID_NOT_SUPPORTED,400,The provided peer ID is not supported PERSISTENT_TIMESTAMP_EMPTY,400,Persistent timestamp empty @@ -205,10 +214,10 @@ PERSISTENT_TIMESTAMP_INVALID,400,Persistent timestamp invalid PERSISTENT_TIMESTAMP_OUTDATED,500,Persistent timestamp outdated PHONE_CODE_EMPTY,400,The phone code is missing PHONE_CODE_EXPIRED,400,The confirmation code has expired -PHONE_CODE_HASH_EMPTY,,The phone code hash is missing +PHONE_CODE_HASH_EMPTY,400,The phone code hash is missing PHONE_CODE_INVALID,400,The phone code entered was invalid PHONE_MIGRATE_X,303,The phone number a user is trying to use for authorization is associated with DC {new_dc} -PHONE_NUMBER_APP_SIGNUP_FORBIDDEN,400, +PHONE_NUMBER_APP_SIGNUP_FORBIDDEN,400,You can't sign up using this app PHONE_NUMBER_BANNED,400,The used phone number has been banned from Telegram and cannot be used anymore. Maybe check https://www.telegram.org/faq_spam PHONE_NUMBER_FLOOD,400,You asked for the code too many times. PHONE_NUMBER_INVALID,400 406,The phone number is invalid @@ -219,6 +228,7 @@ PHONE_PASSWORD_PROTECTED,400,This phone is password protected PHOTO_CONTENT_URL_EMPTY,400,The content from the URL used as a photo appears to be empty or has caused another HTTP error PHOTO_CROP_SIZE_SMALL,400,Photo is too small PHOTO_EXT_INVALID,400,The extension of the photo is invalid +PHOTO_ID_INVALID,400,Photo id is invalid PHOTO_INVALID,400,Photo invalid PHOTO_INVALID_DIMENSIONS,400,The photo dimensions are invalid (hint: `pip install pillow` for `send_file` to resize images) PHOTO_SAVE_FILE_INVALID,400,The photo you tried to send cannot be saved by Telegram. A reason may be that it exceeds 10MB. Try resizing it locally @@ -231,6 +241,7 @@ POLL_QUESTION_INVALID,400,The poll question was either empty or too long POLL_UNSUPPORTED,400,This layer does not support polls in the issued method PRIVACY_KEY_INVALID,400,The privacy key is invalid PRIVACY_TOO_LONG,400,Cannot add that many entities in a single request +PRIVACY_VALUE_INVALID,400,The privacy value is invalid PTS_CHANGE_EMPTY,500,No PTS change QUERY_ID_EMPTY,400,The query ID is empty QUERY_ID_INVALID,400,The query ID is invalid @@ -246,15 +257,16 @@ RANGES_INVALID,400,Invalid range provided REACTION_EMPTY,400,No reaction provided REACTION_INVALID,400,Invalid reaction provided (only emoji are allowed) REG_ID_GENERATE_FAILED,500,Failure while generating registration ID +REPLY_MARKUP_GAME_EMPTY,400,The provided reply markup for the game is empty REPLY_MARKUP_INVALID,400,The provided reply markup is invalid REPLY_MARKUP_TOO_LONG,400,The data embedded in the reply markup buttons was too much +RESULTS_TOO_MUCH,400,"You sent too many results, see https://core.telegram.org/bots/api#answerinlinequery for the current limit" RESULT_ID_DUPLICATE,400,Duplicated IDs on the sent results. Make sure to use unique IDs RESULT_ID_INVALID,400,The given result cannot be used to send the selection to the bot RESULT_TYPE_INVALID,400,Result type invalid -RESULTS_TOO_MUCH,400,You sent too many results. See https://core.telegram.org/bots/api#answerinlinequery for the current limit. RIGHT_FORBIDDEN,403,Either your admin rights do not allow you to do this or you passed the wrong rights combination (some rights only apply to channels and vice versa) -RPC_CALL_FAIL,,"Telegram is having internal issues, please try again later." -RPC_MCGET_FAIL,,"Telegram is having internal issues, please try again later." +RPC_CALL_FAIL,500,"Telegram is having internal issues, please try again later." +RPC_MCGET_FAIL,500,"Telegram is having internal issues, please try again later." RSA_DECRYPT_FAILED,400,Internal RSA decryption failed SCHEDULE_BOT_NOT_ALLOWED,400,Bots are not allowed to schedule messages SCHEDULE_DATE_TOO_LATE,400,The date you tried to schedule is too far in the future (last known limit of 1 year and a few hours) @@ -264,6 +276,7 @@ SEARCH_QUERY_EMPTY,400,The search query is empty SECONDS_INVALID,400,"Slow mode only supports certain values (e.g. 0, 10s, 30s, 1m, 5m, 15m and 1h)" SEND_MESSAGE_MEDIA_INVALID,400,The message media was invalid or not specified SEND_MESSAGE_TYPE_INVALID,400,The message type is invalid +SENSITIVE_CHANGE_FORBIDDEN,403,Your sensitive content settings cannot be changed at this time SESSION_EXPIRED,401,The authorization has expired SESSION_PASSWORD_NEEDED,401,Two-steps verification is enabled and a password is required SESSION_REVOKED,401,"The authorization has been invalidated, because of the user terminating all sessions" @@ -291,11 +304,12 @@ TAKEOUT_INVALID,400,The takeout session has been invalidated by another data exp TAKEOUT_REQUIRED,400,You must initialize a takeout request first TEMP_AUTH_KEY_EMPTY,400,No temporary auth key provided Timeout,-503,A timeout occurred while fetching data from the worker +THEME_MIME_INVALID,400,"You cannot create this theme, the mime-type is invalid" TMP_PASSWORD_DISABLED,400,The temporary password is disabled TOKEN_INVALID,400,The provided token is invalid TTL_DAYS_INVALID,400,The provided TTL is invalid TYPES_EMPTY,400,The types field is empty -TYPE_CONSTRUCTOR_INVALID,,The type constructor is invalid +TYPE_CONSTRUCTOR_INVALID,400,The type constructor is invalid UNKNOWN_METHOD,500,The method you tried to call cannot be called on non-CDN DCs UNTIL_DATE_INVALID,400,That date cannot be specified in this request (try using None) URL_INVALID,400,The URL used was invalid (e.g. when answering a callback with a URL that's not t.me/yourbot or your game's URL) diff --git a/telethon_generator/data/methods.csv b/telethon_generator/data/methods.csv index 26e5f113..8946f542 100644 --- a/telethon_generator/data/methods.csv +++ b/telethon_generator/data/methods.csv @@ -5,7 +5,7 @@ account.changePhone,user,PHONE_NUMBER_INVALID account.checkUsername,user,USERNAME_INVALID account.confirmPasswordEmail,user, account.confirmPhone,user,CODE_HASH_INVALID PHONE_CODE_EMPTY -account.createTheme,user, +account.createTheme,user,THEME_MIME_INVALID account.deleteSecureValue,user, account.finishTakeoutSession,user, account.getAccountTTL,user, @@ -49,7 +49,8 @@ account.sendVerifyEmailCode,user,EMAIL_INVALID account.sendVerifyPhoneCode,user, account.setAccountTTL,user,TTL_DAYS_INVALID account.setContactSignUpNotification,user, -account.setContentSettings,user, +account.setContentSettings,user,SENSITIVE_CHANGE_FORBIDDEN +account.setGlobalPrivacySettings,user,AUTOARCHIVE_NOT_AVAILABLE account.setPrivacy,user,PRIVACY_KEY_INVALID PRIVACY_TOO_LONG account.unregisterDevice,user,TOKEN_INVALID account.updateDeviceLocked,user, @@ -94,7 +95,7 @@ channels.editAdmin,both,ADMINS_TOO_MUCH ADMIN_RANK_EMOJI_NOT_ALLOWED ADMIN_RANK_ channels.editBanned,both,CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED USER_ADMIN_INVALID USER_ID_INVALID channels.editCreator,user,PASSWORD_MISSING PASSWORD_TOO_FRESH_X SESSION_TOO_FRESH_X channels.editLocation,user, -channels.editPhoto,both,CHANNEL_INVALID CHAT_ADMIN_REQUIRED PHOTO_INVALID +channels.editPhoto,both,CHANNEL_INVALID CHAT_ADMIN_REQUIRED FILE_REFERENCE_INVALID PHOTO_INVALID channels.editTitle,both,CHANNEL_INVALID CHAT_ADMIN_REQUIRED CHAT_NOT_MODIFIED channels.exportMessageLink,user,CHANNEL_INVALID channels.getAdminLog,user,CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED @@ -264,7 +265,7 @@ messages.requestUrlAuth,user, messages.saveDraft,user,PEER_ID_INVALID messages.saveGif,user,GIF_ID_INVALID messages.saveRecentSticker,user,STICKER_ID_INVALID -messages.search,user,CHAT_ADMIN_REQUIRED INPUT_CONSTRUCTOR_INVALID INPUT_USER_DEACTIVATED PEER_ID_INVALID PEER_ID_NOT_SUPPORTED SEARCH_QUERY_EMPTY USER_ID_INVALID +messages.search,user,CHAT_ADMIN_REQUIRED INPUT_CONSTRUCTOR_INVALID INPUT_FILTER_INVALID INPUT_USER_DEACTIVATED PEER_ID_INVALID PEER_ID_NOT_SUPPORTED SEARCH_QUERY_EMPTY USER_ID_INVALID messages.searchGifs,user,METHOD_INVALID SEARCH_QUERY_EMPTY messages.searchGlobal,user,SEARCH_QUERY_EMPTY messages.searchStickerSets,user, @@ -274,7 +275,7 @@ messages.sendEncryptedService,user,DATA_INVALID ENCRYPTION_DECLINED MSG_WAIT_FAI messages.sendInlineBotResult,user,CHAT_SEND_INLINE_FORBIDDEN CHAT_WRITE_FORBIDDEN INLINE_RESULT_EXPIRED PEER_ID_INVALID QUERY_ID_EMPTY SCHEDULE_DATE_TOO_LATE SCHEDULE_TOO_MUCH WEBPAGE_CURL_FAILED WEBPAGE_MEDIA_EMPTY messages.sendMedia,both,BOT_PAYMENTS_DISABLED BOT_POLLS_DISABLED BROADCAST_PUBLIC_VOTERS_FORBIDDEN CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED CHAT_SEND_MEDIA_FORBIDDEN CHAT_WRITE_FORBIDDEN EMOTICON_INVALID EXTERNAL_URL_INVALID FILE_PARTS_INVALID FILE_PART_LENGTH_INVALID FILE_REFERENCE_EMPTY FILE_REFERENCE_EXPIRED GAME_BOT_INVALID INPUT_USER_DEACTIVATED MEDIA_CAPTION_TOO_LONG MEDIA_EMPTY PAYMENT_PROVIDER_INVALID PEER_ID_INVALID PHOTO_EXT_INVALID PHOTO_INVALID_DIMENSIONS PHOTO_SAVE_FILE_INVALID POLL_ANSWERS_INVALID POLL_OPTION_DUPLICATE POLL_QUESTION_INVALID QUIZ_CORRECT_ANSWERS_EMPTY QUIZ_CORRECT_ANSWERS_TOO_MUCH QUIZ_CORRECT_ANSWER_INVALID QUIZ_MULTIPLE_INVALID RANDOM_ID_DUPLICATE SCHEDULE_DATE_TOO_LATE SCHEDULE_TOO_MUCH STORAGE_CHECK_FAILED Timeout USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT VIDEO_CONTENT_TYPE_INVALID WEBPAGE_CURL_FAILED WEBPAGE_MEDIA_EMPTY messages.sendMessage,both,AUTH_KEY_DUPLICATED BOT_DOMAIN_INVALID BUTTON_DATA_INVALID BUTTON_TYPE_INVALID BUTTON_URL_INVALID CHANNEL_INVALID CHANNEL_PRIVATE CHAT_ADMIN_REQUIRED CHAT_ID_INVALID CHAT_RESTRICTED CHAT_WRITE_FORBIDDEN ENTITIES_TOO_LONG ENTITY_MENTION_USER_INVALID INPUT_USER_DEACTIVATED MESSAGE_EMPTY MESSAGE_TOO_LONG MSG_ID_INVALID PEER_ID_INVALID POLL_OPTION_INVALID RANDOM_ID_DUPLICATE REPLY_MARKUP_INVALID REPLY_MARKUP_TOO_LONG SCHEDULE_BOT_NOT_ALLOWED SCHEDULE_DATE_TOO_LATE SCHEDULE_STATUS_PRIVATE SCHEDULE_TOO_MUCH Timeout USER_BANNED_IN_CHANNEL USER_IS_BLOCKED USER_IS_BOT YOU_BLOCKED_USER -messages.sendMultiMedia,both,SCHEDULE_DATE_TOO_LATE SCHEDULE_TOO_MUCH +messages.sendMultiMedia,both,MULTI_MEDIA_TOO_LONG SCHEDULE_DATE_TOO_LATE SCHEDULE_TOO_MUCH messages.sendScheduledMessages,user, messages.sendVote,user,MESSAGE_POLL_CLOSED OPTION_INVALID messages.setBotCallbackAnswer,both,QUERY_ID_INVALID URL_INVALID @@ -282,7 +283,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 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.setInlineBotResults,bot,ARTICLE_TITLE_EMPTY AUDIO_TITLE_EMPTY BUTTON_DATA_INVALID BUTTON_TYPE_INVALID BUTTON_URL_INVALID DOCUMENT_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 @@ -295,7 +296,7 @@ messages.updatePinnedMessage,both, messages.uploadEncryptedFile,user, messages.uploadMedia,both,BOT_MISSING MEDIA_INVALID PEER_ID_INVALID payments.clearSavedInfo,user, -payments.getBankCardData,user, +payments.getBankCardData,user,BANK_CARD_NUMBER_INVALID payments.getPaymentForm,user,MESSAGE_ID_INVALID payments.getPaymentReceipt,user,MESSAGE_ID_INVALID payments.getSavedInfo,user, @@ -311,8 +312,8 @@ phone.saveCallDebug,user,CALL_PEER_INVALID DATA_JSON_INVALID phone.setCallRating,user,CALL_PEER_INVALID photos.deletePhotos,user, photos.getUserPhotos,both,MAX_ID_INVALID USER_ID_INVALID -photos.updateProfilePhoto,user, -photos.uploadProfilePhoto,user,FILE_PARTS_INVALID IMAGE_PROCESS_FAILED PHOTO_CROP_SIZE_SMALL PHOTO_EXT_INVALID VIDEO_FILE_INVALID +photos.updateProfilePhoto,user,PHOTO_ID_INVALID +photos.uploadProfilePhoto,user,ALBUM_PHOTOS_TOO_MANY FILE_PARTS_INVALID IMAGE_PROCESS_FAILED PHOTO_CROP_SIZE_SMALL PHOTO_EXT_INVALID VIDEO_FILE_INVALID ping,both, reqDHParams,both, reqPq,both, @@ -321,7 +322,7 @@ rpcDropAnswer,both, setClientDHParams,both, stats.getBroadcastStats,user,BROADCAST_REQUIRED CHAT_ADMIN_REQUIRED STATS_MIGRATE_X stats.getMegagroupStats,user,CHAT_ADMIN_REQUIRED MEGAGROUP_REQUIRED STATS_MIGRATE_X -stats.loadAsyncGraph,user, +stats.loadAsyncGraph,user,GRAPH_OUTDATED_RELOAD stickers.addStickerToSet,bot,BOT_MISSING STICKERSET_INVALID stickers.changeStickerPosition,bot,BOT_MISSING STICKER_INVALID stickers.createStickerSet,bot,BOT_MISSING PACK_SHORT_NAME_INVALID PACK_SHORT_NAME_OCCUPIED PEER_ID_INVALID SHORTNAME_OCCUPY_FAILED STICKERS_EMPTY STICKER_EMOJI_INVALID STICKER_FILE_INVALID STICKER_PNG_DIMENSIONS STICKER_PNG_NOPNG USER_ID_INVALID