欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信公眾平臺(tái)開發(fā)教程(四) 實(shí)例入門:機(jī)器人回復(fù)(附源碼)

 更新時(shí)間:2016年12月02日 10:11:44   作者:停留的風(fēng)  
本篇文章主要介紹了微信公眾平臺(tái)開發(fā)機(jī)器人,可以實(shí)現(xiàn)簡(jiǎn)單對(duì)話和查詢天氣等,有需要的可以了解一下。

上一篇文章,寫了基本框架,可能很多人會(huì)覺得暈頭轉(zhuǎn)向,這里提供一個(gè)簡(jiǎn)單的例子來予以說明,希望能幫你解開謎團(tuán)。

一、功能介紹

通過微信公眾平臺(tái)實(shí)現(xiàn)在線客服機(jī)器人功能。主要的功能包括:簡(jiǎn)單對(duì)話、查詢天氣等服務(wù)。

這里只是提供比較簡(jiǎn)單的功能,重在通過此實(shí)例來說明公眾平臺(tái)的具體研發(fā)過程。只是一個(gè)簡(jiǎn)單DEMO,如果需要的話可以在此基礎(chǔ)上進(jìn)行擴(kuò)展。

當(dāng)然后續(xù)我們還會(huì)推出比較復(fù)雜的應(yīng)用實(shí)例。

二、具體實(shí)現(xiàn)

1、提供訪問接口

這里不再贅述,參照上一章,微信公眾賬號(hào)開發(fā)教程(二) 基礎(chǔ)框架搭建
http://www.dbjr.com.cn/article/98754.htm

2、簽名認(rèn)證和分發(fā)請(qǐng)求

這里不再贅述,參照上一章,微信公眾賬號(hào)開發(fā)教程(二) 基礎(chǔ)框架搭建
http://www.dbjr.com.cn/article/98754.htm

3、處理請(qǐng)求,并響應(yīng)

1)關(guān)注

當(dāng)微信用戶關(guān)注公眾賬號(hào)時(shí),可以給其適當(dāng)?shù)奶崾尽?梢允菤g迎詞,可以是幫助提示。

直接上代碼:

class EventHandler : IHandler
  {
    /// <summary>
    /// 請(qǐng)求的xml
    /// </summary>
    private string RequestXml { get; set; }
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="requestXml"></param>
    public EventHandler(string requestXml)
    {
      this.RequestXml = requestXml;
    }
    /// <summary>
    /// 處理請(qǐng)求
    /// </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)問候

簡(jiǎn)單的交流問候,比如你好、幫助等等,跟我們使用微信聊天一樣,不過回應(yīng)是由我們的程序響應(yīng)。具體功能,可以根據(jù)自己的需要進(jìn)行添加。

微信本來就是溝通的平臺(tái)。這個(gè)案例,可以用于在線服務(wù)機(jī)器人,類似于淘寶的客服機(jī)器人,可是我們這個(gè)是微信版的。呵呵
其實(shí),很簡(jiǎn)單,獲取請(qǐng)求消息,根據(jù)關(guān)鍵字來匹配回應(yīng)。當(dāng)然這里可能要做的工作很多,如何支持智能匹配,如何支持模糊匹配等。

代碼如下:

/// <summary>
  /// 文本信息處理類
  /// </summary>
  public class TextHandler : IHandler
  {
    /// <summary>
    /// 請(qǐng)求的XML
    /// </summary>
    private string RequestXml { get; set; }
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="requestXml">請(qǐng)求的xml</param>
    public TextHandler(string requestXml)
    {
      this.RequestXml = requestXml;
    }
    /// <summary>
    /// 處理請(qǐng)求
    /// </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)查詢天氣

這個(gè)功能需要請(qǐng)求實(shí)時(shí)查詢的,請(qǐng)求官方的天氣發(fā)布網(wǎng)站,然后解析其返回值,按照我們需要的格式,組織天氣信息,最后發(fā)送給微信客戶。

采用文本消息方式處理。

用戶請(qǐng)求,只需輸入:tq 城市名稱/拼音/首字母,即可獲取消息。

回復(fù)的消息:(以北京為例)

北京
2013年11月6日 星期三
今天:(17℃~4℃)晴北風(fēng)4-5級(jí)轉(zhuǎn)3-4級(jí)4-5級(jí)轉(zhuǎn)3-4級(jí)
24小時(shí)穿衣指數(shù):天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣、冬大衣或厚羽絨服。
明天:(14℃~3℃)晴轉(zhuǎn)多云微風(fēng)小于3級(jí)
48小時(shí)穿衣指數(shù):天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣、冬大衣或厚羽絨服。

來看源碼吧:

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請(qǐng)求,獲取天氣
      if (!string.IsNullOrEmpty(cityCode))
      {
        string url = "http://m.weather.com.cn/data/{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ù),請(qǐng)稍后再試";
        }
        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級(jí)", 
    "fx1": "微風(fēng)", 
    "fx2": "微風(fēng)", 
    "fl1": "小于3級(jí)", 
    "fl2": "小于3級(jí)", 
    "fl3": "小于3級(jí)", 
    "fl4": "小于3級(jí)", 
    "fl5": "小于3級(jí)", 
    "fl6": "4-5級(jí)", 
    "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 = "沒有獲取到該城市的天氣,請(qǐng)確定輸入了正確的城市名稱,如\'北京\'或者\(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; }
    }
  }

 三、源碼

