python-2.7 - Telegram-bot (telepot api) : Is it possible to send an image directly from URL without saving it

标签 python-2.7 telegram-bot python-telegram-bot

我正在使用 python 编写电报机器人 telepot api .我现在被困在我想发送直接来自 URL 而不将其存储在本地的图片的点。 Telepot提供以下发送照片的说明:

>>> f = open('zzzzzzzz.jpg', 'rb')  # some file on local disk
>>> response = bot.sendPhoto(chat_id, f)

现在我正在使用
f = urllib2.urlopen('http://i.imgur.com/B1fzGoh.jpg')
bot.sendPhoto(chat_id, f)

这里的问题是 urllib2.urlopen('url')为我提供类似文件的对象,例如:
<addinfourl at 140379102313792 whose fp = <socket._fileobject object at 0x7fac8e86d750>>
并且不喜欢 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]   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')

有一个 different telegram-bot project provided here 的解决方案他们在哪里发送 urllib2.urlopen('url').read()回到电报,但在我的情况下,这会产生与没有 .read() 相同的错误。

我怎样才能从 url 获取文件作为文件对象(最好不要在本地保存它)?
或者如何从 urlopen() 提供的“类文件对象”中获取“文件对象”?

谢谢你的帮助 :)

最佳答案

在当前 Bot Api 2.3.1 , 您可以简单地将文件的 url 发送到服务器:

bot.sendPhoto(chat_id, "http://i.imgur.com/B1fzGoh.jpg");

就是这样。

你甚至不需要下载它,Telegram会自己上传。

关于python-2.7 - Telegram-bot (telepot api) : Is it possible to send an image directly from URL without saving it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35314526/

相关文章:

python - 对列表进行排序以形成最大可能的数字

python - 为什么 telegram.Bot.getUpdates() 长轮询会立即返回?

telegram-bot - 防止使用网络 Hook 从 Telegram Bot API 获取旧更新

python - (python) Telegram bot-如何定期发送消息?

python - 如何让我的 python 电报机器人每天在特定时间发送消息?

python - 从 CSV 文件绘制 Pandas 框架

python - 为什么 subprocess.call 仅当参数在数组中分隔时才适用于任何字符串?

php - 在 php curl 中发送以 "@"开头的字符串作为字符串

python - 如何在没有额外模型的情况下在 django 中存储数据对?