url 超过最大重试次数:'Connection to api.telegram.org timed out. (connect timeout=5.0)'))

Max retries exceeded with url : 'Connection to api.telegram.org timed out. (connect timeout=5.0)'))

最近我创建了一个 Telegram-bot,该 bot 在开始时运行良好。然后在一段时间后(比如几个小时),机器人开始抛出以下我真的不明白的异常:

Error while getting Updates: urllib3 HTTPError SOCKSHTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot1924749319:<mytoken>/getUpdates (Caused by ConnectTimeoutError(<telegram.vendor.ptb_urllib3.urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x116208c90>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)'))
No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/Library/anaconda3/lib/python3.7/site-packages/socks.py", line 809, in connect
    negotiate(self, dest_addr, dest_port)
  File "/Library/anaconda3/lib/python3.7/site-packages/socks.py", line 444, in _negotiate_SOCKS5
    self, CONNECT, dest_addr)
  File "/Library/anaconda3/lib/python3.7/site-packages/socks.py", line 524, in _SOCKS5_request
    resp = self._readall(reader, 3)
  File "/Library/anaconda3/lib/python3.7/site-packages/socks.py", line 276, in _readall
    d = file.read(count - len(data))
  File "/Library/anaconda3/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

我的代码:

def papa_BT_scan(context: telegram.ext.CallbackContext):
    ws_nonFG_bot_web_scraping.papa_bt_scan(context)

#Server Start
#===========================================================
def server_start(update: telegram.Update, context: telegram.ext.CallbackContext):
    print("Telegram_bot_nonFG Started.")
    context.bot.send_message(chat_id=update.message.chat_id,text=':)')
    context.job_queue.run_daily(papa_BT_scan,datetime.time(hour=18, minute=00, tzinfo=pytz.timezone('Asia/Hong_Kong')),context=update.message.chat_id)
    

if __name__ == "__main__":

    REQUEST_KWARGS = {'proxy_url': 'socks5h://127.0.0.1:9150' }
    u = Updater('<myToken>', use_context=True,request_kwargs=REQUEST_KWARGS)
  

    start_handler = CommandHandler('s', server_start,pass_job_queue=True)
    u.dispatcher.add_handler(start_handler)
    
    u.job_queue.start()
    u.start_polling()
    u.idle()

我在网上搜索了一下,大部分人都建议使用Proxy / VPN (ExpressVPN),我已经试过了,但还是没有用。

经过多次来回测试,我可以通过输入如下超时参数来解决上述问题

u.start_polling(timeout=600)