gpt4 book ai didi

Why does the bot on Telethon, when authorized as a user, not respond to messages?(为什么Telethon上的机器人在被授权为用户时不回复消息?)

转载 作者:bug小助手 更新时间:2023-10-28 10:24:42 25 4
gpt4 key购买 nike



I'm creating a chatbot autoresponder that should respond to messages based on a template. The script is up and running:

我正在创建一个聊天机器人自动响应器,应该会响应基于模板的消息。该脚本已启动并正在运行:


from telethon.sync import TelegramClient, events
import asyncio

from config import API_ID, API_HASH, BOT_TOKEN

api_id = API_ID
api_hash = API_HASH
bot_token = BOT_TOKEN

client = TelegramClient('session_name', api_id, api_hash)

@client.on(events.NewMessage(pattern='(?i)hello.+'))
async def main(event):
if not await client.is_user_authorized():
await client.start(bot_token=BOT_TOKEN)

await event.reply('Hey!')

with client:
client.run_until_disconnected()

But the user, under whom it is authorized with API_ID and API_HASH, is not responding. And it doesn't give any errors. It's as if nothing is working at all.

但是授权它使用API_ID和API_HASH的用户没有响应。而且它不会给出任何错误。这就好像什么都不起作用一样。


更多回答
优秀答案推荐

The error in the code is that the client.run_until_disconnected() method is not a valid method in the TelegramClient class.

代码中的错误在于,Client.run_Until_DisConnected()方法不是Telegram Client类中的有效方法。


To resolve the problem, you should replace the line client.run_until_disconnected() with client.start() to start the client and keep it running until it is disconnected. Here's the corrected code:

要解决这个问题,您应该用client.start()替换行client.run_Until_DisConnected(),以启动客户端并保持其运行,直到断开连接。以下是更正后的代码:


from telethon.sync import TelegramClient, events
import asyncio

from config import API_ID, API_HASH, BOT_TOKEN

api_id = API_ID
api_hash = API_HASH
bot_token = BOT_TOKEN

client = TelegramClient('session_name', api_id, api_hash)

@client.on(events.NewMessage(pattern='(?i)hello.+'))
async def main(event):
if not await client.is_user_authorized():
await client.start(bot_token=BOT_TOKEN)

await event.reply('Hey!')

client.start()
client.run_until_disconnected()

Make sure you have the required values for API_ID, API_HASH, and BOT_TOKEN defined in your config.py file.

确保在config.py文件中定义了API_ID、API_HASH和BOT_TOKEN的所需值。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com