You can use event rules to filter events and route events to DingTalk. This topic describes the prerequisites and procedure for routing custom events to DingTalk. This topic also describes how to verify the result.

Prerequisites

Background information

The DingTalk chatbot can push the following types of messages:
  • Message of the text typeMessage of the text type
  • Message of the link typelink
  • Message of the Markdown typemarkdown
  • Message of the single redirect actionCard typeMessage of the single redirect actionCard type
  • Message of the multi-redirect actionCard typeMessage of the multi-redirect actionCard type
  • Message of the feedCard typefeedcard

Step 1: Create a custom event source

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Sources.
  3. In the top navigation bar, select a region.
  4. On the Event Sources page, click the Custom Event Sources tab. On the Custom Event Sources tab, click Add Event Source.
  5. In the Add Custom Event Source panel, enter a name and a description, select an event provider, click Create Custom Event Bus, enter the name of a new custom event bus, and then click OK.

Step 2: Create an event rule

Notice The event targets that you want to configure for an event rule must reside in the same region as the event rule.
  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Rules.
  3. In the top navigation bar, select a region.
  4. On the Event Rules page, select the custom event bus and click Create Rule.
  5. On the Create Rule page, perform the following steps:
    1. In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and click Next Step.
    2. In the Configure Event Pattern step, set the Event Source Type parameter to Custom Event Source, select the custom event source that is created in step1 from the Event Source drop-down list, specify an event pattern in the Event Pattern Content code editor, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure an event target. Then, click Create.
      Note You can configure a maximum of five event targets for an event rule.
      • Service Type: Click DingTalk.
      • Address: Enter the webhook URL of the DingTalk chatbot.
      • Secret Key: Enter the key of the DingTalk chatbot.
      • Pushed Content: Specify a template. Events are routed in the format that is defined in a custom template to event targets. You can customize variables in the template.

        A DingTalk chatbot can push only messages of the specified type and in the specified format. Typically, a DingTalk chatbot supports the following types of variables and templates:

        • Sample variable and template of the text type

          Sample variable:

          {
            "content": "$.data.content"
          }

          Sample template:

          {
              "msgtype":"text",
              "text":{
                  "content":"${content}@1760514XXXX"
              },
              "at":{
                  "atMobiles":[
                      "1760514XXXX",
                      "1898325XXXX"
                  ],
                  "isAtAll":false
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          text Object Yes The content of the message of the text type.
          content String Yes The content of the message.
          at Object Yes The configuration of the reminder by using the at sign (@).
          atMobiles Array No The mobile number of the member to be reminded by the at sign (@).
          Note Mobile numbers must be bound to DingTalk accounts.
          isAtAll Boolean No Specifies whether to remind all members by using the at sign (@).
          Note If you set the isAtAll parameter to true, DingTalk reminds all members by using the at sign (@). In this case, the at sign (@) that you use in the content parameter to remind a specific member does not take effect.
        • Sample variable and template of the link type

          Sample variable:

          {
            "name": "$.data.name"
          }

          Sample template:

          {
              "msgtype":"link",
              "link":{
                  "text":"The founder ${name} calls the upcoming new version a mangrove. Product managers adopt an appropriate alias for each major upgrade. Why is this version called the mangrove?",
                  "title":"The train of the era is moving forward",
                  "picUrl":"",
                  "messageUrl":"www.example.com"
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          link Object Yes The content of the message of the link type.
          text String Yes The content of the message.
          Note If the content of the message is too long, only part of the content is displayed in the DingTalk window.
          title String Yes The title of the message.
          picUrl String No The URL of the image.
          messageUrl String Yes The URL of the message.
        • Sample variables and template of the Markdown type

          Sample variables:

          {
              "time":"$.time",
              "content":"$.data.content"
          }

          Sample template:

          {
              "msgtype":"markdown",
              "markdown":{
                  "title":"Weather in Jiaxing",
                  "text":"#### Weather in Jiaxing @1760514XXXX \n> ${content}\n> ![screenshot](www.example.com/image_01.png)\n> ###### ${time} Forecast [Weather](www.example.com) \n"
              },
              "at":{
                  "atMobiles":[
                      "1760514XXXX"
                  ],
                  "isAtAll":false
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          markdown Object Yes The content of the message of the Markdown type.
          title String Yes The title of the message.
          text String Yes The content of the message.
          at Object Yes The configuration of the reminder by using the at sign (@).
          atMobiles Array No The mobile number of the member to be reminded by the at sign (@).
          isAtAll Boolean No Specifies whether to remind all members by using the at sign (@).
          Note If you set the isAtAll parameter to true, DingTalk reminds all members by using the at sign (@). In this case, the at sign (@) that you use in the content parameter to remind a specific member does not take effect.
        • Sample variable and template of the single redirect actionCard type

          Sample variables:

          {
              "content":"$.data.content"
          }

          Sample template:

          {
              "msgtype":"actionCard",
              "actionCard":{
                  "title":"Jobs wanted to build an Apple Cafe 20 years ago, and it was the predecessor of Apple Store",
                  "text":"![screenshot](www.example.com/image_02.png) \n ### ${content} \n The design of Apple Store is moving from the original full sense of technology to life, and its life-oriented trend can be traced back to the plan of Apple to build a cafe 20 years ago.",
                  "btnOrientation":"0",
                  "singleTitle":"To read the full article",
                  "singleURL":"www.example.com"
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          actionCard Object Yes The content of the message of the actionCard type.
          title String Yes The title of the message in the preview box.
          text String Yes The content of the message.
          btnOrientation String No The direction of the button.

          Valid values:

          • 0: vertical arrangement
          • 1: horizontal arrangement
          singleTitle String Yes The title of a single button.
          singleURL String Yes The URL that is triggered when you click the title that the singleTitle parameter specifies.
        • Sample variable and template of the multi-redirect actionCard type

          Sample variable:

          {
              "content":"$.data.content"
          }

          Sample template:

          {
              "msgtype":"actionCard",
              "actionCard":{
                  "title":"Jobs wanted to build an Apple Cafe 20 years ago, and it was the predecessor of Apple Store",
                  "text":"![screenshot](www.example.com/image_03.png) \n ### ${content} \n The design of Apple Store is moving from the original full sense of technology to life, and its life-oriented trend can be traced back to the plan of Apple to build a cafe 20 years ago.",
                  "btnOrientation":"1",
                  "btns":[
                      {
                          "title":"The content is good",
                          "actionURL":"www.example.com"
                      },
                      {
                          "title":"Not interested",
                          "actionURL":"www.example.edu"
                      }
                  ]
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          actionCard Object Yes The content of the message of the actionCard type.
          title String Yes The title of the message in the preview box.
          text String Yes The content of the message.
          btnOrientation String No The direction of the button.

          Valid values:

          • 0: vertical arrangement
          • 1: horizontal arrangement
          btns Array Yes The buttons.
          title String Yes The title of the button.
          actionURL String Yes The URL that is triggered when you click the button.
        • Sample variable and template of the feedCard type

          Sample variable:

          {
              "content":"$.data.content"
          }

          Sample template:

          {
              "msgtype":"feedCard",
              "feedCard":{
                  "links":[
                      {
                          "title":"${content}",
                          "messageURL":"www.example.com",
                          "picURL":"www.example.com/image_04.png"
                      },
                      {
                          "title":"What can reading bring us?",
                          "messageURL":"www.example.com",
                          "picURL":"www.example.com/image_05.png"
                      },        
                      {
                          "title":"Either read or work out.",
                          "messageURL":"www.example.com",
                          "picURL":"www.example.com/image_06.png"
                      }
                  ]
              }
          }
          Parameter Type Required Description
          msgtype String Yes The type of the message.
          feedCard Object Yes The content of the message of the feedCard type.
          links Array No The URLs related to the message of the feedCard type.
          title String Yes The title of the message.
          messageURL String Yes The URL of the message.
          picURL String Yes The URL of the image.

        For more information, see Event transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Buses.
  3. In the top navigation bar, select a region.
  4. In the Custom Event Buses section, find the event bus to which you want to publish an event and click Publish Event in the Operations column.
    Note You can publish events only to custom event buses in the EventBridge console.
  5. In the Publish Event to Custom Event Bus panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK.
    For more information about the event parameters, see Overview.

Verify the result

View the message that is pushed by the DingTalk chatbot on DingTalk.