簡體   English   中英

Telethon client.conversation() 不會工作

[英]Telethon client.conversation() won't work

嗨,我真的很絕望......

我嘗試做一個測驗,比如使用 telethon 與電報機器人對話。 它應該看起來像:

user1 > /start
bot > Hello who send you?
user1 > user0
bot > Thank you. So do you want in channel1 or channel2 or both?
user1 > channel1
*Bot invites the user

我使用了這個例子: https://arabic-telethon.readthedocs.io/en/stable/extra/examples/telegram-client.html#conversations-waiting-for-messages-or-replies示例代碼:

with bot.conversation(chat) as conv:
    conv.send_message('Hi!')
    hello = conv.get_response()

    conv.send_message('Please tell me your name')
    name = conv.get_response().raw_text
    while not any(x.isalpha() for x in name):
        conv.send_message("Your name didn't have any letters! Try again")
        name = conv.get_response().raw_text

    conv.send_message('Thanks {}!'.format(name))

但它不會運行。 在文檔中寫道,聊天代表一個實體。 所以我嘗試了發件人 ID 和用戶名...

我的代碼:

@bot.on(events.NewMessage(chats=bot.get_entity('Just Another Test Group'),blacklist_chats=True,pattern='/access'))
async def access(event):
    print("jop")
    temp = await event.get_sender()
    temp2 = await event.get_chat()
    print(temp)
    print(temp2.id)
    with bot.conversation(temp.id) as conv:
        print("started")
        event.respond("hmm")
        conv.send_message('Hi!')
        hello = conv.get_response()

        conv.send_message('Please tell me your name')
        name = conv.get_response().raw_text
        while not any(x.isalpha() for x in name):
            conv.send_message("Your name didn't have any letters! Try again")
            name = conv.get_response().raw_text

        conv.send_message('Thanks {}!'.format(name))

編輯:

我已經激活日志記錄並更改了一些代碼,因為我想主要問題是必須異步並且必須等待協程......它工作得更好但仍然存在問題

代碼:

@bot.on(events.NewMessage(chats=bot.get_entity('Just Another Test Group'),blacklist_chats=True,pattern='/access'))
    async def access(event):
temp = await event.get_sender()
    temp2 = await event.get_chat()
    print(temp)
    print(temp2.id)
    async with bot.conversation(temp.id) as conv:
        print("started")
        await event.respond("hmm")
        await conv.send_message('Hi!')
        # await hello = conv.get_response()

當我取消注釋最后一條評論時,出現錯誤:

await hello = conv.get_response()
    ^
SyntaxError: cannot assign to await expression

如果我不等待這個,我會得到:

 'wait_for' was never awaited
  await callback(event)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我還使用了記錄器,但不知道如何處理此消息:

started
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088190017728 to SendMessageRequest (216c20ca460)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 60 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088190017732 to MsgsAck (216c20ca970)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 44 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling RPC result for message 6882318088190017728
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088584285764 to SendMessageRequest (216c206bb50)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 60 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088584285768 to MsgsAck (216c20ca130)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling RPC result for message 6882318088584285764
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088923057584 to SendMessageRequest (216c20ca220)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 64 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318088923057588 to MsgsAck (216c20cac40)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling RPC result for message 6882318088923057584
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318225772327524 to PingRequest (216c206b430)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318225926609140 to MsgsAck (216c2055c40)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882318225772327524
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318483466030832 to PingRequest (216c206baf0)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318483476936100 to MsgsAck (216c206b9a0)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882318483466030832
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318741250045192 to PingRequest (216c206b880)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318741262651816 to MsgsAck (216c206b430)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882318741250045192
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318998993130716 to PingRequest (216c206ba60)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882318999009786636 to MsgsAck (216c206baf0)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882318998993130716
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319256720751452 to PingRequest (216c206b820)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319256729883836 to MsgsAck (216c206b880)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882319256720751452
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319514437753024 to PingRequest (216c206ba30)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319514450436892 to MsgsAck (216c206ba60)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882319514437753024
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319772162655792 to PingRequest (216c206bfd0)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882319772173152884 to MsgsAck (216c206b820)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882319772162655792
DEBUG:telethon.network.mtprotosender:Receiving items from the network...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882320029904364204 to PingRequest (216c206be80)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 28 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.extensions.messagepacker:Assigned msg_id = 6882320029914435004 to MsgsAck (216c206ba30)
DEBUG:telethon.network.mtprotosender:Encrypting 1 message(s) in 36 bytes for sending
DEBUG:telethon.network.mtprotosender:Encrypted messages put in a queue to be sent
DEBUG:telethon.network.mtprotosender:Waiting for messages to send...
DEBUG:telethon.network.mtprotosender:Handling pong for message 6882320029904364204
DEBUG:telethon.network.mtprotosender:Receiving items from the network...

問候和感謝

天哪……對不起。 這是關於電報 Web。

當我使用手機或桌面應用程序時,我會完美地重新保存所有內容......

我為此感到抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM