当达到列表中的一定数量的行(计数行)时,Telethon定期重复该操作

发布于 2025-02-10 19:38:31 字数 789 浏览 2 评论 0原文

请帮助我执行取决于行数的操作。因此,该程序的任务是跟踪该组中有多少消息,然后当这些消息的值达到一定数字时,执行操作以将消息发送给同一组。

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

当前,该程序分别向我显示每个消息,并计算此特定消息中字符的数量,然后继续进行下一个消息。

我感谢您的耐心:)

非常感谢您

Please help me with the execution of an action that depends on the number of lines. So, the task of the program is to track how many messages were in the group, and then when the value of these messages reaches a certain number, perform an action to send a message to the same group.

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

Currently, the program shows me each message separately and counts the number of characters in this particular message, and then proceeds to the next message.

I am grateful for your patience :)

Thank you very much in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2025-02-17 19:38:31
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()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文