Telethon将名称更改为当前时间

2024-06-25 22:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让我的申请书每分钟都把电报上我的名字改成现在的时间。我已经试过做点什么,但没有用

from telethon import TelegramClient
from telethon.tl.functions.account import UpdateProfileRequest
import asyncio
import datetime

today = datetime.datetime.today()
time= today.strftime("%H.%M")
 
api_id = 123456
api_hash = 'ххх'
client = TelegramClient('session_name', api_id, api_hash)
client.start()

async def main():
    while True:  
        await client(UpdateProfileRequest(first_name=time))
        await asyncio.sleep(1)
    
client.loop.run_forever()

Tags: namefromimportclientapiasyncioidtoday
2条回答

第一个东西不使用while循环,它可能会占用太多内存并禁用telethon的句柄更新 第二件事1秒钟太快了,电报可能会禁止你的垃圾邮件帐户 我更喜欢用aiocron

使用以下命令安装aiocron

pip3 install aiocron

代码:

import asyncio, aiocron, datetime
from telethon import TelegramClient, events, sync, functions, types
from telethon.tl.functions.account import UpdateProfileRequest

api_id = 123456
api_hash = "ххх"
client = TelegramClient("session_name", api_id, api_hash)
client.start()


@aiocron.crontab("*/1 * * * *")
async def set_clock():
    time = datetime.datetime.today().strftime("%H.%M")
    async with client:
        await client(UpdateProfileRequest(first_name=time))


@client.on(events.NewMessage)
async def e(event):
    if event.raw_text == "ping":
        await event.reply("pong")


client.run_until_disconnected()
from telethon import TelegramClient
from telethon.tl.functions.account import UpdateProfileRequest
import asyncio
import datetime
 
api_id = 123456
api_hash = 'ххх'
client = TelegramClient('session_name', api_id, api_hash)
client.start()

async def main():
    while True:
        time = datetime.datetime.today().strftime("%H.%M")
        async with client:
            await client(UpdateProfileRequest(first_name=time))
            await asyncio.sleep(60)
    
asyncio.get_event_loop().run_until_complete(main())

相关问题 更多 >