首页 > 解决方案 > Python-Telegram-Bot - 警告 - 更新“无”导致错误“组发送失败”

问题描述

最近我开发了一个电报机器人,它能够从 CME 集团网站检索美联储利率条形图。( https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html ) 机器人每天在香港时间下午 5:25 运行(通过 run_daily),并将 10 张图片(媒体组限制)推送到我的机器人。但是,我发现机器人出现在警告之下并且在机器人提前几个小时开始时无法发送(等等。我在上午 10:00 启动了机器人,而 print_CME_targetFedRate 在下午 5:25 运行),但是如果我使用run_once 立即运行,我的代码工作正常,我可以从机器人中检索这些图像。

警告 - 更新“无”导致错误“组发送失败”

单击此处查看终端中的警告

有人知道出了什么问题或有任何想法吗?

下面是我的代码:

def print_CME_targetFedRate(context: telegram.ext.CallbackContext):
        media   = []
        browser = ws_functions.get_ChromeDriver(("--headless"))
        browser.implicitly_wait(5)
        browser.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'})

        cme_target_FredRate_url = "https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html#"
        browser.get(cme_target_FredRate_url)
        time.sleep(5)
        try:
            browser.find_element_by_id('pardotCookieButton').click()
            browser.implicitly_wait(10)
        except:
            browser.implicitly_wait(10)

        iframe = browser.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[3]/div/div/div[1]/div/iframe')
        browser.switch_to.frame(iframe)

        #Save images
        src = browser.find_element_by_id('MainContent_ucChart_ctl00').get_attribute("src")
        meet_date = browser.find_element_by_xpath('/html/body/form/div[3]/div[2]/div[3]/div[1]/div/div/div[1]/div/div[3]/div[3]/div/div/div/div[1]/table/tbody/tr/td[1]/table/tbody/tr[3]/td[1]').text
        media.append(InputMediaPhoto(src,caption=meet_date))

        #Capture all periods elements into eleList
        for x in range(2,11):
            browser.find_element_by_xpath('/html/body/form/div[3]/div[2]/div[3]/div[1]/div/div/div[1]/div/div[3]/div[3]/div/div/ul/li[%s]/a'%(x)).click()
            time.sleep(4)
            src = browser.find_element_by_id('MainContent_ucChart_ctl00').get_attribute("src")
            meet_date = browser.find_element_by_xpath('/html/body/form/div[3]/div[2]/div[3]/div[1]/div/div/div[1]/div/div[3]/div[3]/div/div/div/div[1]/table/tbody/tr/td[1]/table/tbody/tr[3]/td[1]').text
            time.sleep(0.3)
            media.append(InputMediaPhoto(src,caption=meet_date))

        time.sleep(1)
        context.bot.send_message(chat_id=context.job.context,text='CME | Target Rate Probabilities for Fed Rate')
        context.bot.sendMediaGroup(chat_id=context.job.context,media=media) #Seems goes wrong here...
        time.sleep(15)
        browser.quit()

#Server Start
#===========================================================
def server_start(update: telegram.Update, context: telegram.ext.CallbackContext):
    print("Telegram_bot_misc Started.")
    context.bot.send_message(chat_id=update.message.chat_id,text=':)')
    context.job_queue.run_daily(print_CME_targetFedRate,datetime.time(hour=17, minute=25, tzinfo=pytz.timezone('Asia/Hong_Kong')),context=update.message.chat_id)
    #context.job_queue.run_once(print_stockIND, 1, context=update.message.chat_id)
    

if __name__ == "__main__":
    u          = Updater('<myToken>', use_context=True,request_kwargs={'read_timeout':6,'connect_timeout':7})
    j          = u.job_queue
    dispatcher = u.dispatcher

    j.set_dispatcher(dispatcher)

    timer_handler = CommandHandler('s', server_start)
    
    u.dispatcher.add_handler(timer_handler)
    u.dispatcher.add_error_handler(error)
    u.start_polling(timeout=600)
    j.start()
    u.idle()

在 20210825 上更新:错误消息(删除错误处理程序后)

标签: pythontelegram-botpython-telegram-bot

解决方案


https://stackoverflow.com/a/68883569/10606962类似,我建议停用/替换您的错误处理程序,以便您可以看到完整的回溯。

关于失败的媒体组发送:我怀疑当你尝试发送媒体组时它有什么关系,因为电报错误消息并不总是很有帮助,你可能不知道它为什么失败。

IISC 您发送的媒体是在运行时确定的。我想尝试发送一组固定的图像用于测试目的是值得的。如果send_media_group直接调用发送固定的图像集有效,但在工作中不起作用,那么肯定有问题……</p>


关于回溯的编辑:

对于未来:请将回溯发布为代码块而不是图像 - 这样阅读起来更舒服;)

虽然回溯提到了print_CME_targetFedRate,但它说print_CME_targetFedRate调用了一个调用的函数ws_bot_web_scraping,其中调用了context.bot.sendMediaGroup,这会导致异常。ws_bot_web_scraping上面的代码中缺少对的调用。

无论如何,我上面的第二和第三段仍然存在:)


免责声明:我目前是python-telegram-bot


推荐阅读