diff --git a/telethon/tl/entity_database.py b/telethon/tl/entity_database.py index b0fc70fb..0c92c75f 100644 --- a/telethon/tl/entity_database.py +++ b/telethon/tl/entity_database.py @@ -24,7 +24,12 @@ class EntityDatabase: self._entities = {} # marked_id: user|chat|channel if input_list: - self._input_entities = {k: v for k, v in input_list} + # TODO For compatibility reasons some sessions were saved with + # 'access_hash': null in the JSON session file. Drop these, as + # it means we don't have access to such InputPeers. Issue #354. + self._input_entities = { + k: v for k, v in input_list if v is not None + } else: self._input_entities = {} # marked_id: hash @@ -69,8 +74,17 @@ class EntityDatabase: try: p = utils.get_input_peer(e, allow_self=False) - new_input[utils.get_peer_id(p, add_mark=True)] = \ - getattr(p, 'access_hash', 0) # chats won't have hash + marked_id = utils.get_peer_id(p, add_mark=True) + + if isinstance(p, InputPeerChat): + # Chats don't have a hash + new_input[marked_id] = 0 + elif p.access_hash: + # Some users and channels seem to be returned without + # an 'access_hash', meaning Telegram doesn't want you + # to access them. This is the reason behind ensuring + # that the 'access_hash' is non-zero. See issue #354. + new_input[marked_id] = p.access_hash if self.enabled_full: if isinstance(e, (User, Chat, Channel)): diff --git a/telethon/utils.py b/telethon/utils.py index afb24b16..a05a4990 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -84,13 +84,13 @@ def get_input_peer(entity, allow_self=True): if entity.is_self and allow_self: return InputPeerSelf() else: - return InputPeerUser(entity.id, entity.access_hash) + return InputPeerUser(entity.id, entity.access_hash or 0) if isinstance(entity, (Chat, ChatEmpty, ChatForbidden)): return InputPeerChat(entity.id) if isinstance(entity, (Channel, ChannelForbidden)): - return InputPeerChannel(entity.id, entity.access_hash) + return InputPeerChannel(entity.id, entity.access_hash or 0) # Less common cases if isinstance(entity, UserEmpty): @@ -120,7 +120,7 @@ def get_input_channel(entity): return entity if isinstance(entity, (Channel, ChannelForbidden)): - return InputChannel(entity.id, entity.access_hash) + return InputChannel(entity.id, entity.access_hash or 0) if isinstance(entity, InputPeerChannel): return InputChannel(entity.channel_id, entity.access_hash) @@ -140,7 +140,7 @@ def get_input_user(entity): if entity.is_self: return InputUserSelf() else: - return InputUser(entity.id, entity.access_hash) + return InputUser(entity.id, entity.access_hash or 0) if isinstance(entity, InputPeerSelf): return InputUserSelf()