這里可是可執(zhí)行的代碼哦。應(yīng)大家的需求,這里提供全部的源代碼。

demo下載

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaScript 函數(shù)的執(zhí)行過程

    JavaScript 函數(shù)的執(zhí)行過程

    下面小編就為大家?guī)硪黄狫avaScript 函數(shù)的執(zhí)行過程。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • TypeScript利用TS封裝Axios實(shí)戰(zhàn)

    TypeScript利用TS封裝Axios實(shí)戰(zhàn)

    這篇文章主要介紹了TypeScript利用TS封裝Axios實(shí)戰(zhàn),TypeScript封裝一遍Axios,能進(jìn)一步鞏固TypeScript的基礎(chǔ)知識(shí),需要的小伙伴可以參考一下
    2022-06-06
  • JavaScript中window和document用法詳解

    JavaScript中window和document用法詳解

    這篇文章主要介紹了JavaScript中window和document用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 純JavaScript創(chuàng)建一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表

    純JavaScript創(chuàng)建一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表

    這篇文章主要給大家介紹了關(guān)于純JavaScript創(chuàng)建一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表的相關(guān)資料,清單通常用于記錄我們?cè)谀骋惶煨枰瓿傻乃惺马?xiàng),將最關(guān)鍵的任務(wù)放在最上方,將最不重要的事項(xiàng)放在最下方,需要的朋友可以參考下
    2024-01-01
  • 網(wǎng)頁和瀏覽器兼容性問題匯總(draft1)

    網(wǎng)頁和瀏覽器兼容性問題匯總(draft1)

    由于IE擴(kuò)展了許多私有的DOM、CSS等導(dǎo)致許多網(wǎng)頁的開發(fā)者都根據(jù)IE開發(fā),才導(dǎo)致許多網(wǎng)頁的不規(guī)范,從而導(dǎo)致現(xiàn)在的瀏覽器瀏覽相同網(wǎng)頁效果不盡相同。
    2009-06-06
  • 分享10個(gè)原生JavaScript技巧

    分享10個(gè)原生JavaScript技巧

    本文給大家分享的是個(gè)人總結(jié)的10條非常常用的原生javascript的小技巧,都是平時(shí)項(xiàng)目中用到的,這里總結(jié)記錄下來,有需要的小伙伴可以參考下。
    2015-04-04
  • JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(二十一) JavaScript中的XML

    JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(二十一) JavaScript中的XML

    雖然XML和DOM已經(jīng)變成Web開發(fā)的重要組成部分,但目前僅IE跟Mozilla支持客戶端的XML處理
    2012-09-09
  • JS實(shí)現(xiàn)的數(shù)組全排列輸出算法

    JS實(shí)現(xiàn)的數(shù)組全排列輸出算法

    這篇文章主要介紹了JS實(shí)現(xiàn)的數(shù)組全排列輸出算法,實(shí)例分析了全排列的原理與相關(guān)的javascript實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • html+javascript+bootstrap實(shí)現(xiàn)層級(jí)多選框全層全選和多選功能

    html+javascript+bootstrap實(shí)現(xiàn)層級(jí)多選框全層全選和多選功能

    想做一個(gè)先按層級(jí)排序并可以多選的功能,首先傾向于用多層標(biāo)簽式的,直接選定加在文本域里,接下來通過本文給大家介紹html+javascript+bootstrap實(shí)現(xiàn)層級(jí)多選框全層全選和多選功能,需要的朋友參考下
    2017-03-03
  • JS實(shí)現(xiàn)輪播圖效果的3種簡(jiǎn)單方法

    JS實(shí)現(xiàn)輪播圖效果的3種簡(jiǎn)單方法

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)輪播圖效果的3種簡(jiǎn)單方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論