9

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

这是示例代码片段:

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 信息可以在这里找到。

谢谢你的耐心。

4

2 回答 2

7

从 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'))

是参考文档。

于 2019-10-15T07:36:44.580 回答
3

我正在努力将这些python-telegram-bot示例与上述建议联系起来。特别是在拥有contextandupdate时,我找不到chatidand bot。现在,我的两分钱:

pic=os.path.expanduser("~/a.png")
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(pic,'rb'))
于 2020-09-03T13:27:45.433 回答