Telegram-bot(telepot api):是否可以直接从 URL 发送图像而不保存它 [英] Telegram-bot (telepot api): Is it possible to send an image directly from URL without saving it
问题描述
我正在使用 python telepot api 编写电报机器人.我现在被困在我想发送直接来自 URL 而不将其存储在本地的图片的点.Telepot 提供了以下发送照片的说明:
<预><代码>>>>f = open('zzzzzzzz.jpg', 'rb') # 本地磁盘上的一些文件>>>响应 = bot.sendPhoto(chat_id, f)现在正在使用
f = urllib2.urlopen('http://i.imgur.com/B1fzGoh.jpg')bot.sendPhoto(chat_id, f)
这里的问题是 urllib2.urlopen('url')
为我提供了类似文件的对象,例如:
而不是像 open('myFile.jpg', 'rb')
这样的文件对象:
<open file 'app-root/runtime/repo/myImage.jpg', mode 'rb' at 0x7fac8f322540>
如果我在 sendPhoto() 中发送类似文件的对象,我会收到以下错误:回溯(最近一次调用):
[Wed Feb 10 06:21:09 2016] [error] 文件/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py",第 340 行,在句柄中[2016 年 2 月 10 日星期三 06:21:09] [错误] 回调(更新 ['message'])[2016 年 2 月 10 日星期三 06:21:09] [错误] 文件/var/lib/openshift/56b8e2787628e1484a00013e/app-root/runtime/repo/moviequiz_main.py",第 35 行,在句柄中[2016 年 2 月 10 日星期三 06:21:09] [错误] 响应 = bot.sendPhoto(chat_id, gif)[2016 年 2 月 10 日星期三 06:21:09] [错误] 文件/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py",第 230 行,在发送照片[2016 年 2 月 10 日星期三 06:21:09] [错误] 返回 self._sendFile(photo, 'photo', p)[2016 年 2 月 10 日星期三 06:21:09] [错误] 文件/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py",第 226 行,在_发送文件[2016 年 2 月 10 日星期三 06:21:09] [错误] 返回 self._parse(r)[2016 年 2 月 10 日星期三 06:21:09] [错误] 文件/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py",第 172 行,在_解析[2016 年 2 月 10 日星期三 06:21:09] [错误] 引发 BadHTTPResponse(response.status_code, response.text)[2016 年 2 月 10 日星期三 06:21:09] [错误] BadHTTPResponse: (414, u'<html>\\r\\n<head><title>414 Request-URI Too Large</title></head>\\r\\n<body bgcolor="white">\\r\\n<center><h1>414 Request-URI Too Large</h1></center>\\r\\n<hr><center>nginx/1.9.1</center>\\r\\n</body>\\r\\n</html>\\r\\n')
有一个针对此处提供的不同电报机器人项目的解决方案,他们将urllib2.urlopen('url').read()
回到电报,但在我的情况下,这会产生与没有 .read() 相同的错误.
如何从 url 获取文件作为文件对象(最好不要在本地保存)?或者如何从 urlopen() 提供的类文件对象"中获取文件对象"?
感谢您的帮助:)
在当前 Bot Api 2.3.1,您可以简单地将文件的 url 发送到服务器:
bot.sendPhoto(chat_id, "http://i.imgur.com/B1fzGoh.jpg");
就是这样.
您甚至不需要下载它,<a href="https://core.telegram.org/bots/api#sendphoto" rel="nofollow noreferrer">Telegram 会通过本身.
Im writing a telegram bot using the python telepot api. I'm now stuck at the point where I want to send a picture which directly comes from an URL without storing it locally. Telepot provides the following instruction to send a photo:
>>> f = open('zzzzzzzz.jpg', 'rb') # some file on local disk
>>> response = bot.sendPhoto(chat_id, f)
Now im using
f = urllib2.urlopen('http://i.imgur.com/B1fzGoh.jpg')
bot.sendPhoto(chat_id, f)
The problem here is that urllib2.urlopen('url')
provide me file-like object like:
<addinfourl at 140379102313792 whose fp = <socket._fileobject object at 0x7fac8e86d750>>
and not like open('myFile.jpg', 'rb')
a file object like:
<open file 'app-root/runtime/repo/myImage.jpg', mode 'rb' at 0x7fac8f322540>
If I send the file-like object in sendPhoto() I get the following error: Traceback (most recent call last):
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 340, in handle
[Wed Feb 10 06:21:09 2016] [error] callback(update['message'])
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/app-root/runtime/repo/moviequiz_main.py", line 35, in handle
[Wed Feb 10 06:21:09 2016] [error] response = bot.sendPhoto(chat_id, gif)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 230, in sendPhoto
[Wed Feb 10 06:21:09 2016] [error] return self._sendFile(photo, 'photo', p)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 226, in _sendFile
[Wed Feb 10 06:21:09 2016] [error] return self._parse(r)
[Wed Feb 10 06:21:09 2016] [error] File "/var/lib/openshift/56b8e2787628e1484a00013e/python/virtenv/lib/python2.7/site-packages/telepot/__init__.py", line 172, in _parse
[Wed Feb 10 06:21:09 2016] [error] raise BadHTTPResponse(response.status_code, response.text)
[Wed Feb 10 06:21:09 2016] [error] BadHTTPResponse: (414, u'<html>\\r\\n<head><title>414 Request-URI Too Large</title></head>\\r\\n<body bgcolor="white">\\r\\n<center><h1>414 Request-URI Too Large</h1></center>\\r\\n<hr><center>nginx/1.9.1</center>\\r\\n</body>\\r\\n</html>\\r\\n')
There is a solution for a different telegram-bot project provided here where they send the urllib2.urlopen('url').read()
back to telegram but in my case this generates the same error as without .read() .
How could I get the file from the url as file object (best would be without saving it locally)? Or how do I get the "file object" out of the "file-like object" provided by urlopen()?
Thanks for any help :)
In current Bot Api 2.3.1, You can simply send url of file to the server:
bot.sendPhoto(chat_id, "http://i.imgur.com/B1fzGoh.jpg");
That's it.
You don't even need to download it, Telegram would upload it by itself.
这篇关于Telegram-bot(telepot api):是否可以直接从 URL 发送图像而不保存它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!