如何从电报机器人发送图像 [英] How to send an image from a telegram bot

查看:88
本文介绍了如何从电报机器人发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的机器人现在可以工作了,但问题是它只能发送文本.我在 Bot API 中看到有发送照片、视频的功能......但我无法让它工作.有人实现了吗?我正在使用来自 yukuku/telebot

I have my bot working by now, but the thing is it can only send text. I have seen in the Bot API there are functions to send photos, videos... but I can't get it to work. Someone has achieved it? I'm using python source code from yukuku/telebot

 elif text == '/image':
            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())

当我更改代码时,什么也没发生.

When I change the code, nothing happened.

img = Image.open('image.png')
img.show()

请帮帮我.我需要正确的代码.抱歉我的英语不好.

Please help me. I need the correct code. Sorry for my bad English.

推荐答案

解决方案是

elif 'Hi' in text:
reply(img=urllib2.urlopen('img url').read())

if text == 'help':
            reply(img=urllib2.urlopen('img url').read())

这篇关于如何从电报机器人发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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