Airflow Telegram Operator因Httpx报错失效,版本适配冲突求助
解决Airflow Telegram Operator报错
socket_options不兼容问题 问题分析
这个错误源于httpx新版本移除了AsyncConnectionPool的socket_options参数,但python-telegram-bot(对应你提到的python-telegram-lib)的部分版本仍在调用该参数,导致版本冲突。降级httpx到0.24.1后又和python-telegram-bot不兼容,核心是要找到两者兼容的版本组合,或者通过其他方式规避参数问题。
可行解决方案
方案1:匹配兼容的版本组合
找python-telegram-bot和httpx的兼容版本对:
- 推荐组合1:
python-telegram-bot==13.7+httpx==0.23.3,双方都不会触发socket_options参数问题 - 推荐组合2:
python-telegram-bot>=20.0+httpx>=0.24.0,新版本的python-telegram-bot已适配httpx的参数变更
执行安装命令:
pip install python-telegram-bot==13.7 httpx==0.23.3 --force-reinstall
方案2:临时修改python-telegram-bot源码(不推荐生产环境)
找到python-telegram-bot中调用AsyncConnectionPool的代码,移除socket_options参数:
- 定位文件:
site-packages/telegram/request/httpxrequest.py - 修改
_get_pool方法,删除包含socket_options=self._socket_options的代码行
方案3:改用自定义Telegram通知Operator
如果版本冲突难以调和,可以直接用requests库封装自定义Operator替代官方Operator,示例代码:
import requests from airflow.models import BaseOperator class CustomTelegramAlertOperator(BaseOperator): def __init__(self, bot_token, chat_id, alert_msg, **kwargs): super().__init__(**kwargs) self.bot_token = bot_token self.chat_id = chat_id self.alert_msg = alert_msg def execute(self, context): api_url = f"https://api.telegram.org/bot{self.bot_token}/sendMessage" payload = {"chat_id": self.chat_id, "text": self.alert_msg} response = requests.post(api_url, json=payload) response.raise_for_status()
在Airflow DAG中直接使用这个自定义Operator即可完成通知推送。
内容的提问来源于stack exchange,提问作者Hoàng Ngô
相关产品推荐
相关产品推荐

