【问题标题】:How to send multiple image in telegram bot sendPhoto() method if one URL not respond如果一个 URL 没有响应,如何在电报机器人 sendPhoto() 方法中发送多个图像
【发布时间】:2017-07-19 08:04:41
【问题描述】:

我是 python 新手,遇到了问题。我想使用以下代码发送一些带有电报机器人的照片。例如,我想发送 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")

【问题讨论】:

  • 提供更多代码。你试过捕捉错误吗?
  • 是的,我使用 try: 除了每个 bot.sendPhoto 但我又遇到了同样的问题

标签: python telegram telegram-bot python-telegram-bot


【解决方案1】:

一种方法是使用 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2020-09-26
    • 2018-02-25
    相关资源
    最近更新 更多