Skip to content

Commit c040ba3

Browse files
committed
增加查看客服聊天记录的接口 #5
1 parent 0c723bb commit c040ba3

File tree

6 files changed

+220
-32
lines changed

6 files changed

+220
-32
lines changed

weixin-java-mp/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<groupId>org.eclipse.jetty</groupId>
4848
<artifactId>jetty-servlet</artifactId>
4949
<scope>test</scope>
50+
</dependency><!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
51+
<dependency>
52+
<groupId>joda-time</groupId>
53+
<artifactId>joda-time</artifactId>
54+
<version>2.9.4</version>
55+
<scope>test</scope>
5056
</dependency>
5157
</dependencies>
5258

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package me.chanjar.weixin.mp.api;
22

33
import java.io.File;
4+
import java.util.Date;
45

56
import me.chanjar.weixin.common.exception.WxErrorException;
67
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
7-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
8-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList;
9-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult;
10-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList;
11-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList;
8+
import me.chanjar.weixin.mp.bean.kefu.result.*;
129

1310
/**
1411
* 客服接口 ,
@@ -135,4 +132,23 @@ boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
135132
* </pre>
136133
*/
137134
WxMpKfSessionWaitCaseList kfSessionGetWaitCase() throws WxErrorException;
135+
136+
//*******************获取聊天记录的接口***********************//
137+
/**
138+
* <pre>
139+
* 获取聊天记录
140+
* 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
141+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a>
142+
* 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
143+
* </pre>
144+
*
145+
* @param startTime 起始时间
146+
* @param endTime 结束时间
147+
* @param msgId 消息id顺序从小到大,从1开始
148+
* @param number 每次获取条数,最多10000条
149+
* @return 聊天记录对象
150+
* @throws WxErrorException
151+
*/
152+
WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException;
153+
138154
}
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.mp.api.impl;
22

33
import java.io.File;
4+
import java.util.Date;
45

6+
import com.google.gson.JsonObject;
7+
import me.chanjar.weixin.common.bean.result.WxError;
58
import me.chanjar.weixin.common.exception.WxErrorException;
69
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
710
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
@@ -10,18 +13,15 @@
1013
import me.chanjar.weixin.mp.api.WxMpService;
1114
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
1215
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
13-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
14-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList;
15-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult;
16-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList;
17-
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList;
16+
import me.chanjar.weixin.mp.bean.kefu.result.*;
1817

