The last article, wrote the basic framework, may be a lot of people will feel dizzy, here provide a simple example to explain, hoping to help you solve the mystery.
First, Function introduction
The function of online customer service robot is realized by micro-trust public platform. The main functions include: simple dialogue, weather and other services.
This is simply a simple function to illustrate the specific development process of the public platform through this example. It's just a simple demo that can be extended on this basis if needed.
Of course, we will also introduce more complex application examples.
Second, the concrete realization
1. Provide access interface
Here no longer repeat, referring to the previous chapter, micro-credit Public account development Tutorial (ii) Foundation framework
Http://www.jb51.net/article/98754.htm
2. Signature authentication and distribution request
Here no longer repeat, referring to the previous chapter, micro-credit Public account development Tutorial (ii) Foundation framework
Http://www.jb51.net/article/98754.htm
3, processing requests, and response
1) concern
When the micro-trust users pay attention to the public account, you can give them appropriate hints. Can be a welcome word, can be a help hint.
Directly on the code:
Class Eventhandler:ihandler {///<summary>///requested XML///</summary> private string requ
estxml {get; set;} <summary>///Constructor///</summary>///<param name= "RequestXML" ></param> public EventHandler (String requestxml) {this.
RequestXML = RequestXML; ///<summary>///processing request///</summary>///<returns></returns> public string HandleRequest () {string response = string.
Empty;
Eventmessage em = eventmessage.loadfromxml (RequestXML); if (em. Event.equals ("subscribe", StringComparison.OrdinalIgnoreCase)) {//Reply to welcome message TextMessage TM = new TextMe
Ssage (); Tm. Tousername = em.
Fromusername; Tm. Fromusername = em.
Tousername; Tm.
Createtime = Common.getnowtime (); Tm. Content = "Welcome your attention * * * *, I am a mobile phone, something asked me, hehe!"
\ n '; Response = TM.
Generatecontent ();
return response;
}
}
2) Greetings
Simple Exchange greetings, such as Hello, help, and so on, with our use of micro-letter chat, but the response is by our program response. Specific functions, can be added according to their own needs.
Micro-letter is the platform of Communication. This case, can be used for online service robot, similar to Taobao customer service robot, but we this is a micro-letter version. Oh
In fact, it's very simple to get the request message and match the response according to the keyword. Of course there may be a lot of work to do here, how to support smart matching, how to support fuzzy matching, and so on.
The code is as follows:
<summary>///text Information Processing class///</summary> public class Texthandler:ihandler {///<summary>
;
The requested XML///</summary> private string RequestXML {get; set;}
<summary>///Constructor///</summary>///<param name= "RequestXML" > Request xml</param> Public Texthandler (String requestxml) {this.
RequestXML = RequestXML; ///<summary>///processing request///</summary>///<returns></returns> public string HandleRequest () {string response = string.
Empty;
TextMessage TM = Textmessage.loadfromxml (RequestXML); String content = tm.
Content.trim (); if (string. IsNullOrEmpty (content)) {response = "You have not input anything, can not help you ah,%>_<%."
"; } else {if (content). StartsWith ("TQ", StringComparison.OrdinalIgnoreCase)) {string cityname = content. Substring (2).
Trim (); Response = WeatherhelpEr.
GetWeather (CityName);
else {response = Handleother (content); } TM.
Content = response; For sender, receiver conversion String temp = TM.
Tousername; Tm. Tousername = TM.
Fromusername; Tm.
Fromusername = temp; Response = TM.
Generatecontent ();
return response; ///<summary>///processing Other messages///</summary>///<param name= "TM" ></param>///&L T;returns></returns> private String Handleother (String requestcontent) {string response = STRING.E
Mpty;
if (Requestcontent.contains ("Hello") | | | requestcontent.contains ("hello")) {response = "you are OK"; else if (requestcontent.contains ("silly")) {response = "I am not stupid!"
Hum ~ "; else if (Requestcontent.contains ("forced") | | | requestcontent.contains ("CAO")) {response = "hum, you say foul language!"
"; else if (Requestcontent.contains ("who") {response = "I'm a cellular,Is there anything I can do for you?
~"; else if (Requestcontent.contains ("Goodbye")) {response = "Goodbye!"
"; else if (Requestcontent.contains ("Bye")) {response = "bye!
"; else if (requestcontent.contains ("Thank You")) {response = "You're welcome!"
Hehe "; else if (requestcontent = "H" | | requestcontent = = "H" | | | requestcontent.contains ("help")) {response
= @ "Query weather, enter TQ city name pinyin \ First letter"; else {response = "You said, unfortunately, I do not understand ah, try other keywords."
";
return response;
}
}
3) Check Weather
This feature requires real-time query, request the official weather release site, and then resolve its return value, according to the format we need to organize weather information, and finally sent to the micro-trust customers.
Processed by text message.
User request, just enter: TQ city name/pinyin/first letter, you can get the message.
Reply message: (take Beijing as an example)
Beijing
Wednesday, November 6, 2013
Today: (17℃~4℃) Clear North Wind 4-5 turn 3-4 level 4-5 turn 3-4 class
24-hour Clothing index: cold, recommended cotton clothing, down jacket, leather jacket and sweater, such as winter clothing. Elderly people should be a thick coat, winter coat or thick down jacket.
Tomorrow: (14℃~3℃) Sunny turn cloudy breeze less than 3
48-hour Clothing index: cold, recommended cotton clothing, down jacket, leather jacket and sweater, such as winter clothing. Elderly people should be a thick coat, winter coat or thick down jacket.
Look at the source code bar:
Class Weatherhelper {///<summary>///City Collection field///</summary> private static dictionary<
string, city> mcities;
<summary>///City Collection///</summary> public static dictionary<string, city> cities {
get {if (mcities = null) {LoadCities ();
return mcities; }///<summary>///load city///</summary> private static void LoadCities () {MCi
Ties = new dictionary<string, city> ();
Mcities.clear ();
Mcities.add ("101010100", New City () {Code = "101010100", Name = "Beijing", PinYin = "Beijing", Fristletter = "BJ"});
Mcities.add ("101020100", New City () {Code = "101020100", Name = "Shanghai", PinYin = "Shanghai", Fristletter = "sh"});
Mcities.add ("101200101", New City () {Code = "101200101", Name = "Wuhan", PinYin = "Wuhai", Fristletter = "WH"});
///<summary>///getting weather in the city</summary>///<param name= "name" > city name, pinyin or initials </param>///<returns></returns> public static string GetWeather (string name) {string, result = String.
Empty; String Citycode = String.
Empty; Get the city code ienumerable<string> codes = from item in cities where item. Value!= null && (item. Value.Name.Equals (Name, stringcomparison.ordinalignorecase) | | Item. Value.PinYin.Equals (name, stringcomparison.ordinalignorecase) | | Item. Value.FristLetter.Equals (name, stringcomparison.ordinalignorecase)) Select item.
Value.code; if (codes!= null && codes. Count () > 0) {citycode = codes.
First<string> (); //http request, get weather if (!string.
IsNullOrEmpty (Citycode)) {string url = "Http://m.weather.com.cn/data/{0}.html"; url = string. Format (URL, Citycode);
WebRequest request = httpwebrequest.create (URL); Timeout: 2 seconds request.
Timeout = 2000; Request.
Credentials = CredentialCache.DefaultCredentials; WebResponse response = Request.
GetResponse (); StreamReader reader = new StreamReader (response.
GetResponseStream (), Encoding.UTF8); String weahterinfo = reader.
ReadToEnd (); if (string.
IsNullOrEmpty (Weahterinfo)) {result = "No weather data is currently taken, please try again later";
else {XmlDocument doc = Jsonconvert.deserializexmlnode (weahterinfo); if (Doc!= null) {XmlNode node = doc.
DocumentElement;
if (node!= null) {StringBuilder builder = new StringBuilder (); Builder. Append (node["City"). innertext).
Append ("\ n"); Builder. Append (node["date_y"). innertext). Append (""). Append (node["Week"). innertext).
Append ("\ n"); Builder. Append ("Today:"). Append ("("). Append (node["Temp1"). innertext). Append (")"). Append (node["Weather1"). innertext). Append (node["Wind1"). innertext). Append (node["FL1"). innertext).
Append ("\ n"); Builder. Append ("24-hour Dressing index:"). Append (node["Index_d"). innertext).
Append ("\ n"); Builder. Append ("Tomorrow:"). Append ("("). Append (node["Temp2"). innertext). Append (")"). Append (node["Weather2"). innertext). Append (node["Wind2"). innertext). Append (node["FL2"). innertext).
Append ("\ n"); Builder. Append ("48-hour Dressing Index:"). Append (node["Index48_d"). innertext).
Append ("\ n"); result = Builder.
ToString (); }} #region weather JSON data Format/* {"Weatherinfo": {"City": "Beijing", "CIT Y_en ":" Beijing "," date_y ":" November 4, 2013 "," Date ":" "," Week ":" Monday "," Fchh ":" One "," Cityid ":" 101 010100 "," Temp1 ":" 17℃~5℃ "," Temp2 ":" 16℃~5℃ "," Temp3 ":" 18℃~4℃ "," Temp4 ":" 17℃~5℃ "," TEMP5 ":" 14 ℃~6℃ "," TEMP6 ":" 14℃~2℃ "," tEmpF1 ":" 62.6℉~41℉ "," tempF2 ":" 60.8℉~41℉ "," tempF3 ":" 64.4℉~39.2℉ "," tempF4 ":" 62.6℉~41℉ "," tempF5 ":
"57.2℉~42.8℉", "tempF6": "57.2℉~35.6℉", "Weather1": "Sunny to Cloudy", "Weather2": "Cloudy", "Weather3": "Cloudy Turn Fine", "Weather4": "Clear to Cloudy", "Weather5": "Doyun", "Weather6": "Yin Turn Clear", "IMG1": "0", "Img2": "1", "IMG3": "1" , "IMG4": "", "Img5": "1", "Img6": "0", "Img7": "0", "Img8": "1", "IMG9": "1", "img10
":" 2 "," Img11 ":" 2 "," Img12 ":" 0 "," img_single ":" 0 "," img_title1 ":" Clear "," Img_title2 ":" Cloudy ",
"Img_title3": "Cloudy", "img_title4": "Cloudy", "img_title5": "Cloudy", "Img_title6": "Clear", "Img_title7": "Clear",
"Img_title8": "Cloudy", "Img_title9": "Cloudy", "Img_title10": "Yin", "img_title11": "Yin", "img_title12": "Clear", "Img_title_single": "Clear", "wind1": "Breeze", "wind2": "Breeze", "wind3": "Breeze", "Wind4": "Breeze", "Wind5 ":" The Breeze "," Wind6 ":"North wind level 4-5, "fx1": "Breeze", "fx2": "Breeze", "fl1": "Less than 3", "FL2": "Less than 3", "FL3": "Less than 3", "FL4": "Less than 3 levels "," FL5 ":" Less than 3 "," fl6 ":" 4-5 Level "," index ":" Colder "," index_d ":" Recommended coats, coats and sweaters, Wei clothing and other clothing. " A thick coat and a thick sweater are suitable for the frail body. Due to the large temperature difference between day and night, pay attention to clothing. "," index48 ":" Cold "," index48_d ":" The weather is cold, suggest the cotton suit, down jacket, leather jacket and sweater and so on winter clothing. " Elderly people should be a thick coat, winter coat or thick down jacket.
"," Index_uv ":" Medium "," INDEX48_UV ":" Weak "," INDEX_XC ":" Appropriate "," INDEX_TR ":" Appropriate "," Index_co ":" Comfortable ", "St1": "St2": "5", "ST3": "", "St4": "5", "ST5": "A", "St6": "6", "INDEX_CL": "Appropriate
"," Index_ls ":" Appropriate "," Index_ag ":" very difficult to send "}//#endregion}} else
{result = "did not get the weather in the city, please make sure you entered the correct city name, such as ' Beijing ' or ' beijing\ ' or ' bj\ '";
//return returns result;
///<summary>///Inner class: City///</summary> Internal class Cities {///<summary> Code///</summary> PubLic string Code {get; set;}
<summary>///name///</summary> public string name {get; set;}
<summary>///Pinyin///</summary> public string PinYin {get; set;}
<summary>///Pinyin First letter///</summary> public string Fristletter {get; set;}
}
}
Third, the source code
This is an executable code, OH. In response to everyone's needs, this provides all the source code.
Demo download
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.