0

i'm new in python and have a problem. I want to send some photos with telegram bot with using below code.E.g.i want to send 5 image with different URL and i write below code 5 times with different URL.my problem is that when one of the URLs is wrong or not respond my code stop their for example if first URL has problem the other 4 sendPhoto doesn't run and i want my code continue and send other 4 image.anyone have any solution?

def start(bot, update):
 bot.sendPhoto(chat_id='chat_id', photo='URL1',caption="caption")
 bot.sendPhoto(chat_id='chat_id', photo='URL2',caption="caption")
 bot.sendPhoto(chat_id='chat_id', photo='URL3',caption="caption")
 bot.sendPhoto(chat_id='chat_id', photo='URL4',caption="caption")
 bot.sendPhoto(chat_id='chat_id', photo='URL5',caption="caption")
user3480650
  • 19
  • 1
  • 5

1 Answers1

2

one way to do so is using urllib2:

import urllib2
def start(bot, update):
    urls = ['url1','url2']

    for url in urls:
        ret = urllib2.urlopen(url)
        if ret.code == 200:
            bot.sendPhoto(chat_id='chat_id', photo=url,caption="caption")
tashakori
  • 2,331
  • 1
  • 22
  • 29