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的所需值。
更多回答
我是一名优秀的程序员,十分优秀!