Python Telegram Bot - 发送图像 [英] Python Telegram Bot - Send Image

查看:68
本文介绍了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'))

这里是参考文档.

这篇关于Python Telegram Bot - 发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