我做了一个电报机器人,根据请求使用pyTelegramBotAPI包装发送照片。所以我试图放置一个虚拟的照片URL并测试机器人是否可以发送图像,但是由于以下错误而失败。从Telegram Bot发送URL的照片
telebot.apihelper.ApiException: sendPhoto failed. Returned result: <Response [400]>
我不知道是什么错误,但我怎么能发送正确使用电报博特API从URL中的照片?这里是我的代码
import telebot
import time
import urllib
from io import BytesIO
from PIL import Image
TOKEN = '<token here>'
url='http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
def listener(*messages):
for m in messages:
chatid = m.chat.id
if m.content_type == 'text':
text = m.text
name = m.fromUser.first_name
msgid = m.message_id
if(text.startswith('/photo')):
img = BytesIO(urllib.request.urlopen(url).read())
tb.send_chat_action(chatid, 'upload_photo')
tb.send_photo(chatid, img, reply_to_message_id=msgid)
tb = telebot.TeleBot(TOKEN)
tb.get_update() # cache exist message
tb.set_update_listener(listener) #register listener
tb.polling()
while True:
time.sleep(1)
我不知道我是否错过了某些东西。
所以我们需要下载整个文件到本地文件夹上传到电报毕竟之前。有什么办法可以在下载的时候缩小图像大小,因为我在机器上完全运行脚本? – Zerocchi
我不知道你到底想做什么,但我确定有办法用python压缩图像,只有不会_while_下载。并且确保在不再需要该文件时从计算机中删除该图像。 – Pete
我的代码在这里 - 我相信,因为我是python新手 - 不需要将图像下载到本地文件夹中:http://stackoverflow.com/a/32441772/1097372 – Iyas