如果一个URL没有响应,如何在电报机器人sendPhoto()方法中发送多个图像(How to send multiple image in telegram bot sendPhoto() meth

编程入门 行业动态 更新时间:2024-10-26 12:27:56
如果一个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")

更多推荐

本文发布于:2023-04-29 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   电报   机器人   图像   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!