大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
微信自动回复机器人原理很简单,获取用户发送到后台的数据,然后调用机器人的api进行回复即可。我使用的茉莉机器人,官网地址如下:
http://www.itpk.cn/
茉莉机器人的api:http://www.itpk.cn/robot.php
接口比较简单,看看就会,我直接上源代码
下面代码同样基于之前的微博,需要的可以去看一下:
http://blog.csdn.net/zxxz5201314/article/details/76917107
首先对用户发送的文本进行处理,特殊字段分别处理,同样Post请求:
@SuppressWarnings("rawtypes")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter writer = resp.getWriter();
try {
Map<String, String> map = MessageUtil.xmlToMap(req);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
System.out.println(toUserName+fromUserName+msgType+content);
String text = null;
if ("text".equals(msgType)) {
switch (content) {
case "笑话":
Map maps1 = (Map)JSON.parse(ITPK.itpk("笑话"));
StringBuffer sb1 = new StringBuffer();
sb1.append("标题:"+maps1.get("title")+"\n");
sb1.append("内容:"+maps1.get("content"));
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, sb1.toString());
break;
case "观音灵签":
Map maps2 = (Map)JSON.parse(ITPK.itpk("观音灵签"));
StringBuffer sb2 = new StringBuffer();
sb2.append("您抽取的是第"+maps2.get("number2")+"签\n");
sb2.append("签位:"+maps2.get("haohua")+"\n");
sb2.append("签语:"+maps2.get("qianyu")+"\n");
sb2.append("诗意:"+maps2.get("shiyi")+"\n");
sb2.append("解签:"+maps2.get("jieqian"));
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, sb2.toString());
break;
case "月老灵签":
Map maps3 = (Map)JSON.parse(ITPK.itpk("月老灵签"));
StringBuffer sb3 = new StringBuffer();
sb3.append("您抽取的是第"+maps3.get("number2")+"签\n");
sb3.append("签位:"+maps3.get("haohua")+"\n");
sb3.append("签语:"+maps3.get("shiyi")+"\n");
sb3.append("解签:"+maps3.get("jieqian"));
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, sb3.toString());
break;
case "财神爷灵签":
Map maps4 = (Map)JSON.parse(ITPK.itpk("财神爷灵签"));
StringBuffer sb4 = new StringBuffer();
sb4.append("您抽取的是第"+maps4.get("number2")+"签\n");
sb4.append("签语:"+maps4.get("qianyu")+"\n");
sb4.append("注释:"+maps4.get("zhushi")+"\n");
sb4.append("解签:"+maps4.get("jieqian")+"\n");
sb4.append("解说:"+maps4.get("jieshuo")+"\n");
sb4.append("婚姻:"+maps4.get("hunyin")+"\n");
sb4.append("事业:"+maps4.get("shiye")+"\n");
sb4.append("求财:"+maps4.get("qiucai"));
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, sb4.toString());
break;
case "指令":
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, MessageUtil.mainMenu());
break;
default:
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, ITPK.itpk(content));
break;
}
}else if (MessageUtil.Message_Event.equals(msgType)) {
if ("subscribe".equals(map.get("Event"))) {
text = MessageUtil.replyByKeyWord(toUserName, fromUserName, MessageUtil.Message_Text, MessageUtil.mainMenu());
}
}
System.out.println(text);
writer.print(text);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
writer.close();
}
}
茉莉机器人url组装类:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class ITPK {
public static String api_key = "api_key";
public static String api_secret = "api_secret";
public static String itpk(String path){
try {
path = URLEncoder.encode(path, "utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = "http://i.itpk.cn/api.php?question="+path+"&api_key="+api_key+"&api_secret="+api_secret;
return LordJson.loadJSON(url);
}
}
json处理类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class LordJson {
public static String loadJSON(String url) {
System.out.println(url);
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(),"utf-8"));//防止乱码
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
}
MessageUtil类:
public class MessageUtil {
public static String Message_Text = "text";
public static String Message_News = "news";
public static String Message_Image = "image";
public static String Message_Voice = "voice";
public static String Message_Video = "video";
public static String Message_ShortVideo = "shortvideo";
public static String Message_Location = "location";
public static String Message_Link = "link";
public static String Message_Event= "event";
public static String Message_Event_Sub= "subscribe";
/** * xml转map集合 * @param request * @return * @throws IOException * @throws DocumentException */
public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException{
Map<String, String> map = new HashMap<String,String>();
SAXReader reader = new SAXReader();
InputStream is = request.getInputStream();
Document document = reader.read(is);
Element root = document.getRootElement();
List<Element> list = root.elements();
for (Element e:list) {
map.put(e.getName(), e.getText());
}
is.close();
return map;
}
/** * 将文本消息转换成xml * @param textMessage * @return */
public static String textMessageToXml(TextMessage textMessage){
XStream xStream = new XStream();
xStream.alias("xml", textMessage.getClass());
return xStream.toXML(textMessage);
}
/** * 将图片信息转换成xml * @param imageMessage * @return */
public static String imageMessageToXml(ImageMessage imageMessage){
XStream xStream = new XStream();
xStream.alias("xml", imageMessage.getClass());
return xStream.toXML(imageMessage);
}
/** *将图文消息转换成xml * @param newsMessage * @return */
public static String newsMessageToXml(NewsMessage newsMessage){
XStream xStream = new XStream();
xStream.alias("xml", newsMessage.getClass());
xStream.alias("item", new News().getClass());
return xStream.toXML(newsMessage);
}
public static String repluByImage(String toUserName,String fromUserName,String msgType,String mediaId){
ImageMessage imageMessage = new ImageMessage();
imageMessage.setToUserName(fromUserName);
imageMessage.setFromUserName(toUserName);
imageMessage.setCreateTime(new Date().getTime());
Image image = new Image();
image.setMediaId(mediaId);
imageMessage.setImage(image);
return MessageUtil.imageMessageToXml(imageMessage);
}
/** * 文本回复 * @param toUserName * @param fromUserName * @param msgType * @param content * @return */
public static String replyByKeyWord(String toUserName,String fromUserName,String msgType,String content){
TextMessage message = new TextMessage();
message.setFromUserName(toUserName);
message.setToUserName(fromUserName);
message.setMsgType(msgType);
message.setCreateTime(new Date().getTime());
message.setContent(content);
return MessageUtil.textMessageToXml(message);
}
public static String mainMenu(){
StringBuffer sb = new StringBuffer();
sb.append("指令:\n");
sb.append("地区天气 \n");
sb.append("天气\n");
sb.append("@tq您的ip地址\n");
sb.append("@ip您的ip地址\n");
sb.append("ip\n");
sb.append("@qq要查询的QQ号码\n");
sb.append("@lol英雄联盟大区--角色名称\n");
sb.append("@sfz身份证号码\n");
sb.append("@sjh手机号码\n");
sb.append("@cy四字成语\n");
sb.append("笑话\n");
sb.append("观音灵签\n");
sb.append("月老灵签\n");
sb.append("财神爷灵签");
return sb.toString();
}
}
还有两个实体类:
/** * 信息基础类 * @author Administrator * */
public class BaseMessage {
private String ToUserName;
private String FromUserName;
private long CreateTime;
private String MsgType;
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public long getCreateTime() {
return CreateTime;
}
public void setCreateTime(long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
}
/** * 文本信息类 * @author Administrator * */
public class TextMessage extends BaseMessage{
private String Content;
private long MsgId;
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public long getMsgId() {
return MsgId;
}
public void setMsgId(long msgId) {
MsgId = msgId;
}
}
其中json传唤成map需要一个jar包,下载地址:
http://download.csdn.net/detail/zxxz5201314/9926317
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188721.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...