I made a telegram bot which sends photo upon request from URL using pyTelegramBotAPI wrapper. So I tried putting a dummy photo URL and test if the bot can send the image but it failed with the following error.
telebot.apihelper.ApiException: sendPhoto failed. Returned result: <Response [400]>
I'm not sure what the error is, but how can I send a photo from URL using Telegram Bot API correctly? Here is my code
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)
I'm not sure whether I missed something though.
Try this:
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)
or (using pyTelegramBotAPI 0.2.0)
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With