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. In the left-side navigation pane, click Event Buses.
  2. In the top navigation bar, select a region. On the Event Buses page, click the name of the custom event bus that you want to manage.
  3. In the left-side navigation pane, click Event Sources, and then click Add Event Source.
  4. In the Add Custom Event Source panel, set the Name and Description parameters, select Custom Application from the Event Provider drop-down list, and then click OK.

Step 2: Create an event rule

Important 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, click Event Buses.
  3. In the top navigation bar, select the region.
  4. On the Event Buses page, click the name of the event bus that you want to manage.
  5. In the left-side navigation pane, click Event Rules.
  6. On the Event Rules page, click Create Rule.
  7. Complete the Create Rule wizard.
    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 Step 1 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 messages only 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
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          textObjectYesThe content of the message of the text type.
          contentStringYesThe content of the message.
          atObjectYesThe configuration of the reminder by using the at sign (@).
          atMobilesArrayNoThe mobile number of the member to be reminded by the at sign (@).
          Note Mobile numbers must be bound to DingTalk accounts.
          isAtAllBooleanNoSpecifies 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"
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          linkObjectYesThe content of the message of the link type.
          textStringYesThe 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.
          titleStringYesThe title of the message.
          picUrlStringNoThe URL of the image.
          messageUrlStringYesThe 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 Hangzhou",
                  "text":"#### Weather in Hangzhou @1760514XXXX \n> ${content}\n> ![screenshot](www.example.com/image_01.png)\n> ###### ${time}Publish [Weather](www.example.com) \n"
              },
              "at":{
                  "atMobiles":[
                      "1760514XXXX"
                  ],
                  "isAtAll":false
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          markdownObjectYesThe content of the message of the Markdown type.
          titleStringYesThe title of the message.
          textStringYesThe content of the message.
          atObjectYesThe configuration of the reminder by using the at sign (@).
          atMobilesArrayNoThe mobile number of the member to be reminded by the at sign (@).
          isAtAllBooleanNoSpecifies 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 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_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":"Read the full text",
                  "singleURL":"www.example.com"
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          actionCardObjectYesThe content of the message of the actionCard type.
          titleStringYesThe title of the message in the preview box.
          textStringYesThe content of the message.
          btnOrientationStringNoThe direction of the button.

          Valid values:

          • 0: vertical arrangement
          • 1: horizontal arrangement
          singleTitleStringYesThe title of a single button.
          singleURLStringYesThe 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":"0",
                  "btns":[
                      {
                          "title":"The content is good",
                          "actionURL":"www.example.com"
                      },
                      {
                          "title":"Not interested",
                          "actionURL":"www.example.edu"
                      }
                  ]
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          actionCardObjectYesThe content of the message of the actionCard type.
          titleStringYesThe title of the message in the preview box.
          textStringYesThe content of the message.
          btnOrientationStringNoThe direction of the button.

          Valid values:

          • 0: vertical arrangement
          • 1: horizontal arrangement
          btnsArrayYesThe buttons.
          titleStringYesThe title of the button.
          actionURLStringYesThe 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":"img.example.com/image_04.png"
                      },
                      {
                          "title":"The train of the era is moving forward",
                          "messageURL":"www.example.edu",
                          "picURL":"img.example.edu/image_05.png"
                      }
                  ]
              }
          }
          ParameterTypeRequiredDescription
          msgtypeStringYesThe type of the message.
          feedCardObjectYesThe content of the message of the feedCard type.
          linksArrayNoThe URLs related to the message of the feedCard type.
          titleStringYesThe title of the message.
          messageURLStringYesThe URL of the message.
          picURLStringYesThe URL of the image.

        For more information, see Event transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
  2. In the top navigation bar, select a region.
  3. On the Event Buses page, 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.
  4. 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.