This article describes how to push the data that is submitted by a device to a DingTalk group by using a data forwarding rule. A thermo-hygrometer is used in this example.


Push the data that is submitted by thermo-hygrometers in office rooms to DingTalk chatbots.


Data forwarding process

Step 1: Create a product and a device

  1. Log on to the IoT Platform console.
  2. In the left-side navigation pane, choose Devices > Products and create a thermo-hygrometer product. When you create the product, set Node Type to Directly Connected Device.
    Use default values for other parameters. For more information, see Create a product.
  3. Click Create TSL. On the Define Feature tab, click Edit Draft. In the Default Module section, add custom features to the product.
    In this example, add the temperature and humidity properties to the product. For more information, see Add a TSL feature.
  4. In the left-side navigation pane, choose Devices > Devices. Create a device named TH_sensor under the product. For more information, see Create a device.
    In the The devices have been added dialog box, click Learn More to obtain the device certificate (ProductKey, DeviceName, and DeviceSecret). You must secure the device certificate. The certificate is a key credential for subsequent communication between the device and IoT Platform.
  5. On the Devices tab, find the device and click View in the Actions column. The Device Details page appears. In the Tag Information section, click Edit to add tags to the device.

    In this example, add the following two tags. For more information, see Tags.

    Key Value Description
    tag Room 00XS, Floor F, Building X, YY Town The location of the device.
    deviceISN T20180102X The serial number (SN) of the device.

Step 2: Configure Function Compute

