如何在 telehton newMessage 事件中识别和下载图像?
how to identify and download images in telehton newMessage event?
我写了一个简单的 python 脚本来使用 telethon 事件处理程序将用户看到的所有消息保存到文件中:
@CLIENT.on(events.NewMessage)
async def my_event_handler(event):
sender = await event.get_sender()
chat_id = event.chat_id
out ='\n\n' + sender.username + ': ' + event.text + ' [' + str(chat_id) + ']'
name = hashlib.sha1(out.encode('utf-8')).hexdigest()
outdir = ECHODIR + '/' + str(chat_id)
f_h = open(outdir + '/' + name, 'a')
f_h.write(out)
f_h.close()
CLIENT.start()
CLIENT.run_until_disconnected()
如何检测已收到图片并从活动中下载图片?
p.s。删除了不必要的代码,例如检查目录是否存在
根据此消息中的 Objects Reference summary for Message
, the message.photo
property will be "The Photo 媒体,如果有的话。"。
这意味着,要检测代码中的图像(或照片),您可以执行以下操作:
if event.photo:
...
Message
methods also contains a message.download_media()
这样:
saved_path = await event.download_media(optional_path)
我写了一个简单的 python 脚本来使用 telethon 事件处理程序将用户看到的所有消息保存到文件中:
@CLIENT.on(events.NewMessage)
async def my_event_handler(event):
sender = await event.get_sender()
chat_id = event.chat_id
out ='\n\n' + sender.username + ': ' + event.text + ' [' + str(chat_id) + ']'
name = hashlib.sha1(out.encode('utf-8')).hexdigest()
outdir = ECHODIR + '/' + str(chat_id)
f_h = open(outdir + '/' + name, 'a')
f_h.write(out)
f_h.close()
CLIENT.start()
CLIENT.run_until_disconnected()
如何检测已收到图片并从活动中下载图片?
p.s。删除了不必要的代码,例如检查目录是否存在
根据此消息中的 Objects Reference summary for Message
, the message.photo
property will be "The Photo 媒体,如果有的话。"。
这意味着,要检测代码中的图像(或照片),您可以执行以下操作:
if event.photo:
...
Message
methods also contains a message.download_media()
这样:
saved_path = await event.download_media(optional_path)