說明:
本次的教程主要是對微信公眾平臺開發者模式的講解,網絡上很多類似文章,但很多都讓初學微信開發的人壹頭霧水,所以總結自己的微信開發經驗,將微信開發的整個過程系統的列出,並對主要代碼進行講解分析,讓初學者盡快上手。
在閱讀本文之前,應對微信公眾平臺的官方開發文檔有所了解,知道接收和發送的都是xml格式的數據。另外,在做內容回復時用到了圖靈機器人的api接口,這是壹個自然語言解析的開放平臺,可以幫我們解決整個微信開發過程中最困難的問題,此處不多講,下面會有其詳細的調用方式。
1.1 在登錄微信官方平臺之後,開啟開發者模式,此時需要我們填寫url和token,所謂url就是我們自己服務器的接口,用WechatServlet.java來實現,相關解釋已經在註釋中說明,代碼如下:
[java]?view plain?copy
package?demo.servlet;?
import?java.io.BufferedReader;?
import?java.io.IOException;?
import?java.io.InputStream;?
import?java.io.InputStreamReader;?
import?java.io.OutputStream;?
import?javax.servlet.ServletException;?
import?javax.servlet./xml/ns/javaee"
xmlns:xsi="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd">?
<servlet>?
<description>This?is?the?description?of?my?J2EE?component</description>?
<display-name>This?is?the?display?name?of?my?J2EE?component</display-name>?
<servlet-name>WechatServlet</servlet-name>?
<servlet-class>demo.servlet.WechatServlet</servlet-class>?
</servlet>?
<servlet-mapping>?
<servlet-name>WechatServlet</servlet-name>?
<url-pattern>/wechat.do</url-pattern>?
</servlet-mapping>?
<welcome-file-list>?
<welcome-file>index.jsp</welcome-file>?
</welcome-file-list>?
</web-app>?
1.3 通過以上代碼,我們已經實現了微信公眾平臺開發的框架,即開通開發者模式並成功接入、接收消息和發送消息這三個步驟。
下面就講解其核心部分——解析接收到的xml數據,並以文本類消息為例,通過圖靈機器人api接口實現智能回復。
2.1 首先看壹下整體流程處理代碼,包括:xml數據處理、調用圖靈api、封裝返回的xml數據。
[java]?view plain?copy
package?demo.process;?
import?java.util.Date;?
import?demo.entity.ReceiveXmlEntity;?
/**?
*?微信xml消息處理流程邏輯類?
*?@author?pamchen-1?
*?
*/?
public?class?WechatProcess?{?
/**?
*?解析處理xml、獲取智能回復結果(通過圖靈機器人api接口)?
*?@param?xml?接收到的微信數據?
*?@return?最終的解析結果(xml格式數據)?
*/?
public?String?processWechatMag(String?xml){?
/**?解析xml數據?*/?
ReceiveXmlEntity?xmlEntity?=?new?ReceiveXmlProcess().getMsgEntity(xml);?
/**?以文本消息為例,調用圖靈機器人api接口,獲取回復內容?*/?
String?result?=?"";?
if("text".endsWith(xmlEntity.getMsgType())){?
result?=?new?TulingApiProcess().getTulingResult(xmlEntity.getContent());?
}?
/**?此時,如果用戶輸入的是“妳好”,在經過上面的過程之後,result為“妳也好”類似的內容?
*?因為最終回復給微信的也是xml格式的數據,所有需要將其封裝為文本類型返回消息?
*?*/?
result?=?new?FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(),?xmlEntity.getToUserName(),?result);?
return?result;?
}?
}?
2.2 解析接收到的xml數據,此處有兩個類,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通過反射的機制動態調用實體類中的set方法,可以避免很多重復的判斷,提高代碼效率,代碼如下:
[java]?view plain?copy
package?demo.entity;?
/**?
*?接收到的微信xml實體類?
*?@author?pamchen-1?
*?
*/?
public?class?ReceiveXmlEntity?{?
private?String?ToUserName="";?
private?String?FromUserName="";?
private?String?CreateTime="";?
private?String?MsgType="";?
private?String?MsgId="";?
private?String?Event="";?
private?String?EventKey="";?
private?String?Ticket="";?
private?String?Latitude="";?
private?String?Longitude="";?
private?String?Precision="";?
private?String?PicUrl="";?
private?String?MediaId="";?
private?String?Title="";?
private?String?Description="";?
private?String?Url="";?
private?String?Location_X="";?
private?String?Location_Y="";?
private?String?Scale="";?
private?String?Label="";?
private?String?Content="";?
private?String?Format="";?
private?String?Recognition="";?
public?String?getRecognition()?{?
return?Recognition;?
}?
public?void?setRecognition(String?recognition)?{?
Recognition?=?recognition;?
}?
public?String?getFormat()?{?
return?Format;?
}?
public?void?setFormat(String?format)?{?
Format?=?format;?
}?
public?String?getContent()?{?
return?Content;?
}?
public?void?setContent(String?content)?{?
Content?=?content;?
}?
public?String?getLocation_X()?{?
return?Location_X;?
}?
public?void?setLocation_X(String?locationX)?{?
Location_X?=?locationX;?
}?
public?String?getLocation_Y()?{?
return?Location_Y;?
}?
public?void?setLocation_Y(String?locationY)?{?
Location_Y?=?locationY;?
}?
public?String?getScale()?{?
return?Scale;?
}?
public?void?setScale(String?scale)?{?
Scale?=?scale;?
}?
public?String?getLabel()?{?
return?Label;?
}?
public?void?setLabel(String?label)?{?
Label?=?label;?
}?
public?String?getTitle()?{?
return?Title;?
}?
public?void?setTitle(String?title)?{?
Title?=?title;?
}?
public?String?getDescription()?{?
return?Description;?
}?
public?void?setDescription(String?description)?{?
Description?=?description;?
}?
public?String?getUrl()?{?
return?Url;?
}?
public?void?setUrl(String?url)?{?
Url?=?url;?
}?
public?String?getPicUrl()?{?
return?PicUrl;?
}?
public?void?setPicUrl(String?picUrl)?{?
PicUrl?=?picUrl;?
}?
public?String?getMediaId()?{?
return?MediaId;?
}?
public?void?setMediaId(String?mediaId)?{?
MediaId?=?mediaId;?
}?
public?String?getEventKey()?{?
return?EventKey;?
}?
public?void?setEventKey(String?eventKey)?{?
EventKey?=?eventKey;?
}?
public?String?getTicket()?{?
return?Ticket;?
}?
public?void?setTicket(String?ticket)?{?
Ticket?=?ticket;?
}?
public?String?getLatitude()?{?
return?Latitude;?
}?
public?void?setLatitude(String?latitude)?{?
Latitude?=?latitude;?
}?
public?String?getLongitude()?{?
return?Longitude;?
}?
public?void?setLongitude(String?longitude)?{?
Longitude?=?longitude;?
}?
public?String?getPrecision()?{?
return?Precision;?
}?
public?void?setPrecision(String?precision)?{?
Precision?=?precision;?
}?
public?String?getEvent()?{?
return?Event;?
}?
public?void?setEvent(String?event)?{?
Event?=?event;?
}?
public?String?getMsgId()?{?
return?MsgId;?
}?
public?void?setMsgId(String?msgId)?{?
MsgId?=?msgId;?
}?
public?String?getToUserName()?{?
return?ToUserName;?
}?
public?void?setToUserName(String?toUserName)?{?