Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【企业微信】增加微信客服回调事件支持 #3212

Merged
merged 6 commits into from
Jan 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ public static class EventType {
public static final String CLICK = "CLICK";
public static final String VIEW = "VIEW";
public static final String MASS_SEND_JOB_FINISH = "MASSSENDJOBFINISH";

/**
* 微信客服消息事件推送
*/
public static final String KF_MSG_OR_EVENT = "kf_msg_or_event";
/**
* 扫码推事件的事件推送
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ public class WxCpXmlMessage implements Serializable {
@XStreamConverter(value = XStreamCDataConverter.class)
private String taskId;

/**
* 微信客服
* 调用拉取消息接口时,需要传此token,用于校验请求的合法性
*/
@XStreamAlias("Token")
@XStreamConverter(value = XStreamCDataConverter.class)
private String token;

/**
* 有新消息的客服账号。可通过sync_msg接口指定open_kfid获取此客服账号的消息
*/
@XStreamAlias("OpenKfId")
@XStreamConverter(value = XStreamCDataConverter.class)
private String openKfId;

/**
* 通讯录变更事件.
* 请参考常量 me.chanjar.weixin.cp.constant.WxCpConsts.ContactChangeType
Expand Down Expand Up @@ -222,6 +237,7 @@ public class WxCpXmlMessage implements Serializable {
@XStreamAlias("WelcomeCode")
@XStreamConverter(value = XStreamCDataConverter.class)
private String welcomeCode;

/**
* 新的UserID,变更时推送(userid由系统生成时可更改一次).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.util.XmlUtils;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.kf.*;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.InputStream;

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

/**
* 测试回调事件
* https://developer.work.weixin.qq.com/document/path/94670
*
* @throws Exception
*/
@Test(priority = 6)
public void testEvent() throws Exception {

String xml = "<xml>\n" +
" <ToUserName><![CDATA[ww12345678910]]></ToUserName>\n" +
" <CreateTime>1348831860</CreateTime>\n" +
" <MsgType><![CDATA[event]]></MsgType>\n" +
" <Event><![CDATA[kf_msg_or_event]]></Event>\n" +
" <Token><![CDATA[ENCApHxnGDNAVNY4AaSJKj4Tb5mwsEMzxhFmHVGcra996NR]]></Token>\n" +
" <OpenKfId><![CDATA[wkxxxxxxx]]></OpenKfId>\n" +
"</xml>";

WxCpXmlMessage xmlMsg = XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
xmlMsg.setAllFieldsMap(XmlUtils.xml2Map(xml));
System.out.println(WxCpGsonBuilder.create().toJson(xmlMsg));

/**
* 微信客服事件推送
* @see WxConsts.EventType.KF_MSG_OR_EVENT
*/
System.out.println("token:" + xmlMsg.getToken());
System.out.println("openKfId:" + xmlMsg.getOpenKfId());
}

}