1
0
mirror of synced 2025-11-06 04:20:53 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
a620b2c9fc Add documentation and fix test imports for intelligent robot interface
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
2025-08-31 15:00:43 +00:00
copilot-swe-agent[bot]
c08d81d28e Implement enterprise WeChat intelligent robot interface
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
2025-08-31 14:59:04 +00:00
copilot-swe-agent[bot]
bb2598da20 Initial plan 2025-08-31 14:49:03 +00:00
13 changed files with 679 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
# 企业微信智能机器人接口
本模块提供企业微信智能机器人相关的API接口实现。
## 官方文档
- [企业微信智能机器人接口](https://developer.work.weixin.qq.com/document/path/101039)
## 接口说明
### 获取服务实例
```java
WxCpService wxCpService = ...; // 初始化企业微信服务
WxCpIntelligentRobotService robotService = wxCpService.getIntelligentRobotService();
```
### 创建智能机器人
```java
WxCpIntelligentRobotCreateRequest request = new WxCpIntelligentRobotCreateRequest();
request.setName("我的智能机器人");
request.setDescription("这是一个智能客服机器人");
request.setAvatar("http://example.com/avatar.jpg");
WxCpIntelligentRobotCreateResponse response = robotService.createRobot(request);
String robotId = response.getRobotId();
```
### 更新智能机器人
```java
WxCpIntelligentRobotUpdateRequest request = new WxCpIntelligentRobotUpdateRequest();
request.setRobotId("robot_id_here");
request.setName("更新后的机器人名称");
request.setDescription("更新后的描述");
request.setStatus(1); // 1:启用, 0:停用
robotService.updateRobot(request);
```
### 查询智能机器人
```java
String robotId = "robot_id_here";
WxCpIntelligentRobot robot = robotService.getRobot(robotId);
System.out.println("机器人名称: " + robot.getName());
System.out.println("机器人状态: " + robot.getStatus());
```
### 智能对话
```java
WxCpIntelligentRobotChatRequest request = new WxCpIntelligentRobotChatRequest();
request.setRobotId("robot_id_here");
request.setUserid("user123");
request.setMessage("你好,请问如何使用这个功能?");
request.setSessionId("session123"); // 可选,用于保持会话连续性
WxCpIntelligentRobotChatResponse response = robotService.chat(request);
String reply = response.getReply();
String sessionId = response.getSessionId();
```
### 重置会话
```java
String robotId = "robot_id_here";
String userid = "user123";
String sessionId = "session123";
robotService.resetSession(robotId, userid, sessionId);
```
### 删除智能机器人
```java
String robotId = "robot_id_here";
robotService.deleteRobot(robotId);
```
## 主要类说明
### 请求类
- `WxCpIntelligentRobotCreateRequest`: 创建机器人请求
- `WxCpIntelligentRobotUpdateRequest`: 更新机器人请求
- `WxCpIntelligentRobotChatRequest`: 智能对话请求
### 响应类
- `WxCpIntelligentRobotCreateResponse`: 创建机器人响应
- `WxCpIntelligentRobotChatResponse`: 智能对话响应
- `WxCpIntelligentRobot`: 机器人信息实体
### 服务接口
- `WxCpIntelligentRobotService`: 智能机器人服务接口
- `WxCpIntelligentRobotServiceImpl`: 智能机器人服务实现
## 注意事项
1. 需要确保企业微信应用具有智能机器人相关权限
2. 智能机器人功能可能需要特定的企业微信版本支持
3. 会话ID可以用于保持对话的连续性提升用户体验
4. 机器人状态: 0表示停用1表示启用

View File

@@ -0,0 +1,67 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.intelligentrobot.*;
/**
* 企业微信智能机器人接口
* 官方文档: https://developer.work.weixin.qq.com/document/path/101039
*
* @author Binary Wang
*/
public interface WxCpIntelligentRobotService {
/**
* 创建智能机器人
*
* @param request 创建请求参数
* @return 创建结果
* @throws WxErrorException 微信接口异常
*/
WxCpIntelligentRobotCreateResponse createRobot(WxCpIntelligentRobotCreateRequest request) throws WxErrorException;
/**
* 删除智能机器人
*
* @param robotId 机器人ID
* @throws WxErrorException 微信接口异常
*/
void deleteRobot(String robotId) throws WxErrorException;
/**
* 更新智能机器人
*
* @param request 更新请求参数
* @throws WxErrorException 微信接口异常
*/
void updateRobot(WxCpIntelligentRobotUpdateRequest request) throws WxErrorException;
/**
* 查询智能机器人
*
* @param robotId 机器人ID
* @return 机器人信息
* @throws WxErrorException 微信接口异常
*/
WxCpIntelligentRobot getRobot(String robotId) throws WxErrorException;
/**
* 智能机器人会话
*
* @param request 聊天请求参数
* @return 聊天响应
* @throws WxErrorException 微信接口异常
*/
WxCpIntelligentRobotChatResponse chat(WxCpIntelligentRobotChatRequest request) throws WxErrorException;
/**
* 重置智能机器人会话
*
* @param robotId 机器人ID
* @param userid 用户ID
* @param sessionId 会话ID
* @throws WxErrorException 微信接口异常
*/
void resetSession(String robotId, String userid, String sessionId) throws WxErrorException;
}

View File

@@ -587,4 +587,11 @@ public interface WxCpService extends WxService {
* @return
*/
WxCpCorpGroupService getCorpGroupService();
/**
* 获取智能机器人服务
*
* @return 智能机器人服务 intelligent robot service
*/
WxCpIntelligentRobotService getIntelligentRobotService();
}

View File

@@ -74,6 +74,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private final WxCpMeetingService meetingService = new WxCpMeetingServiceImpl(this);
private final WxCpCorpGroupService corpGroupService = new WxCpCorpGroupServiceImpl(this);
private final WxCpIntelligentRobotService intelligentRobotService = new WxCpIntelligentRobotServiceImpl(this);
/**
* 全局的是否正在刷新access token的锁.
@@ -702,4 +703,9 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
public WxCpCorpGroupService getCorpGroupService() {
return corpGroupService;
}
@Override
public WxCpIntelligentRobotService getIntelligentRobotService() {
return this.intelligentRobotService;
}
}

View File

@@ -0,0 +1,64 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpIntelligentRobotService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.intelligentrobot.*;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.IntelligentRobot.*;
/**
* 企业微信智能机器人接口实现
*
* @author Binary Wang
*/
@RequiredArgsConstructor
public class WxCpIntelligentRobotServiceImpl implements WxCpIntelligentRobotService {
private final WxCpService cpService;
@Override
public WxCpIntelligentRobotCreateResponse createRobot(WxCpIntelligentRobotCreateRequest request) throws WxErrorException {
String responseText = this.cpService.post(CREATE_ROBOT, request.toJson());
return WxCpIntelligentRobotCreateResponse.fromJson(responseText);
}
@Override
public void deleteRobot(String robotId) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("robot_id", robotId);
this.cpService.post(DELETE_ROBOT, jsonObject.toString());
}
@Override
public void updateRobot(WxCpIntelligentRobotUpdateRequest request) throws WxErrorException {
this.cpService.post(UPDATE_ROBOT, request.toJson());
}
@Override
public WxCpIntelligentRobot getRobot(String robotId) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("robot_id", robotId);
String responseText = this.cpService.post(GET_ROBOT, jsonObject.toString());
return WxCpIntelligentRobot.fromJson(responseText);
}
@Override
public WxCpIntelligentRobotChatResponse chat(WxCpIntelligentRobotChatRequest request) throws WxErrorException {
String responseText = this.cpService.post(CHAT, request.toJson());
return WxCpIntelligentRobotChatResponse.fromJson(responseText);
}
@Override
public void resetSession(String robotId, String userid, String sessionId) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("robot_id", robotId);
jsonObject.addProperty("userid", userid);
jsonObject.addProperty("session_id", sessionId);
this.cpService.post(RESET_SESSION, jsonObject.toString());
}
}