1918
/**
2019
*
2120
* @author Binary Wang
2221
*
2322
*/
2423
public class WxMpKefuServiceImpl implements WxMpKefuService {
24+
public static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice";
2525
private WxMpService wxMpService;
2626

2727
public WxMpKefuServiceImpl(WxMpService wxMpService) {
@@ -30,15 +30,15 @@ public WxMpKefuServiceImpl(WxMpService wxMpService) {
3030

3131
@Override
3232
public WxMpKfList kfList() throws WxErrorException {
33-
String url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist";
33+
String url = API_URL_PREFIX + "/getkflist";
3434
String responseContent = this.wxMpService
3535
.execute(new SimpleGetRequestExecutor(), url, null);
3636
return WxMpKfList.fromJson(responseContent);
3737
}
3838

3939
@Override
4040
public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
41-
String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist";
41+
String url = API_URL_PREFIX + "/getonlinekflist";
4242
String responseContent = this.wxMpService
4343
.execute(new SimpleGetRequestExecutor(), url, null);
4444
return WxMpKfOnlineList.fromJson(responseContent);
@@ -47,7 +47,7 @@ public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
4747
@Override
4848
public boolean kfAccountAdd(WxMpKfAccountRequest request)
4949
throws WxErrorException {
50-
String url = "https://api.weixin.qq.com/customservice/kfaccount/add";
50+
String url = API_URL_PREFIX + "/kfaccount/add";
5151
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
5252
request.toJson());
5353
return true;
@@ -56,15 +56,15 @@ public boolean kfAccountAdd(WxMpKfAccountRequest request)
5656
@Override
5757
public boolean kfAccountUpdate(WxMpKfAccountRequest request)
5858
throws WxErrorException {
59-
String url = "https://api.weixin.qq.com/customservice/kfaccount/update";
59+
String url = API_URL_PREFIX + "/kfaccount/update";
6060
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
6161
request.toJson());
6262
return true;
6363
}
6464

6565
@Override
6666
public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException {
67-
String url = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker";
67+
String url = API_URL_PREFIX + "/kfaccount/inviteworker";
6868
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
6969
request.toJson());
7070
return true;
@@ -73,16 +73,14 @@ public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErro
7373
@Override
7474
public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
7575
throws WxErrorException {
76-
String url = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account="
77-
+ kfAccount;
76+
String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount;
7877
this.wxMpService.execute(new MediaUploadRequestExecutor(), url, imgFile);
7978
return true;
8079
}
8180

8281
@Override
8382
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
84-
String url = "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account="
85-
+ kfAccount;
83+
String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount;
8684
this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
8785
return true;
8886
}
@@ -91,7 +89,7 @@ public boolean kfAccountDel(String kfAccount) throws WxErrorException {
9189
public boolean kfSessionCreate(String openid, String kfAccount)
9290
throws WxErrorException {
9391
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
94-
String url = "https://api.weixin.qq.com/customservice/kfsession/create";
92+
String url = API_URL_PREFIX + "/kfsession/create";
9593
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
9694
request.toJson());
9795
return true;
@@ -101,7 +99,7 @@ public boolean kfSessionCreate(String openid, String kfAccount)
10199
public boolean kfSessionClose(String openid, String kfAccount)
102100
throws WxErrorException {
103101
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
104-
String url = "https://api.weixin.qq.com/customservice/kfsession/close";
102+
String url = API_URL_PREFIX + "/kfsession/close";
105103
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
106104
request.toJson());
107105
return true;
@@ -110,8 +108,7 @@ public boolean kfSessionClose(String openid, String kfAccount)
110108
@Override
111109
public WxMpKfSessionGetResult kfSessionGet(String openid)
112110
throws WxErrorException {
113-
String url = "https://api.weixin.qq.com/customservice/kfsession/getsession?openid="
114-
+ openid;
111+
String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid;
115112
String responseContent = this.wxMpService
116113
.execute(new SimpleGetRequestExecutor(), url, null);
117114
return WxMpKfSessionGetResult.fromJson(responseContent);
@@ -120,8 +117,7 @@ public WxMpKfSessionGetResult kfSessionGet(String openid)
120117
@Override
121118
public WxMpKfSessionList kfSessionList(String kfAccount)
122119
throws WxErrorException {
123-
String url = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account="
124-
+ kfAccount;
120+
String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount;
125121
String responseContent = this.wxMpService
126122
.execute(new SimpleGetRequestExecutor(), url, null);
127123
return WxMpKfSessionList.fromJson(responseContent);
@@ -130,10 +126,28 @@ public WxMpKfSessionList kfSessionList(String kfAccount)
130126
@Override
131127
public WxMpKfSessionWaitCaseList kfSessionGetWaitCase()
132128
throws WxErrorException {
133-
String url = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase";
129+
String url = API_URL_PREFIX + "/kfsession/getwaitcase";
134130
String responseContent = this.wxMpService
135131
.execute(new SimpleGetRequestExecutor(), url, null);
136132
return WxMpKfSessionWaitCaseList.fromJson(responseContent);
137133
}
138134

135+
@Override
136+
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException {
137+
if(startTime.after(endTime)){
138+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build());
139+
}
140+
141+
String url = API_URL_PREFIX + "/msgrecord/getmsglist";
142+
143+
JsonObject param = new JsonObject();
144+
param.addProperty("starttime", startTime.getTime() / 1000); //starttime 起始时间,unix时间戳
145+
param.addProperty("endtime", endTime.getTime() / 1000); //endtime 结束时间,unix时间戳,每次查询时段不能超过24小时
146+
param.addProperty("msgid", msgId); //msgid 消息id顺序从小到大,从1开始
147+
param.addProperty("number", number); //number 每次获取条数,最多10000条
148+
149+
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, param.toString());
150+
return WxMpKfMsgList.fromJson(responseContent);
151+
}
152+
139153
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package me.chanjar.weixin.mp.bean.kefu.result;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Created by Binary Wang on 2016/7/15.
12+
*/
13+
public class WxMpKfMsgList {
14+
@SerializedName("recordlist")
15+
private List<WxMpKfMsgRecord> records;
16+
17+
@SerializedName("number")
18+
private Integer number;
19+
20+
@SerializedName("msgid")
21+
private Long msgId;
22+
23+
public List<WxMpKfMsgRecord> getRecords() {
24+
return records;
25+
}
26+
27+
public void setRecords(List<WxMpKfMsgRecord> records) {
28+
this.records = records;
29+
}
30+
31+
public Integer getNumber() {
32+
return number;
33+
}
34+
35+
public void setNumber(Integer number) {
36+
this.number = number;
37+
}
38+
39+
public Long getMsgId() {
40+
return msgId;
41+
}
42+
43+
public void setMsgId(Long msgId) {
44+
this.msgId = msgId;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
50+
}
51+
52+
public static WxMpKfMsgList fromJson(String responseContent) {
53+
return WxMpGsonBuilder.INSTANCE.create().fromJson(responseContent, WxMpKfMsgList.class);
54+
}
55+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package me.chanjar.weixin.mp.bean.kefu.result;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
4+
import com.google.gson.annotations.SerializedName;
5+
import me.chanjar.weixin.mp.util.json.WxLongTimeJsonSerializer;
6+
import org.apache.commons.lang3.builder.ToStringBuilder;
7+
import org.apache.commons.lang3.builder.ToStringStyle;
8+
9+
/**
10+
* Created by Binary Wang on 2016/7/18.
11+
*/
12+
public class WxMpKfMsgRecord {
13+
/**
14+
* worker 完整客服帐号,格式为:帐号前缀@公众号微信号
15+
*/
16+
@SerializedName("worker")
17+
private String worker;
18+
19+
/**
20+
* openid 用户标识
21+
*/
22+
@SerializedName("openid")
23+
private String openid;
24+
25+
/**
26+
* opercode 操作码,2002(客服发送信息),2003(客服接收消息)
27+
*/
28+
@SerializedName("opercode")
29+
private Integer operateCode;
30+
31+
/**
32+
* text 聊天记录
33+
*/
34+
@SerializedName("text")
35+
private String text;
36+
37+
/**
38+
* time 操作时间,unix时间戳
39+
*/
40+
@SerializedName("time")
41+
@JsonSerialize(using = WxLongTimeJsonSerializer.class)
42+
private Long time;
43+
44+
@Override
45+
public String toString() {
46+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
47+
}
48+
public String getWorker() {
49+
return worker;
50+
}
51+
52+
public void setWorker(String worker) {
53+
this.worker = worker;
54+
}
55+
56+
public String getOpenid() {
57+
return openid;
58+
}
59+
60+
public void setOpenid(String openid) {
61+
this.openid = openid;
62+
}
63+
64+
public String getText() {
65+
return text;
66+
}
67+
68+
public void setText(String text) {
69+
this.text = text;
70+
}
71+
72+
public Long getTime() {
73+
return time;
74+
}
75+
76+
public void setTime(Long time) {
77+
this.time = time;
78+
}
79+
80+
public Integer getOperateCode() {
81+
return operateCode;
82+
}
83+
84+
public void setOperateCode(Integer operateCode) {
85+
this.operateCode = operateCode;
86+
}
87+
}

0 commit comments

Comments
 (0)