Function Compute is an event-driven, fully managed computing service. The programming languages that are supported by Function Compute include Java, Node.js, and Python.For more information, see How to use Function Compute.

  1. Specify the webhook URL of a DingTalk chatbot.
    1. Log on to DingTalk on your PC.
    2. In the DingTalk chat window, click the Group Settings icon. On the panel that appears, click Group Assistant.
    3. Click Add Robot, and then click the Add a DingTalk chatbot icon.
    4. Click Custom, and then click Add.
    5. Specify the Chatbot name and Security Settings parameters, select I have read and accepted《DingTalk Custom Robot Service Terms of Service》, and then click Finished.
    6. Click Copy to save the webhook URL on your PC.
  2. Write a Function Compute script.
    In this example, the Node.js runtime environment is used. The function obtains device data from IoT Platform, processes the data based on the specified DingTalk message format, and then sends the data the webhook URL of the specified DingTalk chatbot by using the HTTPS POST method. The device data includes the device location, device SN, real-time temperature and humidity data, and the time when the device submits the data to IoT Platform.

    After you write the script, name the script file as index.js, and then compress it into the file. The following sample code can be used.

    You must replace accessToken with the value of the access_token parameter in the webhook URL.

    const https = require('https');
    const accessToken = 'Specify the value of the access_token parameter in the webhook URL.
    module.exports.handler = function(event, context, callback) {
    var eventJson = JSON.parse(event.toString());
    // DingTalk message format
    const postData = JSON.stringify({
    "msgtype": "markdown",
    "markdown": {
    "title": "Thermo-hygrometer",
    "text": "#### Submit data\n" +
    "> Device location: " + eventJson.tag + "\n\n" +
    "> Device SN: " + eventJson.isn+ "\n\n" +
    "> Temperature: " + eventJson.temperature + "℃\n\n" +
    "> Humidity: " + eventJson.humidity + "%\n\n" +
    "> ###### " + eventJson.time + " published by [IoT Platform]( \n"
    "at": {
    "isAtAll": false
    const options = {
    hostname: '',
    port: 443,
    path: '/robot/send?access_token=' + accessToken,
    method: 'POST',
    headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
    const req = https.request(options, (res) => {
    res.on('data', (chunk) => {});
    res.on('end', () => {
    callback(null, 'success');
    // Return an error.
    req.on('error', (e) => {
    // Write the data.
  3. Create a service and a function.
    1. For information about how to activate Alibaba Cloud Function Compute, see Activate Function Compute.
    2. Log on to the Function Compute console. In the left-side navigation pane, click Services and Functions.
    3. Click Create Service, set Service Name to IoT_Service, and then click Submit.
    4. On the Services tab, find IoT_Service and click Create Function.
    5. Move the pointer over Event Function and click Configure and Deploy.
    6. Set the parameters of the function. Use the default values for other parameters, and then click Create.
      Parameter Description
      Function Name Enter pushData2DingTalk.
      Runtime Select Node.JS 6.x and set Upload Code to Upload Zip File. Click Upload Code next to Upload Zip File and upload the file that is created in Step 2.

Step 3: Forward data to Function Compute

Configure a data forwarding rule to forward the temperature and humidity data submitted by the TH_sensor device to the pushData2DingTalk function.

  1. Log on to the IoT Platform console.In the left-side navigation pane, choose Rules > Data Forwarding. On the Data Forwarding page, click Create Rule. Set the parameters and click OK.
  2. On the Data Forwarding Rule page, click Write SQL to edit an SQL statement.
    In this example, specify the following fields to filter data:
    • The fields that indicate the device information: deviceName, tag, and deviceISN.
    • The fields that indicate the submitted payloads: temperature, and humidity.

    The following script shows the sample SQL statement:

    deviceName() as deviceName, 
    attribute('tag') as tag, attribute('deviceISN') as isn, 
    items.temperature.value as temperature, items.humidity.value as humidity, 
    timestamp('yyyy-MM-dd HH:mm:ss') as time 
  3. On the Data Forwarding Rule page, click Add Operation to forward data to Function Compute.
    In this example, select the created IoT_Service service and pushData2DingTalk function. For more information, see Forward data to Function Compute.
  4. On the Data Forwarding page, find the rule and click Start in the Actions column to enable the rule.

Step 4: Connect the thermo-hygrometer device to IoT Platform and submit temperature and humidity data

Connect the device to IoT Platform over MQTT by using the device certificate. Then, simulate the submission of temperature and humidity data.

  1. Download and install Node.js on the Windows or Linux operating system. In this example, the Windows operating system is used.
    node --version
  2. Create a JavaScript file such as iot_device.js on your PC to store the Node.js sample code.
    The following Node.js sample code can be used:
    const mqtt = require('aliyun-iot-mqtt');
    // 1. Specify the device identity information
    var options = {
        productKey: "a1***",
        deviceName: "TH_sensor",
        deviceSecret: "9JtvE***",
        regionId: 'cn-shanghai'
    // 1. Create a connection
    const client = mqtt.getAliyunIotMqttClient(options);
    // 2. Configure a listener to receive downstream commands
    client.on('message', function(topic, message) {
        console.log("topic " + topic)
        console.log("message " + message)
    setInterval(function() {
        // 3.Submit data
        client.publish(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`, getPostData(), { qos: 0 });
    }, 6 * 1000);
    function getPostData() {
        const payloadJson = {
            version: "1.0",
            params: {
                Temperature: Math.floor((Math.random() * 20) + 10),
                Humidity: Math.floor((Math.random() * 20) + 10)
            method: ""
        console.log("payloadJson " + JSON.stringify(payloadJson))
        return JSON.stringify(payloadJson);
    Parameter Example Description
    productKey a1*** The ProductKey of the product to which the device belongs. You can view the ProductKey on the Device Details page of the IoT Platform console.
    deviceName TH_sensor The DeviceName of the device. You can view the DeviceName on the Device Details page of the IoT Platform console.
    deviceSecret 9JtvE*** The DeviceSecret of the device. You can view the DeviceSecret on the Device Details page of the IoT Platform console.
    regionId cn-shanghai The ID of the region where the MQTT device resides. For more information about region IDs, see Regions and zones.
  3. Open the command-line interface and run the cd command to go to the directory where the iot_device.js file resides. In this directory, run the NPM command to download the aliyun-iot-mqtt library.
    npm install aliyun-iot-mqtt -S

    The following figure shows the downloaded library file.

  4. In the command-line interface, enter the following command to run the iot_device.js file. This way, the device is enabled and data is submitted.
    node iot_device.js


The following figure shows the result. Submit data

The following figure shows the message that is received by the DingTalk chatbot.

DingTalk chatbot