Dingding Operators¶
先決條件任務¶
- 要使用這些 operators,您必須做幾件事
在您想要傳送訊息的聊天群中新增自定義機器人。
將訪問令牌放入
dingding_defaultConnection 的 password 欄位中。注意:您只需要令牌值而不是完整的 webhook 字串。
基本用法¶
使用 DingdingOperator 透過 釘釘自定義機器人 傳送訊息
tests/system/dingding/example_dingding.py
text_msg_remind_none = DingdingOperator(
task_id="text_msg_remind_none",
message_type="text",
message="Airflow dingding text message remind none",
at_mobiles=None,
at_all=False,
)
在訊息中提醒使用者¶
使用引數 at_mobiles 和 at_all 在傳送訊息時提醒特定使用者,當 at_all 設定為 True 時,at_mobiles 將被忽略。
tests/system/dingding/example_dingding.py
text_msg_remind_all = DingdingOperator(
task_id="text_msg_remind_all",
message_type="text",
message="Airflow dingding text message remind all users in group",
# list of user phone/email here in the group
# when at_all is specific will cover at_mobiles
at_mobiles=["156XXXXXXXX", "130XXXXXXXX"],
at_all=True,
)
傳送富文字訊息¶
DingdingOperator 可以透過 釘釘自定義機器人 傳送富文字訊息,包括連結、Markdown、actionCard 和 feedCard。富文字訊息無法提醒特定使用者,除非使用 Markdown 型別的訊息。
tests/system/dingding/example_dingding.py
markdown_msg = DingdingOperator(
task_id="markdown_msg",
message_type="markdown",
message={
"title": "Airflow dingding markdown message",
"text": "# Markdown message title\n"
"content content .. \n"
"### sub-title\n"
"",
},
at_mobiles=["156XXXXXXXX"],
at_all=False,
)
從任務回撥函式傳送訊息¶
DingdingHook 可以透過編寫回調函式並將其傳遞給 sla_miss_callback、on_success_callback、on_failure_callback 或 on_retry_callback 來處理任務回撥。這裡我們以 on_failure_callback 為例。
tests/system/dingding/example_dingding.py
def failure_callback(context):
"""
The function that will be executed on failure.
:param context: The context of the executed task.
"""
message = f"The task {context['ti'].task_id} failed"
DingdingHook(message_type="text", message=message, at_all=True).send()
如果需要,更改連線主機¶
DingdingOperator 使用預設主機 https://oapi.dingtalk.com 傳送 HTTP 請求,如果需要更改使用的主機,可以在連線的 host 欄位中進行設定。