Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions weixin-java-cp/INTELLIGENT_ROBOT.md
Original file line number Diff line number Diff line change
@@ -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表示启用
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,11 @@ public interface WxCpService extends WxService {
* @return
*/
WxCpCorpGroupService getCorpGroupService();

/**
* 获取智能机器人服务
*
* @return 智能机器人服务 intelligent robot service
*/
WxCpIntelligentRobotService getIntelligentRobotService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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的锁.
Expand Down Expand Up @@ -702,4 +703,9 @@ public WxCpMeetingService getMeetingService() {
public WxCpCorpGroupService getCorpGroupService() {
return corpGroupService;
}

@Override
public WxCpIntelligentRobotService getIntelligentRobotService() {
return this.intelligentRobotService;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Loading