简体   繁体   English

无法从电报(电话)中获取消息

[英]Cannot get message from telegram (telethon)

I want to make an auto reply message with telethon python.我想用 Telethon python 制作自动回复消息。 But when I run, it cannot get message and cannot reply my message.但是当我运行时,它无法获取消息,也无法回复我的消息。

This is my code.这是我的代码。 Anyone can help me please任何人都可以帮助我

def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res

if __name__ == '__main__':
# Create the client and connect
# use sequential_updates=True to respond to messages one at a time
client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)

@client.on(events.NewMessage(incoming=True))
async def handle_new_message(event):  
    if event.is_private:  # only auto-reply to private chats
        from_ = await event.client.get_entity(event.from_id)  
        if not from_.bot:  # don't auto-reply to bots
            msg = events.NewMessage(incoming=True)
            if msg!='':
                message= cchatbot_response(msg)
                print(time.asctime(), '-', event.message)  # optionally log time and message
                time.sleep(1)  # pause for 1 second to rate-limit automatic replies
                await event.respond(message)


print(time.asctime(), '-', 'Auto-replying...')
client.start(phone, password)
client.run_until_disconnected()

I am able to run your code minus these lines我可以运行你的代码减去这些行

message = cchatbot_response(msg)
print(time.asctime(), '-', event.message)  # optionally log time and message
time.sleep(1)  # pause for 1 second to rate-limit automatic replies

Please check the cchatbot_response function.请检查 cchatbot_response 功能。

Also, I called this line另外,我称这条线

client.start(phone, password)

like this像这样

client.start()

It prompted me for my phone number and password but didn't mention it again.它提示我输入我的电话号码和密码,但没有再次提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM