简体   繁体   English

有没有办法知道使用te​​lethon的电报发布照片的网址?

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

im using python and telethon to get messages.. I can download the message photos but i dont want to store and download.我使用python和telethon来获取消息..我可以下载消息照片,但我不想存储和下载。 I want to know the url of the photo to access it any time later using that.我想知道照片的网址,以便以后随时使用它访问它。 is there any way to do that?有没有办法做到这一点?

telegram APIs won't give you direct URL to media files of posts. 电报API不会为您提供直接到帖子媒体文件的URL。 but, as a workaround, take a look at the new feature that Telegram has added to its website a few months ago: 但是,作为一种解决方法,请看一下几个月前Telegram在其网站上添加的新功能:

you can see posts of public channels in the web just by typing URLs with this pattern: https://t.me/ "channel-username"/"post-No" 您只需输入以下格式的网址即可查看网络中公共频道的帖子: https : //t.me/ “ channel-username” /“ post-No”

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

this way you can parse the DOM and find the direct URL to each media file. 这样,您可以解析DOM并找到每个媒体文件的直接URL。

Note: you may need a separate method for each type of files to extract files' URLs. 注意:对于每种类型的文件,您可能需要一种单独的方法来提取文件的URL。

An extension to @tashakori's answer @tashakori 答案的扩展

For channels that have no username set (I've seen a few), it is https://t.me/c/channel_id/message_id对于没有设置用户名的频道(我见过几个),它是https://t.me/c/channel_id/message_id

eg.例如。

async def main():
    # TODO setup client etc.
    # this channel id was obtained from using something similar to https://stackoverflow.com/a/62849271/8608146
    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}")

Also note: These links are accessible only to users who are joined in the channel, of course.另请注意:当然,只有加入频道的用户才能访问这些链接。

There's also the message.chat_id property which returns the -100...还有message.chat_id属性返回 -100 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM