Skip to main content
If you need to implement bot reply or bot message sending, follow the steps in this document to develop bot reply or bot message sending.

Background

Replying to a message with a bot is essentially the same process as sending a message with a bot. Therefore, in this document, replying and sending have the same meaning. Messages can be sent through either the Server API or a Webhook. The following sections introduce both methods.

Prerequisites

  1. Complete the Create an app process.
  2. Complete the process of adding app capabilities.

Send messages through a Webhook

Send messages through a Webhook

Sending messages through a webhook supports the @ feature. See the integration example below for details.
  1. Send messages through the app bot Webhook URL:
    1. In Bot receives messages, obtain the Webhook URL of the app bot in the group. For details, see Obtain the app bot Webhook.
    2. Java development environment setup:
      • Import the Java SDK. For SDKs in other languages, see the legacy Server-side SDK download.
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
        
    3. Integration code example:
      public void sendMessageWebhook() throws ApiException {
              DingTalkClient client = new DefaultDingTalkClient("{ Webhook url }");
              OapiRobotSendRequest request = new OapiRobotSendRequest();
              request.setMsgtype("text");
              OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
              text.setContent("Test text message");
              request.setText(text);
              OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
              at.setAtUserIds(Arrays.asList("4525xxxxxxxxx7041"));
              at.setIsAtAll(false);
              request.setAt(at);
              OapiRobotSendResponse response = client.execute(request);
              System.out.println(response.getBody());
      }
      
      For more message types, see the message types supported by Custom bot message sending.
  2. Send messages through the sessionWebhook field returned when the bot receives a message.
    1. Obtain the sessionWebhook URL from the JSON data returned in Bot receives messages.
    2. Java development environment setup:
      • Import the Java SDK. For SDKs in other languages, see the legacy Server-side SDK download.
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
        
    3. Integration code example:
      public void sendMessageWebhook() throws ApiException {
              DingTalkClient client = new DefaultDingTalkClient("{ SessionWebhook url }");
              OapiRobotSendRequest request = new OapiRobotSendRequest();
              request.setMsgtype("text");
              OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
              text.setContent("Test text message");
              request.setText(text);
              OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
              at.setAtUserIds(Arrays.asList("4525xxxxxxxxx7041"));
              at.setIsAtAll(false);
              request.setAt(at);
              OapiRobotSendResponse response = client.execute(request);
              System.out.println(response.getBody());
      }
      
      For more message types, see the message types supported by Custom bot message sending.

Send messages through the Server API

The following table lists the Server APIs for sending messages. For more API details, see Bot Server API.
API nameDescription
Bot sends group chat messagesSend messages in a group. The @ feature is not supported yet.
Send bot messages in one-on-one chats in batchesSend messages in person-to-bot chats. The @ feature is not supported yet.
Bot sends plain messages in person-to-person chatsSend messages in person-to-person chats. The @ feature is not supported yet. Note Use only in Direct Message Cool App scenarios.
For details on calling the Server API, see the document on how to call the Server API.

Next steps

After bot message sending development is complete, publish the app.