Route events to DingTalk

Updated at:

Use an event rule to filter and route events to a DingTalk chatbot. This topic uses a custom event to explain the prerequisites, procedure, and how to verify the result.

Prerequisites

Background information

A DingTalk chatbot supports the following message types and message formats:
  • Text message: A simple text message, such as I am me, a firework of a different color.
  • Link messagelink
  • Markdown messagemarkdown
  • ActionCard (single-button layout): A message card with an image, title, summary, and a single Read More link. Clicking the card opens a URL.
  • ActionCard (multi-button layout): A message card with a header image, title, and summary, featuring multiple buttons like Good content and Not interested. Each button can open a different URL.
  • FeedCard messagefeedcard

Step 1: Add a custom event source

  1. Log on to the EventBridge console. In the left navigation pane, click Event Buses.
  2. In the top navigation bar, select a region. On the Event Buses page, click the target custom event bus.
  3. In the left navigation pane, click Event Sources, and then click Add Event Source.
  4. In the Add Custom Event Source panel, enter a Name and Description. For Event Provider, select Event Provider, and then click OK.

Step 2: Create an event rule

Important The event rule and its targets must be in the same region.
  1. Log on to the EventBridge console.

  2. In the left-side navigation pane, click Event Buses.

  3. In the top navigation bar, select a 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. On the Create Rule page, complete the following steps:
    1. In the Configure Basic InfoNameDescription step, enter a name and description, and then click Next.
    2. In the Configure Event Pattern step, select Custom Event Sources for Event Source Type. For Event Sources, select the custom event source that you created in Step 1. Enter an event pattern in the Event Pattern Content editor and click Next.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure the following parameters for the event target, and then click Create.
      Note You can add a maximum of five event targets to an event rule.
      • Service Type: Click DingTalk.
      • Webhook URL: Enter the webhook URL for your DingTalk chatbot.
      • Key: Enter the secret key for your DingTalk chatbot.
      • Content to Push: Set to by default. Use custom variables and a template to format the event output.

        The following are example variables and templates for common message types:

        • Text type

          Example variable:

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

          Example template:

          {
              "msgtype":"text",
              "text":{
                  "content":"${content}@1760514XXXX"
              },
              "at":{
                  "atMobiles":[
                      "1760514XXXX",
                      "1898325XXXX"
                  ],
                  "isAtAll":false
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          text Object Yes Specifies the content of the text message.
          content String Yes Specifies the message content.
          at Object Yes Specifies the parameters for the @mention feature.
          atMobiles Array No The mobile numbers of the members to @mention.
          Note The phone numbers must be associated with DingTalk accounts.
          isAtAll Boolean No Specifies whether to @mention all members.
          Note isAtAllIf set to true, all members are @mentioned, and any specific @mentions in the content are ignored.
        • Link type

          Example variable:

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

          Example template:

          {
              "msgtype":"link",
              "link":{
                  "text":"This upcoming new version is called 'Mangrove' by its founder, ${name}. Before this, product managers would always pick a fitting codename for major upgrades. So, why 'Mangrove' this time?",
                  "title":"The train of the era moves forward",
                  "picUrl":"",
                  "messageUrl":"www.example.com"
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          link Object Yes Specifies the content of the link message.
          text String Yes Specifies the message content.
          Note If the content is too long, DingTalk truncates it.
          title String Yes Specifies the message title.
          picUrl String No Specifies the URL of the image.
          messageUrl String Yes Specifies the message URL.
        • Markdown type

          Example variables:

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

          Example template:

          {
              "msgtype":"markdown",
              "markdown":{
                  "title":"Hangzhou Weather",
                  "text":"#### Hangzhou Weather @1760514XXXX \n> ${content}\n> ![screenshot](www.example.com/image_01.png)\n> ###### Published at ${time} [Weather](www.example.com) \n"
              },
              "at":{
                  "atMobiles":[
                      "1760514XXXX"
                  ],
                  "isAtAll":false
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          markdown Object Yes Specifies the content of the Markdown message.
          title String Yes Specifies the message title.
          text String Yes Specifies the message content.
          at Object Yes Specifies the parameters for the @mention feature.
          atMobiles Array No The mobile numbers of the members to @mention.
          isAtAll Boolean No Specifies whether to @mention all members.
          Note isAtAllIf set to true, all members are @mentioned, and any specific @mentions in the content are ignored.
        • ActionCard type (single-button layout)

          Example variable:

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

          Example template:

          {
              "msgtype":"actionCard",
              "actionCard":{
                  "title":"Jobs wanted to build an Apple Cafe 20 years ago, and it was the predecessor of the Apple Store",
                  "text":"![screenshot](www.example.com/image_02.png) \n ### ${content} \n The design of the Apple Store is shifting from its tech-heavy roots to a more lifestyle-oriented approach, a direction that can be traced back to Apple's plan to build a cafe 20 years ago.",
                  "btnOrientation":"0",
                  "singleTitle":"Read the full text",
                  "singleURL":"www.example.com"
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          actionCard Object Yes Specifies the content of the ActionCard message.
          title String Yes Specifies the title displayed in the message preview box.
          text String Yes Specifies the message content.
          btnOrientation String No Specifies the button layout.

          Valid values:

          • 0: Buttons are arranged vertically.
          • 1: Buttons are arranged horizontally.
          singleTitle String Yes Specifies the text for the single button.
          singleURL String Yes The destination URL for the singleTitle button.
        • ActionCard type (multi-button layout)

          Example variable:

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

          Example template:

          {
              "msgtype":"actionCard",
              "actionCard":{
                  "title":"Jobs wanted to build an Apple Cafe 20 years ago, and it was the predecessor of the Apple Store",
                  "text":"![screenshot](www.example.com/image_03.png) \n ### ${content} \n The design of the Apple Store is shifting from its tech-heavy roots to a more lifestyle-oriented approach, a direction that can be traced back to Apple's plan to build a cafe 20 years ago.",
                  "btnOrientation":"0",
                  "btns":[
                      {
                          "title":"Good content",
                          "actionURL":"www.example.com"
                      },
                      {
                          "title":"Not interested",
                          "actionURL":"www.example.edu"
                      }
                  ]
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          actionCard Object Yes Specifies the content of the ActionCard message.
          title String Yes Specifies the title displayed in the message preview box.
          text String Yes Specifies the message content.
          btnOrientation String No Specifies the button layout.

          Valid values:

          • 0: Buttons are arranged vertically.
          • 1: Buttons are arranged horizontally.
          btns Array Yes Specifies the buttons.
          title String Yes Specifies the button title.
          actionURL String Yes The destination URL for the button.
        • FeedCard type

          Example variable:

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

          Example template:

          {
              "msgtype":"feedCard",
              "feedCard":{
                  "links":[
                      {
                          "title":"${content}",
                          "messageURL":"www.example.com",
                          "picURL":"img.example.com/image_04.png"
                      },
                      {
                          "title":"The train of the era moves forward",
                          "messageURL":"www.example.edu",
                          "picURL":"img.example.edu/image_05.png"
                      }
                  ]
              }
          }
          Parameter Type Required Description
          msgtype String Yes Specifies the message type.
          feedCard Object Yes Specifies the content of the FeedCard message.
          links Array No Specifies the links in the FeedCard.
          title String Yes Specifies the message title.
          messageURL String Yes Specifies the message URL.
          picURL String Yes Specifies the URL of the image.

        For more information, see event content transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console.

  2. In the left-side navigation pane, click Event Buses.

  3. In the top navigation bar, select a region.

  4. On the Event Buses page, find the target event bus and click Publish Event in the Operations column.

    Note

    The EventBridge console supports publishing events to custom event buses only.

  5. In the Publish Event To Custom Event Bus panel, configure the following settings:

    SettingDescription
    Custom Event SourceSelect an existing custom event source from the drop-down list.
    Event BodyEnter the event content.
  6. Click OK.

Verify the result

Check your DingTalk client to confirm that the message was received.