Python Telegram Bot - 发送图像

问题描述:

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

I'd like to send a Image (via URL or Path), on request. I use the source code here. The code has already a sample to send an image (via URL or Path), but I don't get it since I'm new to 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 信息可以在这里找到.

Some API infos can be found here.

感谢您的耐心等待.

从 URL 发送照片:

To send a photo from URL:

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

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

To send a photo from local Drive:

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

这里是参考文档.