Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27de971

Browse files
Copilotbinarywang
andcommitted
Implement WeChat Intelligent Conversation support in MP module
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent dec2c18 commit 27de971

File tree

6 files changed

+222
-1
lines changed

6 files changed

+222
-1
lines changed

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpAiOpenService.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.File;
44

55
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.mp.bean.WxMpAiConversationRequest;
7+
import me.chanjar.weixin.mp.bean.WxMpAiConversationResponse;
68
import me.chanjar.weixin.mp.enums.AiLangType;
79

810
/**
@@ -76,4 +78,49 @@ public interface WxMpAiOpenService {
7678
* @throws WxErrorException the wx error exception
7779
*/
7880
String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException;
81+
82+
/**
83+
* <pre>
84+
* 微信智能对话.
85+
* 基于WeChat AI Speech平台的智能对话功能
86+
*
87+
* 文档地址:https://developers.weixin.qq.com/doc/aispeech/platform/INTRODUCTION.html
88+
* </pre>
89+
*
90+
* @param query 用户输入的对话内容
91+
* @param sessionId 会话ID,用于保持对话上下文
92+
* @return 智能对话回复内容
93+
* @throws WxErrorException the wx error exception
94+
*/
95+
String intelligentConversation(String query, String sessionId) throws WxErrorException;
96+
97+
/**
98+
* <pre>
99+
* 微信智能对话(带语言参数).
100+
* 基于WeChat AI Speech平台的智能对话功能
101+
*
102+
* 文档地址:https://developers.weixin.qq.com/doc/aispeech/platform/INTRODUCTION.html
103+
* </pre>
104+
*
105+
* @param query 用户输入的对话内容
106+
* @param sessionId 会话ID,用于保持对话上下文
107+
* @param lang 语言类型,默认中文
108+
* @return 智能对话回复内容
109+
* @throws WxErrorException the wx error exception
110+
*/
111+
String intelligentConversation(String query, String sessionId, AiLangType lang) throws WxErrorException;
112+
113+
/**
114+
* <pre>
115+
* 微信智能对话(使用请求对象).
116+
* 基于WeChat AI Speech平台的智能对话功能,支持更复杂的请求参数
117+
*
118+
* 文档地址:https://developers.weixin.qq.com/doc/aispeech/platform/INTRODUCTION.html
119+
* </pre>
120+
*
121+
* @param request 智能对话请求对象
122+
* @return 智能对话响应对象
123+
* @throws WxErrorException the wx error exception
124+
*/
125+
WxMpAiConversationResponse intelligentConversation(WxMpAiConversationRequest request) throws WxErrorException;
79126
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpAiOpenServiceImpl.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import com.google.gson.JsonObject;
34
import lombok.RequiredArgsConstructor;
45
import me.chanjar.weixin.common.enums.WxType;
56
import me.chanjar.weixin.common.error.WxError;
67
import me.chanjar.weixin.common.error.WxErrorException;
78
import me.chanjar.weixin.common.util.json.GsonParser;
89
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
910
import me.chanjar.weixin.mp.api.WxMpService;
11+
import me.chanjar.weixin.mp.bean.WxMpAiConversationRequest;
12+
import me.chanjar.weixin.mp.bean.WxMpAiConversationResponse;
1013
import me.chanjar.weixin.mp.enums.AiLangType;
1114
import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor;
1215

@@ -70,4 +73,59 @@ public String queryRecognitionResult(String voiceId, AiLangType lang) throws WxE
7073

