JAVA 实现微信公众号用户回复关键字,系统回复多条信息给用户功能
上一遍完成了用户扫描带场景值的二维码后,当用户已经关注过公众号,系统会自动回复一条图文信息给用户(图文数量最多为8),当用户没有关注公众号,则先跳转到公众号关注页面,用户关注后系统推送一条图文信息给用户。
本文是接着上一篇的功能,完成当用户在公众号回复关键字后,系统回复多条信息给用户的功能。因为配置公众号的三个参数后,微信公众号的后台就不支持原有的自动回复功能,菜单配置功能了,需要系统接管相应的功能。
@RequestMapping("/handlePublicMsg")
@ApiOperation(value = "消息被动回复", httpMethod = "GET" )
@ResponseBody
public void handlePublicMsg(HttpServletRequest request, HttpServletResponse response)throws Exception {
logger.error("进入推送消息方法11111");
// 获得微信端返回的xml数据
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = response.getOutputStream();
try {
is = request.getInputStream();
isr = new InputStreamReader(is, "utf-8");
br = new BufferedReader(isr);
String str = null;
StringBuffer returnXml= new StringBuffer();
while ((str = br.readLine()) != null) {
//返回的是xml数据
returnXml.append(str);
}
Map encryptMap = WxPassiveReplyController.xmlToMap(returnXml.toString());
logger.error("接受消息:"+encryptMap.toString());
// 得到公众号传来的加密信息并解密,得到的是明文xml数据
//String decryptXml = WXPublicUtils.decrypt(encryptMap.get("Encrypt"));
// 将xml数据转换为map
/*logger.error(decryptXml.toString());
Map decryptMap = VatifyToken.xmlToMap(decryptXml.toString());*/
Map decryptMap = encryptMap;
// 区分消息类型
String msgType = decryptMap.get("MsgType");
//场景信息
String eventKey = decryptMap.get("EventKey");
logger.error("MsgType:"+msgType);
logger.error("EventKey:"+eventKey);
// 普通消息
if ("text".equals(msgType)) { // 文本消息
// todo 处理文本消息
PicMessage picMessage = new PicMessage();
picMessage.setCreateTime(new Date().getTime());
picMessage.setToUserName(decryptMap.get("FromUserName"));
picMessage.setFromUserName(decryptMap.get("ToUserName"));
picMessage.setMsgType("image");
picMessage.setMediaId("UW_fTdXZYdgXOmfg2k5RvIqJtM2g_FEn_ViNuugm6a4");
String mapToXml = getXmlString3(picMessage);
logger.error("发送数据:"+mapToXml);
os.write(mapToXml.getBytes());
CustomerServiceText text = new CustomerServiceText();
text.setTouser(decryptMap.get("FromUserName"));
text.setMsgtype("text");
Map text1 = new HashMap<>();
text1.put("content","你好,欢迎您的加入!");
text.setText(text1);
String json = JSONObject.toJSONString(text);
logger.error(json);
String token = getAccessToken("prod","prod");
String customerUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+token;
HttpConnectionUtil.AccessUrl(customerUrl,json);
CustomerServiceArticle a1 = new CustomerServiceArticle();
a1.setTitle("你好,欢迎您的加入!");
a1.setDescription("描述1");
a1.setPicurl("http://xxx//res//prod//SSEP/uploadfiles/knowledgeCourseImage/2020729407976775.png");
a1.setUrl("http://www.baidu.com");
CustomerServiceArticle a2 = new CustomerServiceArticle();
a2.setTitle("你好,欢迎您的致电!");
a2.setDescription("描述2");
a2.setPicurl("http://xxx//res//prod//SSEP/uploadfiles/knowledgeCourseImage/2020729407976775.png");
a2.setUrl("http://www.jd.com");
List list = new ArrayList<>();
list.add(a1);
//list.add(a2);
CustomerServiceImageText it = new CustomerServiceImageText();
it.setTouser(decryptMap.get("FromUserName"));
it.setMsgtype("news");
Map> map = new HashMap<>();
map.put("articles",list);
it.setNews(map);
String json2 = JSONObject.toJSONString(it);
logger.error(json2);
HttpConnectionUtil.AccessUrl(customerUrl,json2);
} else if ("image".equals(msgType)) { // 图片消息
// todo 处理图片消息
} else if ("voice".equals(msgType)) { //语音消息
// todo 处理语音消息
} else if ("video".equals(msgType)) { // 视频消息
// todo 处理视频消息
} else if ("shortvideo".equals(msgType)) { // 小视频消息
// todo 处理小视频消息
} else if ("location".equals(msgType)) { // 地理位置消息
// todo 处理地理位置消息
} else if ("link".equals(msgType)) { // 链接消息
// todo 处理链接消息
}
// 事件推送
else if ("event".equals(msgType)) { // 事件消息
// 区分事件推送
String event = decryptMap.get("Event");
logger.error("区分事件推送:"+event);
if ("subscribe".equals(event)) { // 订阅事件 或 未关注扫描二维码事件
String mapToXml = getReturnMessage2(decryptMap,eventKey);
logger.error("发送数据:"+mapToXml);
os.write(mapToXml.getBytes());
//return mapToXml;
} else if ("unsubscribe".equals(event)) { // 取消订阅事件
// todo 处理取消订阅事件
} else if ("SCAN".equals(event)) { // 已关注扫描二维码事件
String mapToXml = getReturnMessage1(decryptMap,eventKey);
logger.error("发送数据:"+mapToXml);
os.write(mapToXml.getBytes());
//return mapToXml;
} else if ("LOCATION".equals(event)) { // 上报地理位置事件
// todo 处理上报地理位置事件
} else if ("CLICK".equals(event)) { // 点击菜单拉取消息时的事件推送事件
// todo 处理点击菜单拉取消息时的事件推送事件
} else if ("VIEW".equals(event)) { // 点击菜单跳转链接时的事件推送
// todo 处理点击菜单跳转链接时的事件推送
}
}
} catch (Exception e) {
logger.error("处理微信公众号请求信息,失败", e);
} finally {
if (null != is) {
is.close();
}
if (null != isr) {
isr.close();
}
if (null != br) {
br.close();
}
}
os.write("".getBytes());
//return null;
}
上一遍中改方法返回的是字符串,这样就无法实现回复多条信息给用户,因此将返回类型修改为void,
使用
OutputStream os = response.getOutputStream();
os.write(mapToXml.getBytes());
这段代码来完成输出。
说明:
CustomerServiceText--封装文本信息的实体类
package com.xxx.controller.wxPassiveReply;
import com.alibaba.fastjson.JSONObject;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 微信客服文本消息
*/
public class CustomerServiceText implements Serializable {
/**
* 接受用户openid
*/
private String touser;
/**
* 消息类型
*/
private String msgtype;
private Map text;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public Map getText() {
return text;
}
public void setText(Map text) {
this.text = text;
}
public static void main(String[] args) {
CustomerServiceText text = new CustomerServiceText();
text.setTouser("qqq");
text.setMsgtype("text");
Map text1 = new HashMap<>();
text1.put("content","你好,欢迎您的加入!");
text.setText(text1);
String json = JSONObject.toJSONString(text);
System.out.print(json);
}
}
CustomerServiceImage--封装图片的实体类
package com.xxx.controller.wxPassiveReply;
import java.util.Map;
/**
* 微信客服图片消息
*/
public class CustomerServiceImage {
/**
* 接受用户openid
*/
private String touser;
/**
* 消息类型
*/
private String msgtype;
private Map image;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public Map getImage() {
return image;
}
public void setImage(Map image) {
this.image = image;
}
}
CustomerServiceImageText--封装图文的实体类
package com.xxx.controller.wxPassiveReply;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 微信客服图文消息
* @date 2020-8-4 09:22:08
* @version 1.0
*/
public class CustomerServiceImageText {
private String touser;
private String msgtype;
private Map> news;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public Map> getNews() {
return news;
}
public void setNews(Map> news) {
this.news = news;
}
public static void main(String[] args) {
CustomerServiceArticle a1 = new CustomerServiceArticle();
a1.setTitle("你好,欢迎您的加入!");
a1.setDescription("描述1");
a1.setPicurl("http://res.minxueedu.cn//res//prod//SSEP/uploadfiles/knowledgeCourseImage/2020729407976775.png");
a1.setUrl("http://www.baidu.com");
CustomerServiceArticle a2 = new CustomerServiceArticle();
a2.setTitle("你好,欢迎您的致电!");
a2.setDescription("描述2");
a2.setPicurl("http://res.minxueedu.cn//res//prod//SSEP/uploadfiles/knowledgeCourseImage/2020729407976775.png");
a2.setUrl("http://www.jd.com");
List list = new ArrayList<>();
list.add(a1);
list.add(a2);
CustomerServiceImageText it = new CustomerServiceImageText();
it.setTouser("11111");
it.setMsgtype("news");
Map> map = new HashMap<>();
map.put("articles",list);
it.setNews(map);
String json = JSONObject.toJSONString(it);
System.out.print(json);
}
}
CustomerServiceArticle--图文明细信息实体类
package com.xxx.controller.wxPassiveReply;
import java.util.Map;
/**
* 微信客服消息Article实体类
* @date 2020-8-4 09:22:08
* @version 1.0
*/
public class CustomerServiceArticle {
private String title;
private String description;
private String url;
private String picurl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
}
发送图片信息的时候需要先将图片上传到微信的素材中,有临时素材(三天有效期)和永久素材(图片100000上线)区分
以下是上传图片到微信后台素材库中的方法(部分代码是使用其他博主的代码)
@RequestMapping("/uploadFile1")
@ControllerLog(description = SysLog.UPLOAD_EXPLAIN_BREAK_EXAMPOINT_LOG)
public@ResponseBody ReturnInfo uploadFile1(HttpServletRequest req,
@RequestParam(value="file1",required=false) MultipartFile upfile,String pid){
logger.debug(" | Start !");
ReturnInfo returnInfo = new ReturnInfo();
HttpSession session=req.getSession();
String userAccnt=(String)session.getAttribute(Constants.USER_ACCNT);
if(userAccnt==null||"".equals(userAccnt)){
returnInfo.setRcode(CommonRcTable.ConnectionTimeIsOutError.getHexRcCode());
returnInfo.setRmessage(CommonRcTable.ConnectionTimeIsOutError.getRcMsg());
return returnInfo;
}
try {
List list = new ArrayList<>();
if(null != upfile) {
File file=null;
String fileName = upfile.getOriginalFilename();
//String id = seqBiz.getSeqLogId("SEQ_LOG_ID");
if(!fileName.isEmpty()){
file = new File(fileName);
upfile.transferTo(file);
String paltForm=PropertisUtil.getInstance().getPlatform();
TRPassiveReplyInfo loadPassiveReplyInfoById = trPassiveReplyInfoBiz.loadPassiveReplyInfoById(pid);
if(loadPassiveReplyInfoById != null) {
String accessToken = getAccessToken(paltForm, loadPassiveReplyInfoById.getSubPlatform());
String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?&type=image&access_token="+accessToken;
String result = fileMaterial(url, file);
JSONObject parseObject = JSONObject.parseObject(result);
String mediaId = (String)parseObject.get("media_id");
String url1 = (String)parseObject.get("url");;
if(mediaId != null && !"".equals(mediaId) && url1 != null && !"".equals(url1)) {
TRPassiveReplyInfoDetail cp = new TRPassiveReplyInfoDetail();
cp.setMediaId(mediaId);
cp.setUrl(url1);
list.add(cp);
}
}
//删除tomcat下的上传文件
file.delete();
}
}
returnInfo.setRows(list);
returnInfo.setRcode(CommonRcTable.RcOk.getHexRcCode());
returnInfo.setRmessage(CommonRcTable.RcOk.getRcMsg());
logger.debug(" | ReturnCode:"+CommonRcTable.RcOk.getHexRcCode()+",ReturnMsg:"+CommonRcTable.RcOk.getRcMsg());
} catch (FrameworkException fex) {
String rcCode=fex.getRcElementObj().getHexRcCode();
String rcMsg=fex.getRcElementObj().getRcMsg();
returnInfo.setRcode(rcCode);
returnInfo.setRmessage(rcMsg);
logger.error(" | ReturnCode:"+rcCode+",ReturnMsg:"+rcMsg);
} catch (Exception e) {
String rcCode=CommonRcTable.ControllerUploadCommunityPaperPdfFileError.getHexRcCode();
String rcMsg=CommonRcTable.ControllerUploadCommunityPaperPdfFileError.getRcMsg();
returnInfo.setRcode(rcCode);
returnInfo.setRmessage(rcMsg);
logger.error(" | ReturnCode:"+rcCode+",ReturnMsg:"+rcMsg);
logger.error(" | Exception:"+e.getMessage());
}
logger.debug(" | End !");
return returnInfo;
}
/**
* 上传图片素材到微信公众号平台
* @param path
* @param file
* @date 2020年7月31日 下午2:13:32
*/
public static String fileMaterial(String path, File file) throws IOException,NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
URL urlObj = new URL(path);
//连接
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
String result = null;
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type","multipart/form-data; boundary="+ BOUNDARY);
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\";filelength=\"" + file.length() + "\";filename=\""+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return result;
}
总结:
1.图片需要上传到微信公众号后台,调用上传素材接口(临时或永久),获取media_id;
2.调用微信的客服消息接口(注意:因为是用户发送的文本信息,因此图文信息列表只能包含一条信息)
实现效果: