Skip to content

Commit 23a2e18

Browse files
authored
🆕 #3682 【企业微信】新增智能机器人的接口支持
1 parent 8f20f3b commit 23a2e18

13 files changed

+679
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# 企业微信智能机器人接口
2+
3+
本模块提供企业微信智能机器人相关的API接口实现。
4+
5+
## 官方文档
6+
7+
- [企业微信智能机器人接口](https://developer.work.weixin.qq.com/document/path/101039)
8+
9+
## 接口说明
10+
11+
### 获取服务实例
12+
13+
```java
14+
WxCpService wxCpService = ...; // 初始化企业微信服务
15+
WxCpIntelligentRobotService robotService = wxCpService.getIntelligentRobotService();
16+
```
17+
18+
### 创建智能机器人
19+
20+
```java
21+
WxCpIntelligentRobotCreateRequest request = new WxCpIntelligentRobotCreateRequest();
22+
request.setName("我的智能机器人");
23+
request.setDescription("这是一个智能客服机器人");
24+
request.setAvatar("http://example.com/avatar.jpg");
25+
26+
WxCpIntelligentRobotCreateResponse response = robotService.createRobot(request);
27+
String robotId = response.getRobotId();
28+
```
29+
30+
### 更新智能机器人
31+
32+
```java
33+
WxCpIntelligentRobotUpdateRequest request = new WxCpIntelligentRobotUpdateRequest();
34+
request.setRobotId("robot_id_here");
35+
request.setName("更新后的机器人名称");
36+
request.setDescription("更新后的描述");
37+
request.setStatus(1); // 1:启用, 0:停用
38+
39+
robotService.updateRobot(request);
40+
```
41+
42+
### 查询智能机器人
43+
44+
```java
45+
String robotId = "robot_id_here";
46+
WxCpIntelligentRobot robot = robotService.getRobot(robotId);
47+
48+
System.out.println("机器人名称: " + robot.getName());
49+
System.out.println("机器人状态: " + robot.getStatus());
50+
```
51+
52+
### 智能对话
53+
54+
```java
55+
WxCpIntelligentRobotChatRequest request = new WxCpIntelligentRobotChatRequest();
56+
request.setRobotId("robot_id_here");
57+
request.setUserid("user123");
58+
request.setMessage("你好,请问如何使用这个功能?");
59+
request.setSessionId("session123"); // 可选,用于保持会话连续性
60+
61+
WxCpIntelligentRobotChatResponse response = robotService.chat(request);
62+
String reply = response.getReply();
63+
String sessionId = response.getSessionId();
64+
```
65+
66+
### 重置会话
67+
68+
```java
69+
String robotId = "robot_id_here";
70+
String userid = "user123";
71+
String sessionId = "session123";
72+
73+
robotService.resetSession(robotId, userid, sessionId);
74+
```
75+
76+
### 删除智能机器人
77+
78+
```java
79+
String robotId = "robot_id_here";
80+
robotService.deleteRobot(robotId);
81+
```
82+
83+
## 主要类说明
84+
85+
### 请求类
86+
87+
- `WxCpIntelligentRobotCreateRequest`: 创建机器人请求
88+
- `WxCpIntelligentRobotUpdateRequest`: 更新机器人请求
89+
- `WxCpIntelligentRobotChatRequest`: 智能对话请求
90+
91+
### 响应类
92+
93+
- `WxCpIntelligentRobotCreateResponse`: 创建机器人响应
94+
- `WxCpIntelligentRobotChatResponse`: 智能对话响应
95+
- `WxCpIntelligentRobot`: 机器人信息实体
96+
97+
### 服务接口
98+
99+
- `WxCpIntelligentRobotService`: 智能机器人服务接口
100+
- `WxCpIntelligentRobotServiceImpl`: 智能机器人服务实现
101+
102+
## 注意事项
103+
104+
1. 需要确保企业微信应用具有智能机器人相关权限
105+
2. 智能机器人功能可能需要特定的企业微信版本支持
106+
3. 会话ID可以用于保持对话的连续性,提升用户体验
107+
4. 机器人状态: 0表示停用,1表示启用
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.intelligentrobot.*;
5+
6+
/**
7+
* 企业微信智能机器人接口
8+
* 官方文档: https://developer.work.weixin.qq.com/document/path/101039
9+
*
10+
* @author Binary Wang
11+
*/
12+
public interface WxCpIntelligentRobotService {
13+
14+
/**
15+
* 创建智能机器人
16+
*
17+
* @param request 创建请求参数
18+
* @return 创建结果
19+
* @throws WxErrorException 微信接口异常
20+
*/
21+
WxCpIntelligentRobotCreateResponse createRobot(WxCpIntelligentRobotCreateRequest request) throws WxErrorException;
22+
23+
/**
24+
* 删除智能机器人
25+
*
26+
* @param robotId 机器人ID
27+
* @throws WxErrorException 微信接口异常
28+
*/
29+
void deleteRobot(String robotId) throws WxErrorException;
30+
31+
/**
32+
* 更新智能机器人
33+
*
34+
* @param request 更新请求参数
35+
* @throws WxErrorException 微信接口异常
36+
*/
37+
void updateRobot(WxCpIntelligentRobotUpdateRequest request) throws WxErrorException;
38+
39+
/**
40+
* 查询智能机器人
41+
*
42+
* @param robotId 机器人ID
43+
* @return 机器人信息
44+
* @throws WxErrorException 微信接口异常
45+
*/
46+
WxCpIntelligentRobot getRobot(String robotId) throws WxErrorException;
47+
48+
/**
49+
* 智能机器人会话
50+
*
51+
* @param request 聊天请求参数
52+
* @return 聊天响应
53+
* @throws WxErrorException 微信接口异常
54+
*/
55+
WxCpIntelligentRobotChatResponse chat(WxCpIntelligentRobotChatRequest request) throws WxErrorException;
56+
57+
/**
58+
* 重置智能机器人会话
59+
*
60+
* @param robotId 机器人ID
61+
* @param userid 用户ID
62+
* @param sessionId 会话ID
63+
* @throws WxErrorException 微信接口异常
64+
*/
65+
void resetSession(String robotId, String userid, String sessionId) throws WxErrorException;
66+
67+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,11 @@ public interface WxCpService extends WxService {
587587
* @return
588588
*/
589589
WxCpCorpGroupService getCorpGroupService();
590+
591+
/**
592+
* 获取智能机器人服务
593+
*
594+
* @return 智能机器人服务 intelligent robot service
595+
*/
596+
WxCpIntelligentRobotService getIntelligentRobotService();
590597
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
7474

7575
private final WxCpMeetingService meetingService = new WxCpMeetingServiceImpl(this);
7676
private final WxCpCorpGroupService corpGroupService = new WxCpCorpGroupServiceImpl(this);
77+
private final WxCpIntelligentRobotService intelligentRobotService = new WxCpIntelligentRobotServiceImpl(this);
7778

7879
/**
7980
* 全局的是否正在刷新access token的锁.
@@ -702,4 +703,9 @@ public WxCpMeetingService getMeetingService() {
702703
public WxCpCorpGroupService getCorpGroupService() {
703704
return corpGroupService;
704705
}
706+
707+
@Override
708+
public WxCpIntelligentRobotService getIntelligentRobotService() {
709+
return this.intelligentRobotService;
710+
}
705711
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.gson.JsonObject;
4+
import lombok.RequiredArgsConstructor;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.cp.api.WxCpIntelligentRobotService;
7+
import me.chanjar.weixin.cp.api.WxCpService;
8+
import me.chanjar.weixin.cp.bean.intelligentrobot.*;
9+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
10+
11+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.IntelligentRobot.*;
12+
13+
/**
14+
* 企业微信智能机器人接口实现
15+
*
16+
* @author Binary Wang
17+
*/
18+
@RequiredArgsConstructor
19+
public class WxCpIntelligentRobotServiceImpl implements WxCpIntelligentRobotService {
20+
21+
private final WxCpService cpService;
22+
23+
@Override
24+
public WxCpIntelligentRobotCreateResponse createRobot(WxCpIntelligentRobotCreateRequest request) throws WxErrorException {
25+
String responseText = this.cpService.post(CREATE_ROBOT, request.toJson());
26+
return WxCpIntelligentRobotCreateResponse.fromJson(responseText);
27+
}
28+
29+
@Override
30+
public void deleteRobot(String robotId) throws WxErrorException {
31+
JsonObject jsonObject = new JsonObject();
32+
jsonObject.addProperty("robot_id", robotId);
33+
this.cpService.post(DELETE_ROBOT, jsonObject.toString());
34+
}
35+
36+
@Override
37+
public void updateRobot(WxCpIntelligentRobotUpdateRequest request) throws WxErrorException {
38+
this.cpService.post(UPDATE_ROBOT, request.toJson());
39+
}
40+
41+
@Override
42+
public WxCpIntelligentRobot getRobot(String robotId) throws WxErrorException {
43+
JsonObject jsonObject = new JsonObject();
44+
jsonObject.addProperty("robot_id", robotId);
45+
String responseText = this.cpService.post(GET_ROBOT, jsonObject.toString());
46+
return WxCpIntelligentRobot.fromJson(responseText);
47+
}
48+
49+
@Override
50+
public WxCpIntelligentRobotChatResponse chat(WxCpIntelligentRobotChatRequest request) throws WxErrorException {
51+
String responseText = this.cpService.post(CHAT, request.toJson());
52+
return WxCpIntelligentRobotChatResponse.fromJson(responseText);
53+
}
54+
55+
@Override
56+
public void resetSession(String robotId, String userid, String sessionId) throws WxErrorException {
57+
JsonObject jsonObject = new JsonObject();
58+
jsonObject.addProperty("robot_id", robotId);
59+
jsonObject.addProperty("userid", userid);
60+
jsonObject.addProperty("session_id", sessionId);
61+
this.cpService.post(RESET_SESSION, jsonObject.toString());
62+
}
63+
64+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package me.chanjar.weixin.cp.bean.intelligentrobot;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
7+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* 智能机器人信息
13+
*
14+
* @author Binary Wang
15+
*/
16+
@Data
17+
@EqualsAndHashCode(callSuper = true)
18+
public class WxCpIntelligentRobot extends WxCpBaseResp implements Serializable {
19+
private static final long serialVersionUID = -1L;
20+
21+
/**
22+
* 机器人ID
23+
*/
24+
@SerializedName("robot_id")
25+
private String robotId;
26+
27+
/**
28+
* 机器人名称
29+
*/
30+
@SerializedName("name")
31+
private String name;
32+
33+
/**
34+
* 机器人描述
35+
*/
36+
@SerializedName("description")
37+
private String description;
38+
39+
/**
40+
* 机器人头像
41+
*/
42+
@SerializedName("avatar")
43+
private String avatar;
44+
45+
/**
46+
* 机器人状态 0:停用 1:启用
47+
*/
48+
@SerializedName("status")
49+
private Integer status;
50+
51+
/**
52+
* 创建时间
53+
*/
54+
@SerializedName("create_time")
55+
private Long createTime;
56+
57+
/**
58+
* 更新时间
59+
*/
60+
@SerializedName("update_time")
61+
private Long updateTime;
62+
63+
/**
64+
* From json wx cp intelligent robot.
65+
*
66+
* @param json the json
67+
* @return the wx cp intelligent robot
68+
*/
69+
public static WxCpIntelligentRobot fromJson(String json) {
70+
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobot.class);
71+
}
72+
73+
public String toJson() {
74+
return WxCpGsonBuilder.create().toJson(this);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)