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)