Dingding Operators

Prerequisite Tasks

To use this operators, you must do a few things:
  • Add custom robot to chat group group which you want to send a message.

  • Get DingTalk Custom Robot webhook token.

  • Put the access token in the password field of the dingding_default Connection. Note: You need only token value rather than the whole webhook string.

Basic Usage

Use the DingdingOperator to send message through DingTalk Custom Robot:

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 DingdingOperator can send rich text messages including link, markdown, actionCard and feedCard through DingTalk Custom Robot. 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

DingdingHook could handle task callback by writing a callback function 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"The task {context['ti'].task_id} failed"
    DingdingHook(message_type="text", message=message, at_all=True).send()


Changing connection host if you need

The DingdingOperator 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.

Was this entry helpful?