我的机器人在将图像从一个频道发送到另一个频道时无法保留图像的格式,也就是说,如果图像隐藏在扰流板下,则它会在没有扰流板的情况下到达。有可能以某种方式解决这个问题吗?
async def send_media(message, destination_channel_id):
try:
if message.media and isinstance(message.media, (MessageMediaPhoto, MessageMediaDocument)):
return await client.send_message(destination_channel_id, message.text, file=message.media)
except Exception as e:
print(f"Error: {e}")
我正在使用 Telethon 1.33
如有任何帮助,我将不胜感激。
Telethon 提供了一种下载与消息关联的文件的方法 (
download_media
),然后您可以使用适当的格式将它们上传到目标频道。
async def send_media(message, destination_channel_id):
try:
if message.media and isinstance(message.media, (MessageMediaPhoto, MessageMediaDocument)):
file = await client.download_media(message)
caption = message.text or '' # Preserve the caption if it exists
# Check if the message contains a spoiler tag
if 'spoiler' in message.text.lower():
caption = f"||{caption}||"
return await client.send_file(destination_channel_id, file, caption=caption)
except Exception as e:
print(f"Error: {e}")