skip to Main Content

I want my application to change my name in the telegram to the current time every minute. I have already tried to do something, but to no avail

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

2

Answers


  1. 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())
    
    Login or Signup to reply.
  2. first stuff don’t use while loop , it may use too memory and disable handle updates from telethon
    second stuff 1 second its too fast and telegram may ban you account for spam
    I prefer to use aiocron

    Install aiocron using the following command

    pip3 install aiocron
    

    Code:

    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()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search