如果一个URL没有响应,如何在电报机器人sendPhoto()方法中发送多个图像(How to send multiple image in telegram bot sendPhoto() method if one URL not respond)
我是python的新手,有问题。 我想用电报机器人发送一些照片,使用下面的代码.Egi想要发送5个不同URL的图像,我用不同的URL写下代码5次。我的问题是当其中一个URL错误或没有回复我的代码时停止他们的例如,如果第一个URL有问题,其他4 sendPhoto不运行,我希望我的代码继续并发送其他4图像。任何人有任何解决方案?
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")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")最满意答案
一种方法是使用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")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")更多推荐
发布评论