微信订阅号之客服消息接口回复
前言
Github:https://github.com/HealerJean
1、微信客服发送文本消息
1.1、文本消息实体
@Data
public class CallBaseMessage {
private String touser; //openId 接收方帐号(收到的OpenID) ToUserName
private String msgtype; // 类型
}
@Data
public class CallTextMessage extends CallBaseMessage {
private TextContent text ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TextContent{
private String content;
}
}
1.2、发送接口
/**
* @Desc: 客服发送文本消息
{
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}
* @Date: 2018/5/30 上午11:36.
*/
@Override
public boolean sendCallTextMessage(String openId,String content,WeChatBusinessNo weChatBusinessNo) {
String access_toaken = AccessToakeUtil.getAccessToaken(weChatBusinessNo);
String url = WechatApiUrlParams.CALLPERSION_SEND_MESSAGE_URL.replace("ACCESS_TOKEN",access_toaken);
CallTextMessage callTextMessage = new CallTextMessage();
callTextMessage.setMsgtype(SystemParams.MESSAGE_TEXT);
callTextMessage.setTouser(openId);
callTextMessage.setText(new CallTextMessage().new TextContent(content) );
String paramJson = JSONObject.fromObject(callTextMessage).toString();
log.info("客服发送文本"+paramJson);
String resultJson = HttpHelper.handlePostJson(url,paramJson);
log.info(resultJson);
if(JSONObject.fromObject(resultJson).has("errmsg")){
if(StringUtils.equals("ok",JSONObject.fromObject(resultJson).getString("errmsg"))){
return true;
}
}
return false;
}
2、微信客服发送图片
2.1、实体类
@Data
public class CallImageMessage extends CallBaseMessage {
private ImageMediaId image ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ImageMediaId{
private String media_id;
}
}
2.2、实现类接口
/**
*微信客服发送图片
* @param openId
* @param filePathOrUrL
* @param type 2 url 1 文件路径
* @param weChatBusinessNo
* @return
* {
* "touser":"OPENID",
* "msgtype":"image",
* "image":
* {
* "media_id":"MEDIA_ID"
* }
* }
*/
@Override
public boolean sendCallImageMessage(String openId, String filePathOrUrL, Integer type, WeChatBusinessNo weChatBusinessNo) {
CallImageMessage imageMessage = new CallImageMessage();
imageMessage.setMsgtype(SystemParams.MESSAtGE_IMAGE);
imageMessage.setTouser(openId);
String mediaId = WeChatUploadUtil.getMedia(filePathOrUrL,type ,weChatBusinessNo.getId());
imageMessage.setImage(new CallImageMessage().new ImageMediaId(mediaId));
String paramJson = JSONObject.fromObject(imageMessage).toString();
log.info("客服发送image"+paramJson);
String access_toaken = AccessToakeUtil.getAccessToaken(weChatBusinessNo);
String url = WechatApiUrlParams.CALLPERSION_SEND_MESSAGE_URL.replace("ACCESS_TOKEN",access_toaken);
String resultJson = HttpHelper.handlePostJson(url,paramJson);
log.info(resultJson);
if(JSONObject.fromObject(resultJson).has("errmsg")){
if(StringUtils.equals("ok",JSONObject.fromObject(resultJson).getString("errmsg"))){
return true;
}
}
return false;
}
3、微信客服发送视频
3.1、实体类
/**
* @Desc:
* @Author HealerJean
* @Date 2018/5/30 上午11:13.
*/
@Data
public class CallVideoMessage extends CallBaseMessage {
private VideoMediaId video ;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class VideoMediaId{
private String media_id;
private String thumb_media_id;
private String title;
private String description;
}
}
3.2、发送视频方法
/**
* 微信客服发送视频
* type 1 文件路径
* @return
*/
@Override
public boolean sendCallVideoMessage(String openId, String filePathOrUrL, Integer type, String title ,String description, WeChatBusinessNo weChatBusinessNo) {
CallVideoMessage videoMessage = new CallVideoMessage();
videoMessage.setMsgtype(SystemParams.MESSAGE_VIDEO);
videoMessage.setTouser(openId);
String mediaId = WeChatUploadUtil.getMedia(filePathOrUrL,type ,weChatBusinessNo.getId());
videoMessage.setVideo(new CallVideoMessage().new VideoMediaId().setMedia_id(mediaId).setThumb_media_id(mediaId).setTitle(title).setDescription(description));
String paramJson = JSONObject.fromObject(videoMessage).toString();
log.info("客服发送video"+paramJson);
String access_toaken = AccessToakeUtil.getAccessToaken(weChatBusinessNo);
String url = WechatApiUrlParams.CALLPERSION_SEND_MESSAGE_URL.replace("ACCESS_TOKEN",access_toaken);
String resultJson = HttpHelper.handlePostJson(url,paramJson);
log.info(resultJson);
if(JSONObject.fromObject(resultJson).has("errmsg")){
if(StringUtils.equals("ok",JSONObject.fromObject(resultJson).getString("errmsg"))){
return true;
}
}
return false;
}