All Products
Search
Document Center

Container Service for Kubernetes:Use a DingTalk chabot to send event notifications

Last Updated:Dec 18, 2023

You may need to send notifications to an external system after a workflow or a step in a workflow is complete. For example, you may want to send emails or instant messages (such as DingTalk messages) or use a message bus (such as Kafka) to notify the external system. This topic describes how to configure a workflow to send notifications to an external system. In this topic, DingTalk messages are used as an example.

Prerequisites

A Kubernetes cluster for distributed Argo workflows is created. The virtual private cloud (VPC) of the cluster has Internet access.

How it works

  1. An external system usually receives messages by exposing an HTTP webhook.

  2. Argo workflows support exit handlers. You can define exit handlers for the steps in a workflow or define an exit handler for the workflow.

  3. Exit handlers run in containers. You can run the curl command in a container to send event notifications carried in HTTP messages to an external system.

Step 1: Create a DingTalk chatbot

After you create a DingTalk chatbot, a dedicated webhook URL is generated. You can associate the webhook URL with other services, such as Argo workflows, to receive notifications.

  1. Go to the DingTalk group that needs to receive event notifications.

  2. Go to the chatbot settings page.

    1. Click the Group Settings icon 图标.png in the upper-right corner. In the Group Settings panel, click Bot.

    2. In the Robot Management panel, click Add Robot. In the Add Robot section of the Robot dialog box, click the 图标2.png icon.

    3. In the Please choose which robot to add section, click Custom. In the Robot details dialog box, click Add. Configure a DingTalk chatbot as prompted. You need to select at least one security setting. We recommend that you select at least one of the following settings to ensure security: Additional Signature and IP Address.

      Important

      To configure a workflow to send DingTalk messages, make sure that the VPC of the workflow cluster has Internet access. To do this, you can configure an Internet NAT gateway for the workflow cluster and specify the elastic IP address (EIP) of the gateway in the IP Address parameter of the chatbot.

  3. After you complete the configuration, record the webhook URL.image.png

Step 2: Use the DingTalk chatbot in the workflow

Use the following YAML template to create a workflow and use the DingTalk chatbot in the workflow. For more information, see Create a workflow.

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: notification-demo-
spec:
  entrypoint: say-hello
  onExit: exit-handler   # Run the exit handler after the workflow is complete. 
  templates:
  - name: say-hello
    container:
      image: alpine:latest
      command: [sh, -c]
      args: ["echo hello"]

  - name: exit-handler
    container:
      image: curlimages/curl
      command: [sh, -c]
      # Run the curl command to send DingTalk messages and set the message type to link. You can include the link to the Argo workflow console to allow other users to quickly view the details of the workflow. 
      # The following variables can be referenced: {{workflow.name}}, {{workflow.status}}, {{workflow.failures}}, and {{workflow.workflow.duration}}. 
      args: [
        "curl  -H 'Content-Type: application/json' -d '{
          \"msgtype\": \"link\",
          \"link\": {
            \"title\":\"Argo workflow notification\",
            \"text\":\"WF {{workflow.name}} {{workflow.status}}\",
            \"messageUrl\":\"https://argo.xxx.cn-zhangjiakou.alicontainer.com:2746/workflows/default/{{workflow.name}}?tab=workflow\"
          }
        }'
        https://oapi.dingtalk.com/robot/send?access_token=b97fb519129fdfce879baa4e3b905b14e6a64e8994f0ea3b11dda****"  #Replace it with the webhook URL of your DingTalk group. 
      ]

The following figure shows an example of the DingTalk message.

消息.png

References

For more information about how to integrate a custom chatbot with a system, see Integrate custom chatbots.