【问题标题】:Python Telegram Bot - Send ImagePython Telegram Bot - 发送图片
【发布时间】:2015-11-26 09:49:08
【问题描述】:

我想根据要求发送一张图片(通过 URL 或路径)。 我使用源code here. 该代码已经有一个发送图像的示例(通过 URL 或路径),但我不明白,因为我是 Python 新手。

这里是示例代码sn-p:

elif text == '/image':        #request
    img = Image.new('RGB', (512, 512))
    base = random.randint(0, 16777216)
    pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
    img.putdata(pixels)
    output = StringIO.StringIO()
    img.save(output, 'JPEG')
    reply(img=output.getvalue())

一些API信息可以是found here.

感谢您的耐心等待。

【问题讨论】:

标签: python image api telegram-bot


【解决方案1】:

从 URL 发送照片:

bot.send_photo(chat_id=chat_id, photo='https://telegram.org/img/t_logo.png')

从本地云端硬盘发送照片:

bot.send_photo(chat_id=chat_id, photo=open('tests/test.png', 'rb'))

Here 是参考文档。

【讨论】:

  • Oof +1,到目前为止,python-telegram-bot 可以理解的唯一清晰示例
  • @RaahimFareed 很高兴它有帮助:)
【解决方案2】:

我很难将python-telegram-bot 示例与上述建议联系起来。特别是在拥有contextupdate 时,我找不到chatidbot。现在,我的两分钱:

pic=os.path.expanduser("~/a.png")
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(pic,'rb'))

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 2017-06-06
    • 2021-04-24
    • 2018-11-12
    • 2015-09-20
    • 2019-11-18
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多