请帮助我执行一个取决于行数的操作。因此,程序的任务是跟踪该组中有多少条消息,然后当这些消息的值达到一定数量时,执行一个动作向同一组发送消息。
from telethon import TelegramClient, sync
from telethon.events import NewMessage
api_id = '____'
api_hash = '______'
client = TelegramClient('session_name', api_id, api_hash)
@client.on(event=NewMessage(chats=('https://t.me/some_group')))
async def normal_handler(event):
value = event.message.to_dict()['message']
lines = 0
for line in value:
lines += 1
print(lines)
if lines % 2: #the task is to send messages every 2 income messages
await client.send_message('https://t.me/some_group', message='something')
client.start()
client.run_until_disconnected()
目前,程序分别向我显示每条消息,并计算这条消息中的字符数,然后继续执行下一条消息。
我很感激你的耐心:)
提前谢谢你
from telethon import TelegramClient, sync
from telethon.events import NewMessage
api_id = '______'
api_hash = '______'
client = TelegramClient('session_name', api_id, api_hash)
@client.on(event=NewMessage(chats=('https://t.me/_____')))
async def normal_handler(event):
value = event.message.to_dict()['message']
file = open('text.txt', 'r+')
read = file.read()
file.write(value + 'n')
read_str = ''.join(read)
splited = read_str.split('n')
number = len(splited)
if number >= 15:
file.truncate(0)
print('Sending the message')
await client.send_message('_____', message='Строк больше 15')
print(number)
file.close()
client.start()
client.run_until_disconnected()