WeChat is becoming more and more popular, and WeChat public platform has become a new favorite. This article usesC#.netDevelop WeChat public signal interface.
WeChat interface address code:
weixin _wx = new weixin(); string postStr = ""; if (() == "post") { Stream s = ; byte[] b = new byte[]; (b, 0, (int)); postStr = Encoding.(b); if (!(postStr)) //Request processing { _wx.Handle(postStr); } } else { _wx.Auth(); } WeChat processing category: public class weixin { private string Token = "weixin_token"; //Switch to your own token public void Auth() { string echoStr = ["echoStr"]; if (CheckSignature()) { if (!(echoStr)) { (echoStr); (); } } } public void Handle(string postStr) { //Encapsulate request class XmlDocument doc = new XmlDocument(); (postStr); XmlElement rootElement = ; XmlNode MsgType = ("MsgType"); RequestXML requestXML = new RequestXML(); = ("ToUserName").InnerText; = ("FromUserName").InnerText; = ("CreateTime").InnerText; = ; if ( == "text") { = ("Content").InnerText; } else if ( == "location") { requestXML.Location_X = ("Location_X").InnerText; requestXML.Location_Y = ("Location_Y").InnerText; = ("Scale").InnerText; = ("Label").InnerText; } else if ( == "image") { = ("PicUrl").InnerText; } //Reply to the message ResponseMsg(requestXML); } /// <summary> /// Verify WeChat signature /// </summary> /// * Sort dictionary order of token, timestamp, and nonce /// * Splice three parameter strings into one string for sha1 encryption /// * The strings obtained by the developer can be compared with signature, identifying that the request comes from WeChat. /// <returns></returns> private bool CheckSignature() { string signature = ["signature"]; string timestamp = ["timestamp"]; string nonce = ["nonce"]; string[] ArrTmp = { Token, timestamp, nonce }; (ArrTmp); //Dictionary sort string tmpStr = ("", ArrTmp); tmpStr = (tmpStr, "SHA1"); tmpStr = (); if (tmpStr == signature) { return true; } else { return false; } } /// <summary> /// Reply message (WeChat information returns) /// </summary> /// <param name="weixinXML"></param> private void ResponseMsg(RequestXML requestXML) { try { string resxml = ""; mijiya mi = new mijiya(,); if ( == "text") { //Execute a series of operations here to automatically reply to content. string _reMsg = (); if ( == 1) { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>2</ArticleCount><Articles>"; resxml += (); resxml += "</Articles><FuncFlag>1</FuncFlag></xml>"; } else { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + _reMsg + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } } else if ( == "location") { string city = GetMapInfo(requestXML.Location_X, requestXML.Location_Y); if (city == "0") { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Okay, we know your location. You can:" + () + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } else { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Okay, we know your location. You can:" + () + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } } else if ( == "image") { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Dear, I don't understand what you mean. You can:" + () + "]]></Content><FuncFlag>1</FuncFlag></xml>"; //Return to less than 10 //int size = 10; //resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + size + "</ArticleCount><Articles>"; //List<string> list = new List<string>(); //// If there are 20 queries to return results //for (int i = 0; i < 20; i++) //{ // ("1"); //} //string[] piclist = new string[] { "/Abstract_Pencil_Scribble_Background_Vector_main.jpg", "/balloon_tree.jpg", "/", "/colorful_flowers.jpg", "/colorful_summer_flower.jpg", "/", "/fall_tree.jpg", "/growing_flowers.jpg", "/shoes_illustration.jpg", "/splashed_tree.jpg" }; //for (int i = 0; i < size && i < ; i++) //{ // resxml += "<item><Title><![CDATA[Shenyang-Heilongjiang]]></Title><Description><![CDATA[New Year's Day Special Price: ¥300 Market Price: ¥400]]></Description><PicUrl><![CDATA[" + "" + piclist[i] + "]]></PicUrl><Url><![CDATA[]]></Url></item>"; //} //resxml += "</Articles><FuncFlag>1</FuncFlag></xml>"; } else if (wx_tmsg.GetMsgCount() == 0) { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + () + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } else { resxml = "<xml><ToUserName><![CDATA[" + + "]]></ToUserName><FromUserName><![CDATA[" + + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[Dear, I don't understand what you mean. You can:" + () + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } //WriteTxt(resxml); (resxml); WriteToDB(requestXML, resxml,); } catch (Exception ex) { //WriteTxt("Exception:" + + "Struck:" + ()); wx_logs.MyInsert("abnormal:" + + "Struck:" + ()); } } /// <summary> ///Unix time converted to datetime /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> private DateTime UnixTimeToTime(string timeStamp) { DateTime dtStart = (new DateTime(1970, 1, 1)); long lTime = (timeStamp + "0000000"); TimeSpan toNow = new TimeSpan(lTime); return (toNow); } /// <summary> /// Convert datetime to unixtime /// </summary> /// <param name="time"></param> /// <returns></returns> private int ConvertDateTimeInt( time) { startTime = (new (1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } /// <summary> /// Call Baidu map to return coordinate information /// </summary> /// <param name="y">Longitude</param> /// <param name="x">latitude</param> /// <returns></returns> public string GetMapInfo(string x, string y) { try { string res = ; string parame = ; string url = "/maps/api/geocode/xml"; parame = "latlng=" + x + "," + y + "&language=zh-CN&sensor=false";//This key is for personal application res = webRequestPost(url, parame); XmlDocument doc = new XmlDocument(); (res); XmlElement rootElement = ; string Status = ("status").InnerText; if (Status == "OK") { // Get only cities XmlNodeList xmlResults = ("/GeocodeResponse").ChildNodes; for (int i = 0; i < ; i++) { XmlNode childNode = xmlResults[i]; if ( == "status") { continue; } string city = "0"; for (int w = 0; w < ; w++) { for (int q = 0; q < [w].; q++) { XmlNode childeTwo = [w].ChildNodes[q]; if ( == "long_name") { city = ; } else if ( == "locality") { return city; } } } return city; } } } catch (Exception ex) { //WriteTxt("map exception:" + () + "Struck:" + ()); return "0"; } return "0"; } /// <summary> /// Post Submit Call Crawl /// </summary> /// <param name="url">Submit address</param> /// <param name="param">param</param> /// <returns>string</returns> public string webRequestPost(string url, string param) { byte[] bs = .(param); HttpWebRequest req = (HttpWebRequest)(url + "?" + param); = "Post"; = 120 * 1000; = "application/x-www-form-urlencoded;"; = ; using (Stream reqStream = ()) { (bs, 0, ); (); } using (WebResponse wr = ()) { //The received page content is processed here Stream strm = (); StreamReader sr = new StreamReader(strm, .UTF8); string line; sb = new (); while ((line = ()) != null) { (line + ); } (); (); return (); } } private void WriteToDB(RequestXML requestXML,string _xml,int _pid) { wx_tmsg wx = new wx_tmsg(); (); = ; = ; = ; = ; = ; wx.Location_X = requestXML.Location_X; wx.Location_Y = requestXML.Location_Y; = ; = ; = ; = _xml; = _pid; try { (); } catch (Exception ex) { wx_logs.MyInsert(); //; } } } //WeChat request class public class RequestXML { private string toUserName=""; /// <summary> /// The WeChat account of the message recipient is generally the WeChat account of the public platform account /// </summary> public string ToUserName { get { return toUserName; } set { toUserName = value; } } private string fromUserName = ""; /// <summary> /// WeChat signal of the sender /// </summary> public string FromUserName { get { return fromUserName; } set { fromUserName = value; } } private string createTime = ""; /// <summary> /// Creation time /// </summary> public string CreateTime { get { return createTime; } set { createTime = value; } } private string msgType = ""; /// <summary> /// Information type Geographic location: location, text message: text, message type: image /// </summary> public string MsgType { get { return msgType; } set { msgType = value; } } private string content = ""; /// <summary> /// Information content /// </summary> public string Content { get { return content; } set { content = value; } } private string location_X = ""; /// <summary> /// Geographical latitude /// </summary> public string Location_X { get { return location_X; } set { location_X = value; } } private string location_Y = ""; /// <summary> /// Geographical location longitude /// </summary> public string Location_Y { get { return location_Y; } set { location_Y = value; } } private string scale = ""; /// <summary> /// Map zoom size /// </summary> public string Scale { get { return scale; } set { scale = value; } } private string label = ""; /// <summary> /// Geographic location information /// </summary> public string Label { get { return label; } set { label = value; } } private string picUrl = ""; /// <summary> /// Image link, developers can use HTTP GET to obtain /// </summary> public string PicUrl { get { return picUrl; } set { picUrl = value; } } }
Note: What are usedmijiyaClasses mainly call the database to automatically reply to keyword matching and can be written according to your own business situation.