ValueError: Could not find the input entity for PeerUser

ValueError: Could not find the input entity for PeerUser

我在解决我之前的问题后遇到了这个错误,这是日志:

    Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=981588717) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=1658148281) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=965240226) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=1964324322) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=1660769244) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=1893756687) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=1834060740) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
Unhandled exception on handle_new_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 38, in handle_new_message
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 287, in get_entity
    inputs.append(await self.get_input_entity(x))
  File "C:\Users\user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\telethon\client\users.py", line 461, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=716702203) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
U

这是代码:

    #!/usr/bin/env python3
# A simple script to print some messages.
import time
import re
import json
import random
import os
from asyncio import sleep
from pprint import pprint

from telethon import TelegramClient, events, utils
from dotenv import load_dotenv

load_dotenv() # get .env variable

session = os.environ.get('TG_SESSION', 'printer')
api_id = os.getenv("API_ID")
api_hash = os.getenv("API_HASH")
debug_mode = os.getenv("DEBUG_MODE").upper() == "TRUE"

proxy = None  # https://github.com/Anorov/PySocks

# Create and start the client so we can make requests (we don't here)
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()

# create a sender list to check if user already send private message or mention
senderList = [] 

#read json file and prepare quiz to send later
with open('quizzes.json') as json_file:
    quizzes = json.load(json_file)

@client.on(events.NewMessage)
async def handle_new_message(event):
    
    me = await client.get_me()
    username = me.username
    from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
    to_ = await event.client.get_entity(event.message.to_id)

    needToProceed = from_.is_self if debug_mode else not from_.is_self and (event.is_private or re.search("@"+me.username,event.raw_text))
    if needToProceed:  # only auto-reply to private chats:  # only auto-reply to private chats   
        if not from_.bot and event:  # don't auto-reply to bots
            print(time.asctime(), '-', event.message)  # optionally log time and message
            await sleep(1)  # pause for 1 second to rate-limit automatic replies   
            message = ""
            senderList.append(to_.id)
            if senderList.count(to_.id) < 2:
                message =   f"""**AUTO REPLY**
                \nHi @{from_.username},
                \n\nMohon maaf boss saya sedang offline, mohon tunggu sebentar.
                \nSilahkan lihat-lihat [imacakes](https://www.instagram.com/ima_cake_cirebon) dulu untuk cuci mata.
                \n\n**AUTO REPLY**"""
            elif senderList.count(to_.id) < 3:
                message =   f"""**AUTO REPLY**
                \nMohon bersabar @{from_.username}, boss saya masih offline """
            elif senderList.count(to_.id) < 4:
                message = f"""**AUTO REPLY** 
                \n@{from_.username} Tolong bersabar yaa """
            else:
                random_number = random.randint(0,len(quizzes) - 1)
                question = quizzes[random_number]['question']
                answer = quizzes[random_number]['answer']
                message = f"""**AUTO REPLY**
                \n @{from_.username}, Main tebak-tebakan aja yuk 
                \n {question}
                \n {answer}
                \n """
            
            if message != "":
                await event.reply(message)

client.start()
client.run_until_disconnected()

我真的不知道怎么了,因为我是新手,对编码了解不多,所以如果有人愿意帮助我,我真的很感激:)

也感谢@ogdenkev帮我解决了之前的问题:)

谢谢大家的帮助:)

我不熟悉Telethon,但是docs referenced in the error状态

To “encounter” an ID, you would have to “find it” like you would in the normal app. If the peer is in your dialogs, you would need to client.get_dialogs(). If the peer is someone in a group, you would similarly client.get_participants(group).

Once you have encountered an ID, the library will (by default) have saved their access_hash for you, which is needed to invoke most methods. This is why sometimes you might encounter this error when working with the library. You should except ValueError and run code that you know should work to find the entity.

并且 summary 提示您可能需要尝试在不同的地方找到该实体。尽管理想情况下您已经知道 正确 查找实体的方法。在您的代码中,这可能类似于。

@client.on(events.NewMessage)
async def handle_new_message(event):
    
    me = await client.get_me()
    username = me.username
    try:
        from_ = await event.client.get_entity(event.from_id)
    except ValueError:
        pass
    try:
        # Do you have a conversation open with them? Get dialogs.
        await client.get_dialogs()
    except ValueError:
        pass
    try:
        # Are they participant of some group? Get them.
        await client.get_participants('username')
    except ValueError:
        pass
    if from_ is None:
        raise ValueError("I could not find the user")