程式碼地址:
程式碼使用了jdom解析和輸出xml
https://github.com/0x0001/java-weixinlib
public class WxCtr1 extends Controller {
private static final String TOKEN = "xxxxxx";
public void index() {
String signature = getPara("signature");
String timestamp = getPara("timestamp");
String nonce = getPara("nonce");
String echostr = getPara("echostr");
if(null != timestamp && null != nonce && null != echostr && null != signature) {
if(WeiXin.access(TOKEN, signature, timestamp, nonce)) {
renderHtml(echostr);
return;
}
renderNull();
return;
}
try {
WxRecvMsg msg = WeiXin.recv(getRequest().getInputStream());
WxSendMsg sendMsg = WeiXin.builderSendByRecv(msg);
System.out.println(msg);
if(msg instanceof WxRecvEventMsg) {
WxRecvEventMsg m = (WxRecvEventMsg) msg;
String event = m.getEvent();
if("subscribe".equals(event)) {
String content = "感謝關注";
sendMsg = new WxSendTextMsg(sendMsg, content);
WeiXin.send(sendMsg, getResponse().getOutputStream());
renderNull();
}
}
if(msg instanceof WxRecvTextMsg) {
WxRecvTextMsg m = (WxRecvTextMsg) msg;
String text = m.getContent();
if(null != text)
text = text.trim();
Set<String> keyworkds = new HashSet<String>();
keyworkds.add("1");
keyworkds.add("選單");
keyworkds.add("menu");
if(keyworkds.contains(text)) {
String content = "1. 選單\n2.音樂\n,3.圖文\n";
sendMsg = new WxSendTextMsg(sendMsg, content);
} else if("2".equals(text)) {
sendMsg = new WxSendMusicMsg(sendMsg, "夜夜夜夜","林志炫",
"http://hugemaxiao.co/COFFdD0xMzY1Njg5NTIyJmk9MTIxLjM0LjMxLjMmdT1Tb25ncy92MS9mYWludFFDLzQ3LzkxMGFlM2JjMzM5MDNlZGI3NmY3MDUzY2ZjZDI1YjQ3Lm1wMyZtPTMzMWQwNGY2YTNkNTRiNTMwYzc1MjhkN2FmNDRhMGZjJnY9ZG93biZuPdK50rnSudK5JnM9wdbWvuzFJnA9cw==.mp3",
"http://hugemaxiao.co/COFFdD0xMzY1Njg5NTIyJmk9MTIxLjM0LjMxLjMmdT1Tb25ncy92MS9mYWludFFDLzQ3LzkxMGFlM2JjMzM5MDNlZGI3NmY3MDUzY2ZjZDI1YjQ3Lm1wMyZtPTMzMWQwNGY2YTNkNTRiNTMwYzc1MjhkN2FmNDRhMGZjJnY9ZG93biZuPdK50rnSudK5JnM9wdbWvuzFJnA9cw==.mp3");
} else if("3".equals(text)) {
sendMsg = new WxSendNewsMsg(sendMsg)
.addItem("圖片4", "描述4", "http://g.hiphotos.baidu.com/album/w%3D230/sign=b2b8791418d8bc3ec60801c9b28ba6c8/1ad5ad6eddc451da5ff2c0c5b7fd5266d016329c.jpg", "http://www.oschina.net")
.addItem("圖片1", "描述1", "http://a.hiphotos.baidu.com/album/w%3D230/sign=67afac3d242dd42a5f0906a8333a5b2f/8c1001e93901213f088bb78855e736d12f2e952e.jpg", "http://www.google.com")
.addItem("圖片2", "描述2", "http://e.hiphotos.baidu.com/album/w%3D230/sign=5254e9265243fbf2c52ca120807eca1e/77c6a7efce1b9d1669f2564ff2deb48f8c5464e3.jpg", "http://www.baidu.com")
.addItem("圖片3", "描述3", "http://d.hiphotos.baidu.com/album/w%3D230/sign=c3b3fe3aaa18972ba33a07c9d6cc7b9d/3812b31bb051f81995bdc164dbb44aed2e73e700.jpg", "http://www.sina.com")
;
} else {
String content = "1. 選單\n2.音樂\n,3.圖文\n";
sendMsg = new WxSendTextMsg(sendMsg, content);
}
WeiXin.send(sendMsg, getResponse().getOutputStream());
renderNull();
return;
} else {
String content = "1. 選單\n2.音樂\n,3.圖文\n";
sendMsg = new WxSendTextMsg(sendMsg, content);
WeiXin.send(sendMsg, getResponse().getOutputStream());
renderNull();
return;
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
renderNull();
}
}
學習了。