有什么方法可以使用 telethon 知道电报 post 照片的 url 吗?

is there any way to know the url of telegram post photo using telethon?

我正在使用 python 和电视节目来获取消息.. 我可以下载消息照片,但我不想存储和下载。 我想知道照片的 url 以便以后随时访问它。 有什么办法吗?

telegram API 不会让您直接 URL 到帖子的媒体文件。但是,作为解决方法,请查看 Telegram 几个月前添加到其网站的新功能:

您只需按以下模式输入 URLs 即可在网络上看到 public 个频道的帖子: https://t.me/"channel-username"/"post-No"

(例如 https://t.me/gizmiztel/2350

这样您就可以解析 DOM 并找到每个媒体文件的直接 URL。

注意:您可能需要针对每种类型的文件使用单独的方法来提取文件的 URLs。

@tashakori 回答的扩展

对于没有设置用户名的频道(我见过几个),它是https://t.me/c/channel_id/message_id

例如

async def main():
    # TODO setup client etc.
    # this channel id was obtained from using something similar to 
    channel = await client.get_entity(-1001006503122)
    async for message in client.iter_messages(channel, reverse=True):
        # channel.id and the above -100.. id are not same
        # this channel.id looks like 1006503122
        print("message URL", f"https://t.me/c/{channel.id}/{message.id}")

另请注意:当然,只有加入频道的用户才能访问这些链接。

还有 message.chat_id 属性 其中 returns -100...