我制作了一个电报机器人,它使用 pyTelegramBotAPI 包装器根据 URL 的请求发送照片。所以我尝试放置一个虚拟照片 URL 并测试机器人是否可以发送图像,但它失败并出现以下错误。
telebot.apihelper.ApiException: sendPhoto failed. Returned result: <Response [400]>
我不确定错误是什么,但是如何正确使用 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)
我不确定我是否错过了什么。