Skip to content

Commit 9341727

Browse files
Copilotbinarywang
andauthored
支持企业微信智能机器人JSON回调消息解析并补充测试
Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/07e83fbe-cc11-4b67-82ec-6cb21fb184a3 Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 87fd7b5 commit 9341727

3 files changed

Lines changed: 282 additions & 1 deletion

File tree

weixin-java-cp/INTELLIGENT_ROBOT.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ String fromUser = message.getFromUserName(); // 发送用户
109109
// ...
110110
```
111111

112+
对于智能机器人 API 模式的 JSON 回调消息,可使用 `WxCpIntelligentRobotMessage` 解析:
113+
114+
```java
115+
WxCpIntelligentRobotMessage callbackMessage = WxCpIntelligentRobotMessage.fromJson(jsonBody);
116+
String botId = callbackMessage.getAiBotId();
117+
String userId = callbackMessage.getFrom().getUserid();
118+
String msgType = callbackMessage.getMsgType();
119+
```
120+
112121
### 删除智能机器人
113122

114123
```java
@@ -146,4 +155,4 @@ robotService.deleteRobot(robotId);
146155
1. 需要确保企业微信应用具有智能机器人相关权限
147156
2. 智能机器人功能可能需要特定的企业微信版本支持
148157
3. 会话ID可以用于保持对话的连续性,提升用户体验
149-
4. 机器人状态: 0表示停用,1表示启用
158+
4. 机器人状态: 0表示停用,1表示启用
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package me.chanjar.weixin.cp.bean.intelligentrobot;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
/**
11+
* 企业微信智能机器人回调消息.
12+
*
13+
* <p>官方文档: https://developer.work.weixin.qq.com/document/path/100719</p>
14+
*/
15+
@Data
16+
public class WxCpIntelligentRobotMessage implements Serializable {
17+
private static final long serialVersionUID = -1L;
18+
19+
/**
20+
* 本次回调的唯一性标志.
21+
*/
22+
@SerializedName("msgid")
23+
private String msgId;
24+
25+
/**
26+
* 智能机器人id.
27+
*/
28+
@SerializedName("aibotid")
29+
private String aiBotId;
30+
31+
/**
32+
* 会话id,仅群聊类型时返回.
33+
*/
34+
@SerializedName("chatid")
35+
private String chatId;
36+
37+
/**
38+
* 会话类型,single/group.
39+
*/
40+
@SerializedName("chattype")
41+
private String chatType;
42+
43+
/**
44+
* 消息发送者.
45+
*/
46+
@SerializedName("from")
47+
private From from;
48+
49+
/**
50+
* 支持主动回复消息的临时url.
51+
*/
52+
@SerializedName("response_url")
53+
private String responseUrl;
54+
55+
/**
56+
* 消息类型.
57+
*/
58+
@SerializedName("msgtype")
59+
private String msgType;
60+
61+
@SerializedName("text")
62+
private Text text;
63+
64+
@SerializedName("image")
65+
private Image image;
66+
67+
@SerializedName("mixed")
68+
private Mixed mixed;
69+
70+
@SerializedName("voice")
71+
private Voice voice;
72+
73+
@SerializedName("file")
74+
private FileInfo file;
75+
76+
@SerializedName("video")
77+
private Video video;
78+
79+
@SerializedName("quote")
80+
private Quote quote;
81+
82+
@SerializedName("stream")
83+
private Stream stream;
84+
85+
public static WxCpIntelligentRobotMessage fromJson(String json) {
86+
return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotMessage.class);
87+
}
88+
89+
public String toJson() {
90+
return WxCpGsonBuilder.create().toJson(this);
91+
}
92+
93+
@Data
94+
public static class From implements Serializable {
95+
private static final long serialVersionUID = -1L;
96+
97+
@SerializedName("userid")
98+
private String userid;
99+
}
100+
101+
@Data
102+
public static class Text implements Serializable {
103+
private static final long serialVersionUID = -1L;
104+
105+
@SerializedName("content")
106+
private String content;
107+
}
108+
109+
@Data
110+
public static class Image implements Serializable {
111+
private static final long serialVersionUID = -1L;
112+
113+
@SerializedName("url")
114+
private String url;
115+
}
116+
117+
@Data
118+
public static class Voice implements Serializable {
119+
private static final long serialVersionUID = -1L;
120+
121+
@SerializedName("content")
122+
private String content;
123+
}
124+
125+
@Data
126+
public static class FileInfo implements Serializable {
127+
private static final long serialVersionUID = -1L;
128+
129+
@SerializedName("url")
130+
private String url;
131+
}
132+
133+
@Data
134+
public static class Video implements Serializable {
135+
private static final long serialVersionUID = -1L;
136+
137+
@SerializedName("url")
138+
private String url;
139+
}
140+
141+
@Data
142+
public static class Stream implements Serializable {
143+
private static final long serialVersionUID = -1L;
144+
145+
@SerializedName("id")
146+
private String id;
147+
}
148+
149+
@Data
150+
public static class Mixed implements Serializable {
151+
private static final long serialVersionUID = -1L;
152+
153+
@SerializedName("msg_item")
154+
private List<MixedItem> msgItem;
155+
}
156+
157+
@Data
158+
public static class MixedItem implements Serializable {
159+
private static final long serialVersionUID = -1L;
160+
161+
@SerializedName("msgtype")
162+
private String msgType;
163+
164+
@SerializedName("text")
165+
private Text text;
166+
167+
@SerializedName("image")
168+
private Image image;
169+
}
170+
171+
@Data
172+
public static class Quote implements Serializable {
173+
private static final long serialVersionUID = -1L;
174+
175+
@SerializedName("msgtype")
176+
private String msgType;
177+
178+
@SerializedName("text")
179+
private Text text;
180+
181+
@SerializedName("image")
182+
private Image image;
183+
184+
@SerializedName("mixed")
185+
private Mixed mixed;
186+
187+
@SerializedName("voice")
188+
private Voice voice;
189+
190+
@SerializedName("file")
191+
private FileInfo file;
192+
193+
@SerializedName("video")
194+
private Video video;
195+
}
196+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package me.chanjar.weixin.cp.bean.intelligentrobot;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.testng.Assert.assertEquals;
6+
import static org.testng.Assert.assertNotNull;
7+
import static org.testng.Assert.assertNull;
8+
9+
/**
10+
* 智能机器人回调消息测试.
11+
*/
12+
public class WxCpIntelligentRobotMessageTest {
13+
14+
@Test
15+
public void testFromJsonWithTextMessage() {
16+
String json = "{"
17+
+ "\"msgid\":\"msg_1\","
18+
+ "\"aibotid\":\"bot_1\","
19+
+ "\"chatid\":\"chat_1\","
20+
+ "\"chattype\":\"group\","
21+
+ "\"from\":{\"userid\":\"zhangsan\"},"
22+
+ "\"response_url\":\"https://example.com/reply\","
23+
+ "\"msgtype\":\"text\","
24+
+ "\"text\":{\"content\":\"@robot hello\"}"
25+
+ "}";
26+
27+
WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json);
28+
assertEquals(message.getMsgId(), "msg_1");
29+
assertEquals(message.getAiBotId(), "bot_1");
30+
assertEquals(message.getChatId(), "chat_1");
31+
assertEquals(message.getChatType(), "group");
32+
assertNotNull(message.getFrom());
33+
assertEquals(message.getFrom().getUserid(), "zhangsan");
34+
assertEquals(message.getResponseUrl(), "https://example.com/reply");
35+
assertEquals(message.getMsgType(), "text");
36+
assertNotNull(message.getText());
37+
assertEquals(message.getText().getContent(), "@robot hello");
38+
assertNull(message.getMixed());
39+
assertNull(message.getStream());
40+
}
41+
42+
@Test
43+
public void testFromJsonWithMixedAndQuote() {
44+
String json = "{"
45+
+ "\"msgid\":\"msg_2\","
46+
+ "\"aibotid\":\"bot_2\","
47+
+ "\"chattype\":\"single\","
48+
+ "\"from\":{\"userid\":\"lisi\"},"
49+
+ "\"msgtype\":\"mixed\","
50+
+ "\"mixed\":{\"msg_item\":["
51+
+ "{\"msgtype\":\"text\",\"text\":{\"content\":\"hello\"}},"
52+
+ "{\"msgtype\":\"image\",\"image\":{\"url\":\"https://example.com/1.png\"}}"
53+
+ "]},"
54+
+ "\"quote\":{\"msgtype\":\"text\",\"text\":{\"content\":\"quoted\"}}"
55+
+ "}";
56+
57+
WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json);
58+
assertEquals(message.getMsgType(), "mixed");
59+
assertNotNull(message.getMixed());
60+
assertNotNull(message.getMixed().getMsgItem());
61+
assertEquals(message.getMixed().getMsgItem().size(), 2);
62+
assertEquals(message.getMixed().getMsgItem().get(0).getMsgType(), "text");
63+
assertEquals(message.getMixed().getMsgItem().get(0).getText().getContent(), "hello");
64+
assertEquals(message.getMixed().getMsgItem().get(1).getMsgType(), "image");
65+
assertEquals(message.getMixed().getMsgItem().get(1).getImage().getUrl(), "https://example.com/1.png");
66+
assertNotNull(message.getQuote());
67+
assertEquals(message.getQuote().getMsgType(), "text");
68+
assertEquals(message.getQuote().getText().getContent(), "quoted");
69+
70+
String serialized = message.toJson();
71+
WxCpIntelligentRobotMessage deserialized = WxCpIntelligentRobotMessage.fromJson(serialized);
72+
assertEquals(deserialized.getAiBotId(), "bot_2");
73+
assertEquals(deserialized.getFrom().getUserid(), "lisi");
74+
assertEquals(deserialized.getMixed().getMsgItem().size(), 2);
75+
}
76+
}

0 commit comments

Comments
 (0)