Skip to content

Commit d957896

Browse files
authored
🎨 #3212【企业微信】增加微信客服回调事件支持
1 parent d0324c6 commit d957896

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ public static class EventType {
301301
public static final String CLICK = "CLICK";
302302
public static final String VIEW = "VIEW";
303303
public static final String MASS_SEND_JOB_FINISH = "MASSSENDJOBFINISH";
304+
305+
/**
306+
* 微信客服消息事件推送
307+
*/
308+
public static final String KF_MSG_OR_EVENT = "kf_msg_or_event";
304309
/**
305310
* 扫码推事件的事件推送
306311
*/

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ public class WxCpXmlMessage implements Serializable {
187187
@XStreamConverter(value = XStreamCDataConverter.class)
188188
private String taskId;
189189

190+
/**
191+
* 微信客服
192+
* 调用拉取消息接口时,需要传此token,用于校验请求的合法性
193+
*/
194+
@XStreamAlias("Token")
195+
@XStreamConverter(value = XStreamCDataConverter.class)
196+
private String token;
197+
198+
/**
199+
* 有新消息的客服账号。可通过sync_msg接口指定open_kfid获取此客服账号的消息
200+
*/
201+
@XStreamAlias("OpenKfId")
202+
@XStreamConverter(value = XStreamCDataConverter.class)
203+
private String openKfId;
204+
190205
/**
191206
* 通讯录变更事件.
192207
* 请参考常量 me.chanjar.weixin.cp.constant.WxCpConsts.ContactChangeType
@@ -222,6 +237,7 @@ public class WxCpXmlMessage implements Serializable {
222237
@XStreamAlias("WelcomeCode")
223238
@XStreamConverter(value = XStreamCDataConverter.class)
224239
private String welcomeCode;
240+
225241
/**
226242
* 新的UserID,变更时推送(userid由系统生成时可更改一次).
227243
*/

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpKfServiceImplTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@
33
import com.google.inject.Inject;
44
import me.chanjar.weixin.common.api.WxConsts;
55
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
6+
import me.chanjar.weixin.common.util.XmlUtils;
67
import me.chanjar.weixin.cp.api.ApiTestModule;
78
import me.chanjar.weixin.cp.api.WxCpService;
89
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
910
import me.chanjar.weixin.cp.bean.kf.*;
11+
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
12+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
13+
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
1014
import org.testng.annotations.Guice;
1115
import org.testng.annotations.Test;
1216

1317
import java.io.InputStream;
1418

1519
/**
1620
* WxCpKfServiceImpl-测试类
17-
* 需要用到专门的 secret https://kf.weixin.qq.com/api/doc/path/93304#secret
21+
* 需要用到专门的secret
22+
* <a href="https://developer.work.weixin.qq.com/document/path/94638">官方文档1</a>
23+
* <a href="https://kf.weixin.qq.com/api/doc/path/93304#secret">官方文档2</a>
1824
*
1925
* @author Fu created on 2022/1/19 20:12
2026
*/
@@ -97,4 +103,34 @@ public void testAccountDel() throws Exception {
97103
System.out.println(resp);
98104
}
99105

106+
/**
107+
* 测试回调事件
108+
* https://developer.work.weixin.qq.com/document/path/94670
109+
*
110+
* @throws Exception
111+
*/
112+
@Test(priority = 6)
113+
public void testEvent() throws Exception {
114+
115+
String xml = "<xml>\n" +
116+
" <ToUserName><![CDATA[ww12345678910]]></ToUserName>\n" +
117+
" <CreateTime>1348831860</CreateTime>\n" +
118+
" <MsgType><![CDATA[event]]></MsgType>\n" +
119+
" <Event><![CDATA[kf_msg_or_event]]></Event>\n" +
120+
" <Token><![CDATA[ENCApHxnGDNAVNY4AaSJKj4Tb5mwsEMzxhFmHVGcra996NR]]></Token>\n" +
121+
" <OpenKfId><![CDATA[wkxxxxxxx]]></OpenKfId>\n" +
122+
"</xml>";
123+
124+
WxCpXmlMessage xmlMsg = XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
125+
xmlMsg.setAllFieldsMap(XmlUtils.xml2Map(xml));
126+
System.out.println(WxCpGsonBuilder.create().toJson(xmlMsg));
127+
128+
/**
129+
* 微信客服事件推送
130+
* @see WxConsts.EventType.KF_MSG_OR_EVENT
131+
*/
132+
System.out.println("token:" + xmlMsg.getToken());
133+
System.out.println("openKfId:" + xmlMsg.getOpenKfId());
134+
}
135+
100136
}

0 commit comments

Comments
 (0)