我正在尝试运行Telethon 文档提供的第一个代码片段。但是,在出现多个问题(此处和此处)之后,我最终得到了这个修改后的版本:
import os
import sys
from telethon.sync import TelegramClient, events
# import nest_asyncio
# nest_asyncio.apply()
session_name = "<session_name>"
api_id = <api_id>
api_hash = "<api_hash>"
os.chdir(sys.path[0])
if f"{session_name}.session" in os.listdir():
os.remove(f"{session_name}.session")
async with TelegramClient(session_name, api_id, api_hash) as client:
client.send_message('me', 'Hello, myself!')
print(client.download_profile_photo('me'))
@client.on(events.NewMessage(pattern='(?i).*Hello'))
async def handler(event):
await event.reply('Hey!')
client.run_until_disconnected()
Run Code Online (Sandbox Code Playgroud)
但是现在我收到这些警告:
usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:23:RuntimeWarning:协程“MessageMethods.send_message”从未等待 RuntimeWarning:启用tracemalloc以获取对象分配回溯 /usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:24:RuntimeWarning:从未等待协程“DownloadMethods.download_profile_photo” RuntimeWarning:启用tracemalloc以获取对象分配回溯 /usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:30:RuntimeWarning:从未等待协程“UpdateMethods._run_until_disconnected” RuntimeWarning:启用tracemalloc以获取对象分配回溯
在 Jupyter 上运行代码时。现在我的问题是:
@行首的符号是什么@client.on...意思?那条线应该做什么?从这一行开始我就不明白代码了。如果您能帮助我理解它,我将不胜感激。