用户:
一只鲨鱼吖查看:16 回复:7 评论:16 创建时间:2022-12-05T15:25:41
RequestMapping(value="/api",method=RequestMethod.POST)
public void initPostAPI(HttpServletRequest request, HttpServletResponse response)
{
String Event = "";
String MsgType = "";
String Content = "";
String strJson = new String();
// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 调用核心业务类接收消息、处理消息
User user = new User();
String respMessage = "";//反馈信息
Map<String, String> map = new HashMap<String, String>();
try {
map = parseXml(request);
} catch (Exception e) {
e.printStackTrace();
}
Event = map.get("Event");
MsgType = map.get("MsgType");
Content = map.get("Content");
System.out.println("Content = " + Content);
System.out.println("MsgType = " + MsgType);
System.out.println("Event = " + Event);
System.out.println("EventKey = " + map.get("EventKey"));
if(Event == null){//Event为null 则为用户输入信息被动回复
//自动回复用户信息
if(MsgType.equals("text")){
strJson = sendMess(map,strJson, Content);
}
}
SendUserMess.sendMess(strJson);
}
这是回复信息内容的处理(文本信息,图文信息)
public String sendMess(Map map,String strJson,String Content){
//根据用户输入的关键字查询对应响应
AutoReply autoReply = new AutoReply();
autoReply.setKeyName(Content);
List<AutoReply> list = this.replyService.findByKeyname(autoReply);
System.out.println("list size = "+list.size());
//向用户回复消息
if(list.size() != 0){
autoReply = list.get(0);
System.out.println("KeyValue-----"+autoReply.getKeyValue()+"--");
if(!autoReply.getKeyValue().equals("")){
strJson = "{\"touser\" :\""+map.get("FromUserName")+"\",";
strJson += "\"msgtype\":\"text\",";
strJson += "\"text\":{";
strJson += "\"content\":\""+autoReply.getKeyValue()+"\"";
strJson += "}}";
}else if(!autoReply.getGraphic().equals("")){
String newsinfo = "";
String[] strs = autoReply.getGraphic().split(",");
strJson = "{\"touser\" :\""+map.get("FromUserName")+"\",";
strJson += "\"msgtype\":\"news\",";
strJson += "\"news\":";
String news = "{\"articles\": [";
System.out.println(news);
for(String str:strs){
Graphic graphic = graphicService.loadByPK(Integer.valueOf(str));
String url = graphic.getPicurl();
//url = saveUrl + url.substring(24);
newsinfo += "{\"title\":\""+graphic.getTitle()+"\"," +
"\"description\":\""+graphic.getDescription()+"\",\"url\":\""+graphic.getUrl()+"\"," +
"\"picurl\":\""+url+"\""+
"},";
}
news = news + newsinfo.substring(0, (newsinfo.length()-1));
news = news + "]}";
strJson += news;
strJson += "}";
System.out.println("news-------"+news);
}
}else{//如果在数据库中找不到对应回复用户的信息则回复默认消息
autoReply.setKeyName("未知");
List<AutoReply> list_ = this.replyService.findByKeyname(autoReply);
autoReply = list_.get(0);
/*StringBuffer content = new StringBuffer();
content.append("感谢您!").append("\n");
content.append("再次感谢您").append("\n");
content.append("再次再次感谢您");*/
//拼装需要发送的消息
strJson = "{\"touser\" :\""+map.get("FromUserName")+"\",";
strJson += "\"msgtype\":\"text\",";
strJson += "\"text\":{";
strJson += "\"content\":\""+autoReply.getKeyValue()+"\"";
strJson += "}}";
}
————————————————
版权声明;是鲨鱼吖