View File

@@ -0,0 +1,77 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 智能机器人信息
*
* @author Binary Wang
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class WxCpIntelligentRobot extends WxCpBaseResp implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人ID
*/
@SerializedName("robot_id")
private String robotId;
/**
* 机器人名称
*/
@SerializedName("name")
private String name;
/**
* 机器人描述
*/
@SerializedName("description")
private String description;
/**
* 机器人头像
*/
@SerializedName("avatar")
private String avatar;
/**
* 机器人状态 0:停用 1:启用
*/
@SerializedName("status")
private Integer status;
/**
* 创建时间
*/
@SerializedName("create_time")
private Long createTime;
/**
* 更新时间
*/
@SerializedName("update_time")
private Long updateTime;
/**
* From json wx cp intelligent robot.
*
* @param json the json
* @return the wx cp intelligent robot
*/
public static WxCpIntelligentRobot fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobot.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
}

View File

@@ -0,0 +1,49 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 智能机器人聊天请求
*
* @author Binary Wang
*/
@Data
public class WxCpIntelligentRobotChatRequest implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人ID
*/
@SerializedName("robot_id")
private String robotId;
/**
* 用户ID
*/
@SerializedName("userid")
private String userid;
/**
* 消息内容
*/
@SerializedName("message")
private String message;
/**
* 会话ID可选用于保持会话连续性
*/
@SerializedName("session_id")
private String sessionId;
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpIntelligentRobotChatRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotChatRequest.class);
}
}

View File

@@ -0,0 +1,46 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 智能机器人聊天响应
*
* @author Binary Wang
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class WxCpIntelligentRobotChatResponse extends WxCpBaseResp implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人回复内容
*/
@SerializedName("reply")
private String reply;
/**
* 会话ID
*/
@SerializedName("session_id")
private String sessionId;
/**
* 消息ID
*/
@SerializedName("msg_id")
private String msgId;
public static WxCpIntelligentRobotChatResponse fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotChatResponse.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
}

View File

