1. 我无法在简单的代码中插入for循环。该代码从电报聊天中接收新消息。要处理消息,您需要运行一个循环。该循环仅运行一次。每次有新消息到达时它都必须工作。该代码工作正常,但仅适用于第一条消息,因为......该循环根本不从第二条消息开始。
2.
from telethon import TelegramClient, sync, events
import time
api_id = *******
api_hash = '****************************'
area = open("area.txt", "r")
client = TelegramClient('test_tg', api_id, api_hash, device_model="iPhone 13 Pro Max",
system_version="14.8.1", app_version="8.4", lang_code="en", system_lang_code="en-US")
@client.on(events.NewMessage())
async def normal_handler(event):
if '5725556759' in str(event.message):
act_mes = str(event.message)
print(act_mes)
print('Message at {} UTC'.format(event.message.date))
for i in area:
print(i.rstrip())
if i.rstrip() in act_mes:
print(act_mes)
client.start()
client.run_until_disconnected()
如果你读了一次文件,就结束了,第二次循环不会读任何东西,文件就结束了。因此,每次
area循环之前都要重新打开文件。通过上下文管理器更好,即通过with。不然你还没有关闭,这样不太好,以后可能会出问题。并与经理它会自动关闭。