


WeChat public platform development tutorial (4) Getting started with examples: robots
Feb 16, 2017 pm 04:20 PM1. Function introduction
Realize the online customer service robot function through the WeChat public platform. The main functions include: simple conversation, weather query and other services.
Here we only provide relatively simple functions, focusing on using this example to illustrate the specific development process of the public platform. It's just a simple DEMO and can be expanded on this basis if necessary.
Of course, we will also introduce more complex application examples in the future.
2. Specific implementation
1. Provide access interface
I won’t go into details here. Refer to the previous chapter, WeChat public account development tutorial (2) Basic framework construction
2. Signature authentication and distribution request
I won’t go into details here. Refer to the previous chapter, WeChat public account development tutorial (2) Basic framework construction
3. Process the request and respond
1) Follow
When WeChat users follow public accounts, they can be given appropriate prompts. It can be a welcome message or a help tip.
Directly enter the code:
????class?EventHandler?:?IHandler ????{????????///?<summary> ????????///?請求的xml????????///?</summary> ????????private?string?RequestXml?{?get;?set;?}????????///?<summary> ????????///?構(gòu)造函數(shù)????????///?</summary> ????????///?<param name="requestXml"></param> ????????public?EventHandler(string?requestXml) ????????{????????????this.RequestXml?=?requestXml; ????????}????????///?<summary> ????????///?處理請求????????///?</summary> ????????///?<returns></returns> ????????public?string?HandleRequest() ????????{????????????string?response?=?string.Empty; ????????????EventMessage?em?=?EventMessage.LoadFromXml(RequestXml);????????????if?(em.Event.Equals("subscribe",StringComparison.OrdinalIgnoreCase)) ????????????{????????????????//回復(fù)歡迎消息 ????????????????TextMessage?tm?=?new?TextMessage(); ????????????????tm.ToUserName?=?em.FromUserName; ????????????????tm.FromUserName?=?em.ToUserName; ????????????????tm.CreateTime?=?Common.GetNowTime(); ????????????????tm.Content?=?"歡迎您關(guān)注***,我是大哥大,有事就問我,呵呵!\n\n"; ????????????????response?=?tm.GenerateContent(); ????????????}????????????return?response; ????????} ????}
2) Greetings
Simple communication greetings, such as hello, help, etc. , just like we use WeChat to chat, but the response is responded by our program. Specific functions can be added according to your own needs.
WeChat is originally a platform for communication. This case can be used for online service robots, similar to Taobao’s customer service robots, but ours is the WeChat version. Haha
Actually, it’s very simple, get the request message and match the response based on the keywords. Of course, there may be a lot of work to be done here, such as how to support intelligent matching, how to support fuzzy matching, etc.
The code is as follows:
????///?<summary> ????///?文本信息處理類????///?</summary> ????public?class?TextHandler?:?IHandler ????{????????///?<summary> ????????///?請求的XML????????///?</summary> ????????private?string?RequestXml?{?get;?set;?}????????///?<summary> ????????///?構(gòu)造函數(shù)????????///?</summary> ????????///?<param name="requestXml">請求的xml</param> ????????public?TextHandler(string?requestXml) ????????{????????????this.RequestXml?=?requestXml; ????????}????????///?<summary> ????????///?處理請求????????///?</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?=?"您什么都沒輸入,沒法幫您啊,%>_<%。"; ????????????}????????????else ????????????{????????????????if?(content.StartsWith("tq",?StringComparison.OrdinalIgnoreCase)) ????????????????{????????????????????string?cityName?=?content.Substring(2).Trim(); ????????????????????response?=?WeatherHelper.GetWeather(cityName); ????????????????}????????????????else ????????????????{ ????????????????????response?=?HandleOther(content); ????????????????} ????????????} ????????????tm.Content?=?response;????????????//進(jìn)行發(fā)送者、接收者轉(zhuǎn)換 ????????????string?temp?=?tm.ToUserName; ????????????tm.ToUserName?=?tm.FromUserName; ????????????tm.FromUserName?=?temp; ????????????response?=?tm.GenerateContent();????????????return?response; ????????}????????///?<summary> ????????///?處理其他消息????????///?</summary> ????????///?<param name="tm"></param> ????????///?<returns></returns> ????????private?string?HandleOther(string?requestContent) ????????{????????????string?response?=?string.Empty;????????????if?(requestContent.Contains("你好")?||?requestContent.Contains("您好")) ????????????{ ????????????????response?=?"您也好~"; ????????????}????????????else?if?(requestContent.Contains("傻")) ????????????{ ????????????????response?=?"我不傻!哼~?"; ????????????}????????????else?if?(requestContent.Contains("逼")?||?requestContent.Contains("操")) ????????????{ ????????????????response?=?"哼,你說臟話!?"; ????????????}????????????else?if?(requestContent.Contains("是誰")) ????????????{ ????????????????response?=?"我是大哥大,有什么能幫您的嗎?~"; ????????????}????????????else?if?(requestContent.Contains("再見")) ????????????{ ????????????????response?=?"再見!"; ????????????}????????????else?if?(requestContent.Contains("bye")) ????????????{ ????????????????response?=?"Bye!"; ????????????}????????????else?if?(requestContent.Contains("謝謝")) ????????????{ ????????????????response?=?"不客氣!嘿嘿"; ????????????}????????????else?if?(requestContent?==?"h"?||?requestContent?==?"H"?||?requestContent.Contains("幫助")) ????????????{ ????????????????response?=?@"查詢天氣,輸入tq?城市名稱\拼音\首字母"; ????????????}????????????else ????????????{ ????????????????response?=?"您說的,可惜,我沒明白啊,試試其他關(guān)鍵字吧。"; ????????????}????????????return?response; ????????} ????}
3) Query the weather
This function requires a request for real-time query , request the official weather release website, then parse its return value, organize the weather information according to the format we need, and finally send it to WeChat customers.
Using text message processing.
User request, just enter: tq city name/pinyin/initial letter to get the message.
Reply message: (Take Beijing as an example)
北京 2013年11月6日?星期三 今天:(17℃~4℃)晴北風(fēng)4-5級轉(zhuǎn)3-4級4-5級轉(zhuǎn)3-4級 24小時(shí)穿衣指數(shù):天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣、冬大衣或厚羽絨服。 明天:(14℃~3℃)晴轉(zhuǎn)多云微風(fēng)小于3級 48小時(shí)穿衣指數(shù):天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣、冬大衣或厚羽絨服。
Let’s take a look at the source code:
????class?WeatherHelper ????{????????///?<summary> ????????///?城市集合字段????????///?</summary> ????????private?static?Dictionary<string, City>?mCities;????????///?<summary> ????????///?城市集合????????///?</summary> ????????public?static?Dictionary<string, City>?Cities ????????{????????????get ????????????{????????????????if?(mCities?==?null) ????????????????{ ????????????????????LoadCities(); ????????????????}????????????????return?mCities; ????????????} ????????}????????///?<summary> ????????///?加載城市????????///?</summary> ????????private?static?void?LoadCities() ????????{ ????????????mCities?=?new?Dictionary<string, City>(); ????????????mCities.Clear(); ????????????mCities.Add("101010100",?new?City()?{?Code?=?"101010100",?Name?=?"北京",?PinYin?=?"beijing",?FristLetter?=?"bj"?}); ????????????mCities.Add("101020100",?new?City()?{?Code?=?"101020100",?Name?=?"上海",?PinYin?=?"shanghai",?FristLetter?=?"sh"?}); ????????????mCities.Add("101200101",?new?City()?{?Code?=?"101200101",?Name?=?"武漢",?PinYin?=?"wuhai",?FristLetter?=?"wh"?}); ???????????? ????????}????????///?<summary> ????????///?獲取城市的天氣????????///?</summary> ????????///?<param name="name">城市名稱、拼音或首字母</param> ????????///?<returns></returns> ????????public?static?string?GetWeather(string?name) ????????{????????????string?result?=?string.Empty;????????????string?cityCode?=?string.Empty;????????????//獲取城市編碼 ????????????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請求,獲取天氣 ????????????if?(!string.IsNullOrEmpty(cityCode)) ????????????{????????????????string?url?=?"http://m.miracleart.cn/{0}.html"; ????????????????url?=?string.Format(url,?cityCode); ????????????????WebRequest?request?=?HttpWebRequest.Create(url);????????????????//超時(shí)時(shí)間為:2秒 ????????????????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?=?"暫時(shí)沒有取到天氣數(shù)據(jù),請稍后再試"; ????????????????}????????????????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("今天:").Append("(").Append(node["temp1"].InnerText).Append(")").Append(node["weather1"].InnerText).Append(node["wind1"].InnerText).Append(node["fl1"].InnerText).Append("\n"); ????????????????????????????builder.Append("24小時(shí)穿衣指數(shù):").Append(node["index_d"].InnerText).Append("\n"); ????????????????????????????builder.Append("明天:").Append("(").Append(node["temp2"].InnerText).Append(")").Append(node["weather2"].InnerText).Append(node["wind2"].InnerText).Append(node["fl2"].InnerText).Append("\n"); ????????????????????????????builder.Append("48小時(shí)穿衣指數(shù):").Append(node["index48_d"].InnerText).Append("\n"); ????????????????????????????result?=?builder.ToString(); ????????????????????????} ????????????????????}????????????????????#region?天氣json數(shù)據(jù)格式????????????????????/* ????????????????? ????????????????{ ???"weatherinfo":?{ ???????"city":?"北京",? ???????"city_en":?"beijing",? ???????"date_y":?"2013年11月4日",? ???????"date":?"",? ???????"week":?"星期一",? ???????"fchh":?"11",? ???????"cityid":?"101010100",? ???????"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":?"晴轉(zhuǎn)多云",? ???????"weather2":?"多云",? ???????"weather3":?"多云轉(zhuǎn)晴",? ???????"weather4":?"晴轉(zhuǎn)多云",? ???????"weather5":?"多云轉(zhuǎn)陰",? ???????"weather6":?"陰轉(zhuǎn)晴",? ???????"img1":?"0",? ???????"img2":?"1",? ???????"img3":?"1",? ???????"img4":?"99",? ???????"img5":?"1",? ???????"img6":?"0",? ???????"img7":?"0",? ???????"img8":?"1",? ???????"img9":?"1",? ???????"img10":?"2",? ???????"img11":?"2",? ???????"img12":?"0",? ???????"img_single":?"0",? ???????"img_title1":?"晴",? ???????"img_title2":?"多云",? ???????"img_title3":?"多云",? ???????"img_title4":?"多云",? ???????"img_title5":?"多云",? ???????"img_title6":?"晴",? ???????"img_title7":?"晴",? ???????"img_title8":?"多云",? ???????"img_title9":?"多云",? ???????"img_title10":?"陰",? ???????"img_title11":?"陰",? ???????"img_title12":?"晴",? ???????"img_title_single":?"晴",? ???????"wind1":?"微風(fēng)",? ???????"wind2":?"微風(fēng)",? ???????"wind3":?"微風(fēng)",? ???????"wind4":?"微風(fēng)",? ???????"wind5":?"微風(fēng)",? ???????"wind6":?"北風(fēng)4-5級",? ???????"fx1":?"微風(fēng)",? ???????"fx2":?"微風(fēng)",? ???????"fl1":?"小于3級",? ???????"fl2":?"小于3級",? ???????"fl3":?"小于3級",? ???????"fl4":?"小于3級",? ???????"fl5":?"小于3級",? ???????"fl6":?"4-5級",? ???????"index":?"較冷",? ???????"index_d":?"建議著大衣、呢外套加毛衣、衛(wèi)衣等服裝。體弱者宜著厚外套、厚毛衣。因晝夜溫差較大,注意增減衣服。",? ???????"index48":?"冷",? ???????"index48_d":?"天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣、冬大衣或厚羽絨服。",? ???????"index_uv":?"中等",? ???????"index48_uv":?"弱",? ???????"index_xc":?"適宜",? ???????"index_tr":?"適宜",? ???????"index_co":?"舒適",? ???????"st1":?"17",? ???????"st2":?"5",? ???????"st3":?"17",? ???????"st4":?"5",? ???????"st5":?"18",? ???????"st6":?"6",? ???????"index_cl":?"適宜",? ???????"index_ls":?"適宜",? ???????"index_ag":?"極不易發(fā)" ???} }????????????????*/ ????????????????????#endregion ????????????????} ????????????}????????????else ????????????{ ????????????????result?=?"沒有獲取到該城市的天氣,請確定輸入了正確的城市名稱,如\'北京\'或者\(yùn)'beijing\'或者\(yùn)'bj\'"; ????????????}????????????//返回 ????????????return?result; ????????}????????///?<summary> ????????///?內(nèi)部類:城市????????///?</summary> ????????internal?class?City ????????{????????????///?<summary> ????????????///?編碼????????????///?</summary> ????????????public?string?Code?{?get;?set;?}????????????///?<summary> ????????????///?名稱????????????///?</summary> ????????????public?string?Name?{?get;?set;?}????????????///?<summary> ????????????///?拼音????????????///?</summary> ????????????public?string?PinYin?{?get;?set;?}????????????///?<summary> ????????????///?拼音首字母????????????///?</summary> ????????????public?string?FristLetter?{?get;?set;?} ????????} ????}
More WeChat public platform development tutorials (4) Getting started with examples: For robot-related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
