【问题标题】:is there any way to know the url of telegram post photo using telethon?有没有办法知道使用 Telethon 的电报张贴照片的网址?
【发布时间】:2019-02-27 04:42:54
【问题描述】:

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

【问题讨论】:

    标签: python telegram telethon


    【解决方案1】:

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

    您只需键入以下格式的 URL,即可在网络上查看公共频道的帖子: https://t.me/"channel-username"/"post-No"

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

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

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

    【讨论】:

      【解决方案2】:

      @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 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}")
      

      另请注意:当然,这些链接仅供加入频道的用户访问。

      还有 message.chat_id 属性返回 -100...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-16
        • 1970-01-01
        • 2010-12-24
        • 2019-03-13
        • 2018-05-21
        • 2015-12-29
        • 2019-06-26
        相关资源
        最近更新 更多