Skip to main content

Use Cases

  • When your business requires high real-time performance for data. For example, when a new employee joins or leaves, the app needs to update user data immediately. In this case, subscribe to Contacts events.
  • When your app needs to respond promptly to user actions. For example, when a user joins a group chat, the app can subscribe to group chat events to send a welcome message when the user enters the group chat.
These are just a few simple use cases. Developers can handle different events in different ways.

Event Subscription Process

The event subscription process is shown in the figure below. First, configure an HTTP request receiving URL on the DingTalk Open Platform to receive pushed subscribed events, and then set the events to subscribe to. After you configure the request URL, the DingTalk Open Platform sends a POST request to it. The event subscription is completed only when an encrypted string containing “success” is correctly returned within the specified time.

Configure the Request URL and Event Subscription

  1. Sign in to the Developer Console and locate your created internal app.
  2. Click Event Subscription, and then click Edit to configure the HTTP URL used to receive requests. Note Make sure this URL is accessible over the public network. After you edit the request URL and click the Save button, the Open Platform pushes an application/json POST request to the URL you configured to verify its validity. The request is as follows:
    When you receive the POST verification request from the Open Platform, decrypt it and return an encrypted string containing success (in JSON format) within 1,500 ms. After the DingTalk Open Platform receives the returned JSON information, it decrypts it. If it can obtain the correct success string, the callback push is verified as normal. Otherwise, it is determined to be a failed callback.
  3. After you successfully configure the request URL, turn on the events to subscribe to in the Event Subscription list area.

Receive and Respond to Events

  • Receive Event Information When an event occurs, DingTalk actively sends a POST request to the configured HTTP URL to push the corresponding event information. For example, after you subscribe to Contacts events, when the Contacts change, event information is pushed to the registered HTTP URL in the following format. Note DingTalk servers push information in real time. If the organization’s callback URL does not return the correct encrypted information to the DingTalk server within 1,500 milliseconds, the DingTalk server determines the push to have failed. The request URL format is as follows:
    The included JSON data is as follows:
    Where:
    • signature is the signature of the message body.
    • timestamp is the timestamp.
    • nonce is a random string.
    • encrypt is the encrypted pushed event information.
  • Respond to Event Information When you receive the POST verification request from the Open Platform, decrypt it and return an encrypted string containing success (in JSON format) within 1,500 ms. After the DingTalk Open Platform receives the returned JSON information, it decrypts it. If it can obtain the correct success string, the callback push is verified as normal. Otherwise, it is determined to be a failed callback. The specific data format returned to DingTalk is as follows: Note The returned data format must be JSON.
    Where:
    • msg_signature is the signature of the message body.
    • timeStamp is the timestamp.
    • nonce is a random string.
    • encrypt is the encrypted success string.

Message Encryption and Decryption

To ensure the security of data transmission, DingTalk carries the configured token to verify the event source when it pushes subscribed events. It also uses this key to symmetrically encrypt the message content. Click here to get the callback encryption and decryption library and the corresponding demo. The DingTalk server encodes the plaintext msg message body into encrypt. encrypt = Base64_Encode(AES_Encrypt[random(16B) + msg_len(4B) + msg + $key]) is the Base64 encoding of the encrypted plaintext message msg. Where:
  • random is a 16-byte random string.
  • msg_len is the 4-byte length of msg, in network byte order.
  • msg is the plaintext of the message body.
  • key is the appKey of the app.
Extract the encrypt field from the returned JSON:
  • Base64-decode the ciphertext: aes_msg=Base64_Decode(encrypt);
  • Use AESKey to perform AES decryption: rand_msg=AES_Decrypt(aes_msg);
The encryption and decryption code example is as follows: Note
  • The encryption and decryption process in this code example depends on the DingCallbackCrypto utility class. See dingtalk-callback-Crypto.
  • The Constant.OWNER_KEY in the example is described as follows:
    • When you use the method in this document to receive subscribed events pushed by DingTalk, they are pushed at the app dimension. OWNER_KEY is the App Key of the app, which can be obtained on the app details page in the Developer Console.
    • When you use the HTTP callback registration API method to receive subscribed events pushed by DingTalk, they are pushed at the organization dimension. OWNER_KEY is the CorpId.
The following is the data format of the Contacts change event after decryption:

FAQ

  • Problem Description When you click Save, the page reports the error “HTTP request result verification returned field value failed”, as shown in the figure below.
  • Cause
    • One of the field values in the JSON information returned to the DingTalk server is incorrect.
    • The information returned to the DingTalk server is not in JSON format.
  • Solution Construct a main method, use the four field values returned by the callback URL, call the encryption API, and verify whether the obtained value is the success string. For example:
    The run result at this point is as follows:
    • If you get the success string, the returned value is correct, and the problem lies in the incorrect format of the value parameters returned by the callback API to the DingTalk server. Confirm them again.
    • If the run produces an error, the common runtime errors and causes are as follows: