skip to Main Content

Recently I’ve developed a telegram-bot which able to retrieve Fed-Rate Bar graph from CME group website. (https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html)
The bot runs daily (via run_daily) at every 5:25 PM HKT and pushs 10 images (media group limitation) to my bot.
However, I’ve found that the bot occurs below warning and failed to send when the bot has started few hours ahead
(etc. I started the bot at 10:00am and the print_CME_targetFedRate runs at 5:25 pm ), but if I use run_once to run immediately, my code works fine and I can retrieve those images from the bot.

WARNING – Update "None" caused error "Group send failed"

Click here to view the warning in Terminal

Would anyone knows what is wrong or have any idea?

Below is my code:

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()

Updated on 20210825 : error message (After remove Error Handler)

2

Answers


  1. Chosen as BEST ANSWER

    I have tried to add time.sleep(30) statement in between the sendMediaGroup, then seems my problem is gone and now can send successfully.


  2. Similar to https://stackoverflow.com/a/68883569/10606962 I recommend to deactivate/replace your error handler so that you can see the full traceback.

    Regarding the failed media group sending: I doubt that it has anything to do when you try to send the media group and because Telegrams error messages are not always very helpful, you probably won’t know why it failed.

    IISC the media that you sent is determined at runtime. I guess it’s worth to try to send a fixed set of images for testing purposes. If sending the fixed set of images works if you call send_media_group directly, but doesn’t work in the job, then something’s definetily wrong …


    Edit regarding the traceback:

    For the future: Please just post tracebacks as codeblock instead of as image – that’s way more comfortable to read 😉

    While the traceback mentions the print_CME_targetFedRate, it says that print_CME_targetFedRate calls a function called ws_bot_web_scraping in which there is a call to context.bot.sendMediaGroup, which causes the exception. The call to ws_bot_web_scraping is missing in your code above.

    Anyway, the my 2nd & 3rd paragraph above still stand 🙂


    Disclaimer: I’m currently the maintainer of python-telegram-bot

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search