Dingding Operators

Prerequisite Tasks

To use this operators, you must do a few things:

  • Add custom robot to Dingding group which you want to send Dingding message.

  • Get the webhook token from Dingding custom robot.

  • Put the Dingding custom robot token in the password field of the dingding_default Connection. Notice that you just need token rather than the whole webhook string.

Basic Usage

Use the DingdingOperator to send Dingding message:

tests/system/providers/dingding/example_dingding.py[source]

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,
)

Remind users in message

Use parameters at_mobiles and at_all to remind specific users when you send message, at_mobiles will be ignored When at_all is set to True:

tests/system/providers/dingding/example_dingding.py[source]

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,
)

Send rich text message

The Dingding operator can send rich text messages including link, markdown, actionCard and feedCard. A rich text message can not remind specific users except by using markdown type message:

tests/system/providers/dingding/example_dingding.py[source]

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"
        "![logo](https://airflow.apache.org/_images/pin_large.png)",
    },
    at_mobiles=["156XXXXXXXX"],
    at_all=False,
)

Sending messages from a Task callback

Dingding operator could handle task callback by writing a function wrapper dingding operators and then pass the function to sla_miss_callback, on_success_callback, on_failure_callback, or on_retry_callback. Here we use on_failure_callback as an example:

tests/system/providers/dingding/example_dingding.py[source]

def failure_callback(context):
    """
    The function that will be executed on failure.

    :param context: The context of the executed task.
    """
    message = (
        f"AIRFLOW TASK FAILURE TIPS:\n"
        f"DAG:    {context['task_instance'].dag_id}\n"
        f"TASKS:  {context['task_instance'].task_id}\n"
        f"Reason: {context['exception']}\n"
    )
    return DingdingOperator(
        task_id="dingding_success_callback",
        message_type="text",
        message=message,
        at_all=True,
    ).execute(context)


Changing connection host if you need

The Dingding operator post http requests using default host https://oapi.dingtalk.com, if you need to change the host used you can set the host field of the connection.

More information

See Dingding documentation on how to custom robot.

Was this entry helpful?