Python 使用电报机器人从URL发送照片

Python 使用电报机器人从URL发送照片,python,telegram-bot,Python,Telegram Bot,我制作了一个电报机器人,它使用pyTelegramBotAPI包装器根据URL的请求发送照片。因此,我尝试放置一个虚拟照片URL并测试bot是否可以发送图像,但失败了,出现以下错误 telebot.apihelper.ApiException:发送照片失败。返回结果: 我不确定错误是什么,但如何正确使用Telegram Bot API从URL发送照片?这是我的密码 import telebot import time import urllib from io import BytesIO fr

我制作了一个电报机器人,它使用pyTelegramBotAPI包装器根据URL的请求发送照片。因此,我尝试放置一个虚拟照片URL并测试bot是否可以发送图像,但失败了,出现以下错误

telebot.apihelper.ApiException:发送照片失败。返回结果:

我不确定错误是什么,但如何正确使用Telegram Bot 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)
导入远程机器人
导入时间
导入URL库
从io导入字节io
从PIL导入图像
令牌=“”
url='1〕http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
def侦听器(*消息):
对于m in消息:
chatid=m.chat.id
如果m.content_type==“text”:
text=m.text
name=m.fromUser.first\u name
msgid=m.message\u id
如果(text.startswith('/photo')):
img=BytesIO(urllib.request.urlopen(url.read())
tb.send_chat_action(chatid,‘上传照片’)
tb.发送照片(聊天id、img、回复邮件id=msgid)
tb=远程机器人。远程机器人(令牌)
tb.get_update()#缓存存在消息
tb.set_update_listener(listener)#register listener
tb.polling()
尽管如此:
时间。睡眠(1)
但我不确定我是否错过了什么。

试试这个:

import telebot
import time
import urllib

url = 'http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
f = open('out.jpg','wb')
f.write(urllib.request.urlopen(url).read())
f.close()

def listener(*messages):
    for m in messages:
        chat_id = m.chat.id
        if m.content_type == 'text':
            text = m.text
            msgid = m.message_id
            if text.startswith('/photo'):
                tb.send_chat_action(chat_id, 'upload_photo')
                img = open('out.jpg', 'rb')
                tb.send_photo(chat_id, img, reply_to_message_id=msgid)
                img.close()


tb = telebot.TeleBot(TOKEN)
tb.set_update_listener(listener) #register listener
tb.polling()

while True:
    time.sleep(0)
或(使用API 0.2.0)


所以,在上传到电报之前,我们需要将整个文件下载到本地文件夹中。考虑到我完全在我的机器上运行脚本,有没有办法在下载时减小图像大小?我不知道你到底想做什么,但我确信有办法用python压缩图像,只是在下载时没有。当您不再需要该文件时,请确保从您的计算机中删除该图像。我这里的代码—我相信我是python新手—不需要将图像下载到本地文件夹:
import telebot
import time
import urllib

url='http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
f = open('out.jpg','wb')
f.write(urllib.request.urlopen(url).read())
f.close()

tb = telebot.TeleBot(TOKEN)

@tb.message_handler(commands=['photo'])
def send_photo(message):
    tb.send_chat_action(message.chat.id, 'upload_photo')
    img = open('out.jpg', 'rb')
    tb.send_photo(message.chat.id, img, reply_to_message_id=message.message_id)
    img.close()

tb.polling()

while True:
    time.sleep(0)
elif 'Hi' in text:
    reply(img=urllib2.urlopen('img url').read())