擅长:python、mysql、java
<p>第一个东西不使用while循环,它可能会占用太多内存并禁用telethon的句柄更新
第二件事1秒钟太快了,电报可能会禁止你的垃圾邮件帐户
我更喜欢用aiocron</p>
<p>使用以下命令安装aiocron</p>
<pre><code>pip3 install aiocron
</code></pre>
<p>代码:</p>
<pre><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()
</code></pre>