7174
return GsonParser.parse(response).get("result").getAsString();
7275
}
76+
77+
@Override
78+
public String intelligentConversation(String query, String sessionId) throws WxErrorException {
79+
return this.intelligentConversation(query, sessionId, AiLangType.zh_CN);
80+
}
81+
82+
@Override
83+
public String intelligentConversation(String query, String sessionId, AiLangType lang) throws WxErrorException {
84+
if (lang == null) {
85+
lang = AiLangType.zh_CN;
86+
}
87+
88+
// 构建请求JSON
89+
JsonObject request = new JsonObject();
90+
request.addProperty("query", query);
91+
request.addProperty("session_id", sessionId);
92+
request.addProperty("lang", lang.getCode());
93+
94+
final String response = this.wxMpService.post(INTELLIGENT_CONVERSATION_URL.getUrl(this.wxMpService.getWxMpConfigStorage()),
95+
request.toString());
96+
97+
WxError error = WxError.fromJson(response, WxType.MP);
98+
if (error.getErrorCode() != 0) {
99+
throw new WxErrorException(error);
100+
}
101+
102+
return GsonParser.parse(response).get("reply").getAsString();
103+
}
104+
105+
@Override
106+
public WxMpAiConversationResponse intelligentConversation(WxMpAiConversationRequest request) throws WxErrorException {
107+
// 构建请求JSON
108+
JsonObject requestJson = new JsonObject();
109+
requestJson.addProperty("query", request.getQuery());
110+
requestJson.addProperty("session_id", request.getSessionId());
111+
requestJson.addProperty("lang", request.getLang() != null ? request.getLang().getCode() : AiLangType.zh_CN.getCode());
112+
113+
final String response = this.wxMpService.post(INTELLIGENT_CONVERSATION_URL.getUrl(this.wxMpService.getWxMpConfigStorage()),
114+
requestJson.toString());
115+
116+
WxError error = WxError.fromJson(response, WxType.MP);
117+
if (error.getErrorCode() != 0) {
118+
throw new WxErrorException(error);
119+
}
120+
121+
WxMpAiConversationResponse result = WxMpAiConversationResponse.fromJson(response);
122+
if (result.getReply() == null) {
123+
result.setReply(GsonParser.parse(response).get("reply").getAsString());
124+
}
125+
if (result.getSessionId() == null) {
126+
result.setSessionId(request.getSessionId());
127+
}
128+
129+
return result;
130+
}
73131
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import lombok.Data;
4+
import me.chanjar.weixin.mp.enums.AiLangType;
5+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* 微信智能对话请求对象
11+
*
12+
* @author Binary Wang
13+
*/
14+
@Data
15+
public class WxMpAiConversationRequest implements Serializable {
16+
private static final long serialVersionUID = 1L;
17+
18+
/**
19+
* 用户输入的对话内容
20+
*/
21+
private String query;
22+
23+
/**
24+
* 会话ID,用于保持对话上下文
25+
*/
26+
private String sessionId;
27+
28+
/**
29+
* 语言类型,默认中文
30+
*/
31+
private AiLangType lang = AiLangType.zh_CN;
32+
33+
public String toJson() {
34+
return WxMpGsonBuilder.create().toJson(this);
35+
}
36+
37+
public static WxMpAiConversationRequest fromJson(String json) {
38+
return WxMpGsonBuilder.create().fromJson(json, WxMpAiConversationRequest.class);
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import lombok.Data;
4+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* 微信智能对话响应对象
10+
*
11+
* @author Binary Wang
12+
*/
13+
@Data
14+
public class WxMpAiConversationResponse implements Serializable {
15+
private static final long serialVersionUID = 1L;
16+
17+
/**
18+
* 智能对话回复内容
19+
*/
20+
private String reply;
21+
22+
/**
23+
* 会话ID
24+
*/
25+
private String sessionId;
26+
27+
/**
28+
* 错误码
29+
*/
30+
private Integer errcode;
31+
32+
/**
33+
* 错误消息
34+
*/
35+
private String errmsg;
36+
37+
public String toJson() {
38+
return WxMpGsonBuilder.create().toJson(this);
39+
}
40+
41+
public static WxMpAiConversationResponse fromJson(String json) {
42+
return WxMpGsonBuilder.create().fromJson(json, WxMpAiConversationResponse.class);
43+
}
44+
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,11 @@ enum AiOpen implements WxMpApiUrl {
469469
/**
470470
* queryrecoresultfortext.
471471
*/
472-
VOICE_QUERY_RESULT_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/voice/queryrecoresultfortext");
472+
VOICE_QUERY_RESULT_URL(API_DEFAULT_HOST_URL, "/cgi-bin/media/voice/queryrecoresultfortext"),
473+
/**
474+
* 智能对话.
475+
*/
476+
INTELLIGENT_CONVERSATION_URL(API_DEFAULT_HOST_URL, "/cgi-bin/aispeech/conversation");
473477

474478
private final String prefix;
475479
private final String path;

‎weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpAiOpenServiceImplTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import me.chanjar.weixin.common.error.WxErrorException;
99
import me.chanjar.weixin.mp.api.WxMpService;
1010
import me.chanjar.weixin.mp.api.test.ApiTestModule;
11+
import me.chanjar.weixin.mp.bean.WxMpAiConversationRequest;
12+
import me.chanjar.weixin.mp.bean.WxMpAiConversationResponse;
1113
import me.chanjar.weixin.mp.enums.AiLangType;
1214

1315
import static org.assertj.core.api.Assertions.assertThat;
@@ -45,4 +47,30 @@ public void testTranslate() throws WxErrorException {
4547
final String result = this.wxService.getAiOpenService().translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
4648
assertThat(result).isNotEmpty();
4749
}
50+
51+
@Test
52+
public void testIntelligentConversation() throws WxErrorException {
53+
String sessionId = "test_session_" + System.currentTimeMillis();
54+
final String result = this.wxService.getAiOpenService().intelligentConversation("你好", sessionId);
55+
assertThat(result).isNotEmpty();
56+
}
57+
58+
@Test
59+
public void testIntelligentConversationWithLang() throws WxErrorException {
60+
String sessionId = "test_session_" + System.currentTimeMillis();
61+
final String result = this.wxService.getAiOpenService().intelligentConversation("你好,请介绍一下微信", sessionId, AiLangType.zh_CN);
62+
assertThat(result).isNotEmpty();
63+
}
64+
65+
@Test
66+
public void testIntelligentConversationWithRequest() throws WxErrorException {
67+
WxMpAiConversationRequest request = new WxMpAiConversationRequest();
68+
request.setQuery("微信智能对话功能怎么使用?");
69+
request.setSessionId("test_session_bean_" + System.currentTimeMillis());
70+
request.setLang(AiLangType.zh_CN);
71+
72+
final WxMpAiConversationResponse result = this.wxService.getAiOpenService().intelligentConversation(request);
73+
assertThat(result).isNotNull();
74+
assertThat(result.getReply()).isNotEmpty();
75+
}
4876
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /