【问题标题】:Cannot get message from telegram (telethon)无法从电报(电话)中获取消息
【发布时间】:2020-01-19 15:05:11
【问题描述】:

我想用 Telethon python 自动回复消息。但是当我运行时,它无法获取消息并且无法回复我的消息。

这是我的代码。谁能帮帮我

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()

【问题讨论】:

  • msg = event.text替换msg = events.NewMessage(incoming=True)
  • 您粘贴的原始代码本身不起作用,并且缺少某些方法。请提供一个完整的最小示例,以鼓励更多人帮助您。

标签: python bots telegram telethon


【解决方案1】:

我可以运行你的代码减去这些行

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

请检查 cchatbot_response 函数。

另外,我叫这条线

client.start(phone, password)

喜欢这个

client.start()

它提示我输入电话号码和密码,但没有再次提及。

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2022-11-05
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多