@@ -0,0 +1,43 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 创建智能机器人请求
*
* @author Binary Wang
*/
@Data
public class WxCpIntelligentRobotCreateRequest implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人名称
*/
@SerializedName("name")
private String name;
/**
* 机器人描述
*/
@SerializedName("description")
private String description;
/**
* 机器人头像
*/
@SerializedName("avatar")
private String avatar;
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpIntelligentRobotCreateRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotCreateRequest.class);
}
}

View File

@@ -0,0 +1,34 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 创建智能机器人响应
*
* @author Binary Wang
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class WxCpIntelligentRobotCreateResponse extends WxCpBaseResp implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人ID
*/
@SerializedName("robot_id")
private String robotId;
public static WxCpIntelligentRobotCreateResponse fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotCreateResponse.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
}

View File

@@ -0,0 +1,55 @@
package me.chanjar.weixin.cp.bean.intelligentrobot;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
/**
* 更新智能机器人请求
*
* @author Binary Wang
*/
@Data
public class WxCpIntelligentRobotUpdateRequest implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 机器人ID
*/
@SerializedName("robot_id")
private String robotId;
/**
* 机器人名称
*/
@SerializedName("name")
private String name;
/**
* 机器人描述
*/
@SerializedName("description")
private String description;
/**
* 机器人头像
*/
@SerializedName("avatar")
private String avatar;
/**
* 机器人状态 0:停用 1:启用
*/
@SerializedName("status")
private Integer status;
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
public static WxCpIntelligentRobotUpdateRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotUpdateRequest.class);
}
}

View File

@@ -1627,4 +1627,40 @@ public interface WxCpApiPathConsts {
*/
String CONVERT_TMP_EXTERNAL_USER_ID = "/cgi-bin/idconvert/convert_tmp_external_userid";
}
/**
* 智能机器人相关接口
* 官方文档: https://developer.work.weixin.qq.com/document/path/101039
*/
interface IntelligentRobot {
/**
* 创建智能机器人
*/
String CREATE_ROBOT = "/cgi-bin/intelligent_robot/create";
/**
* 删除智能机器人
*/
String DELETE_ROBOT = "/cgi-bin/intelligent_robot/delete";
/**
* 更新智能机器人
*/
String UPDATE_ROBOT = "/cgi-bin/intelligent_robot/update";
/**
* 查询智能机器人
*/
String GET_ROBOT = "/cgi-bin/intelligent_robot/get";
/**
* 智能机器人会话
*/
String CHAT = "/cgi-bin/intelligent_robot/chat";
/**
* 重置智能机器人会话
*/
String RESET_SESSION = "/cgi-bin/intelligent_robot/reset_session";
}
}

View File

@@ -0,0 +1,88 @@
package me.chanjar.weixin.cp.api.impl;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.bean.intelligentrobot.*;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import javax.inject.Inject;
/**
* 智能机器人接口测试
*
* @author Binary Wang
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxCpIntelligentRobotServiceImplTest {
@Inject
private WxCpService wxCpService;
@Test
public void testCreateRobot() {
// 测试创建智能机器人请求对象创建
WxCpIntelligentRobotCreateRequest request = new WxCpIntelligentRobotCreateRequest();
request.setName("测试机器人");
request.setDescription("这是一个测试的智能机器人");
request.setAvatar("avatar_url");
// 验证JSON序列化
String json = request.toJson();
assert json.contains("测试机器人");
assert json.contains("这是一个测试的智能机器人");
// 验证反序列化
WxCpIntelligentRobotCreateRequest fromJson = WxCpIntelligentRobotCreateRequest.fromJson(json);
assert fromJson.getName().equals("测试机器人");
}
@Test
public void testChatRequest() {
// 测试聊天请求对象创建
WxCpIntelligentRobotChatRequest request = new WxCpIntelligentRobotChatRequest();
request.setRobotId("robot123");
request.setUserid("user123");
request.setMessage("你好,机器人");
request.setSessionId("session123");
// 验证JSON序列化
String json = request.toJson();
assert json.contains("robot123");
assert json.contains("你好,机器人");
// 验证反序列化
WxCpIntelligentRobotChatRequest fromJson = WxCpIntelligentRobotChatRequest.fromJson(json);
assert fromJson.getRobotId().equals("robot123");
assert fromJson.getMessage().equals("你好,机器人");
}
@Test
public void testUpdateRequest() {
// 测试更新请求对象创建
WxCpIntelligentRobotUpdateRequest request = new WxCpIntelligentRobotUpdateRequest();
request.setRobotId("robot123");
request.setName("更新后的机器人");
request.setDescription("更新后的描述");
request.setStatus(1);
// 验证JSON序列化
String json = request.toJson();
assert json.contains("robot123");
assert json.contains("更新后的机器人");
// 验证反序列化
WxCpIntelligentRobotUpdateRequest fromJson = WxCpIntelligentRobotUpdateRequest.fromJson(json);
assert fromJson.getRobotId().equals("robot123");
assert fromJson.getName().equals("更新后的机器人");
assert fromJson.getStatus().equals(1);
}
@Test
public void testServiceIntegration() {
// 验证服务可以正确获取
assert this.wxCpService.getIntelligentRobotService() != null;
assert this.wxCpService.getIntelligentRobotService() instanceof WxCpIntelligentRobotServiceImpl;
}
}