diff --git a/channel/qq/qq_channel.py b/channel/qq/qq_channel.py index 0e3094f1..a85072b1 100644 --- a/channel/qq/qq_channel.py +++ b/channel/qq/qq_channel.py @@ -23,6 +23,7 @@ from channel.qq.qq_message import QQMessage from common.expired_dict import ExpiredDict from common.log import logger from common.singleton import singleton +from common.ws_client_compat import websocket_app_run_forever from config import conf # Rich media file_type constants @@ -210,7 +211,7 @@ class QQChannel(ChatChannel): def run_forever(): try: - self._ws.run_forever(ping_interval=0, reconnect=0) + websocket_app_run_forever(self._ws, ping_interval=0, reconnect=0) except (SystemExit, KeyboardInterrupt): logger.info("[QQ] WebSocket thread interrupted") except Exception as e: diff --git a/channel/wecom_bot/wecom_bot_channel.py b/channel/wecom_bot/wecom_bot_channel.py index 550d5614..01ae6503 100644 --- a/channel/wecom_bot/wecom_bot_channel.py +++ b/channel/wecom_bot/wecom_bot_channel.py @@ -26,6 +26,7 @@ from channel.wecom_bot.wecom_bot_message import WecomBotMessage from common.expired_dict import ExpiredDict from common.log import logger from common.singleton import singleton +from common.ws_client_compat import websocket_app_run_forever from config import conf WECOM_WS_URL = "wss://openws.work.weixin.qq.com" @@ -119,7 +120,7 @@ class WecomBotChannel(ChatChannel): def run_forever(): try: - self._ws.run_forever(ping_interval=0, reconnect=0) + websocket_app_run_forever(self._ws, ping_interval=0, reconnect=0) except (SystemExit, KeyboardInterrupt): logger.info("[WecomBot] WebSocket thread interrupted") except Exception as e: diff --git a/common/ws_client_compat.py b/common/ws_client_compat.py new file mode 100644 index 00000000..e5783163 --- /dev/null +++ b/common/ws_client_compat.py @@ -0,0 +1,17 @@ +import inspect +from typing import Any + + +def websocket_app_run_forever(ws: Any, **kwargs: Any) -> None: + """ + Call WebSocketApp.run_forever; strip reconnect= if the installed + websocket-client is too old (reconnect was added in a later 1.x release). + """ + if "reconnect" in kwargs: + try: + params = inspect.signature(ws.run_forever).parameters + except (TypeError, ValueError): + params = {} + if "reconnect" not in params: + kwargs = {k: v for k, v in kwargs.items() if k != "reconnect"} + ws.run_forever(**kwargs) diff --git a/requirements.txt b/requirements.txt index 32e2da3b..2d965ef2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,5 +23,5 @@ lark-oapi # dingtalk dingtalk_stream # wecom bot websocket mode -websocket-client +websocket-client>=1.4.0 pycryptodome