> ## Documentation Index
> Fetch the complete documentation index at: https://help.dingtalk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 机器人回复/发送消息

> 介绍机器人回复和主动发送消息的两种实现方式，包括通过 Webhook 发送消息和通过服务端 API 发送消息。

如果你需要实现机器人回复或机器人发送消息，可依据本文档操作步骤实现机器人回复或机器人发送消息的开发。

## 背景信息

机器人回复消息本质上就是机器人发送消息的过程。因此本文中，回复消息和发送消息具有相同的含义。

发送消息的方式可以通过服务端 API 和 Webhook 的方式进行发送，下面将针对两种发送消息的方式进行介绍。

## 前提条件

1. 需要完成[创建应用](/zh/open/dingstart/create-application)流程。
2. 需要完成添加应用能力流程

## 通过 Webhook 发送消息

### 通过 Webhook 方式发送消息

<Note>
  通过 webhook 方式发送消息，支持 @ 功能，具体请查下下方接入示例。
</Note>

1. 通过应用机器人 Webhook url 进行发送消息：

   1. 在[机器人接收消息](/zh/open/dingstart/robot-receive-message)中，获取群内应用机器人的 Webhook url 地址。详情参考[获取应用机器人 Webhook](/zh/open/development/faq-robot)。
   2. Java 开发环境准备：

      * 引入 Java SDK ，更多语言 SDK 参考旧版服务端SDK下载。

        ```xml lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
        ```
   3. 接入代码示例：

      ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
      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("测试文本消息");
              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());
      }
      ```

      更多消息类型，请参考自定义机器人发送消息的消息类型。
2. 通过机器人接收消息中 sessionWebhook 字段，进行消息发送。

   1. 获取[机器人接收消息](/zh/open/dingstart/robot-receive-message)的 JSON 数据中 sessionWebhook 地址。
   2. Java 开发环境准备：

      * 引入 Java SDK ，更多语言 SDK 参考旧版服务端SDK下载。

        ```xml lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
        ```
   3. 接入代码示例：

      ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
      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("测试文本消息");
              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());
      }
      ```

      更多消息类型，请参考自定义机器人发送消息的消息类型。

## 通过服务端 API 发送消息

服务端 API 发送消息列表，更多 API 内容参考[机器人服务端 API](/zh/open/development/development-robot-overview)。

| **API 名称**                                                                                                   | **描述**                                        |
| ------------------------------------------------------------------------------------------------------------ | --------------------------------------------- |
| [机器人发送群聊消息](/zh/open/development/the-robot-sends-a-group-message)                                            | 群内发送消息。  暂不支持 @ 功能                            |
| [批量发送人与机器人会话中机器人消息](/zh/open/development/chatbots-send-one-on-one-chat-messages-in-batches)                  | 人与机器人会话中发送消息。  暂不支持 @ 功能                      |
| [人与人会话中机器人发送普通消息](/zh/open/development/the-robot-sends-ordinary-messages-in-a-person-to-person-conversation) | 人与人会话中发送消息。  暂不支持 @ 功能  **说明**  仅在单聊酷应用场景下使用。 |

服务端 API 调用详情参考文档如何调用服务端API。

## 后续步骤

机器人发送消息开发完成后，需进行发布应用。
