语音识别这个功能属于高级功能,必须微信实名认证后才能实现,认证费用300元/年,如果你作为开发者可以申请测试帐号,也是可以的。首先建立一个微信消息类,这个类比之前多了一个属性。

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class wxmessage    
  2.   {    
  3.       public string FromUserName { getset; }    
  4.       public string ToUserName { getset; }    
  5.        public string MsgType { getset; }    
  6.        public string EventName { getset; }    
  7.        public string Content { getset; }  
  8.        public string Recognition { getset; }  
  9.        public string EventKey { getset; }   
  10.    }  


语音识别是微信自带的功能,非常强大无需我们做过多的操作:


[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. protected void Page_Load(object sender, EventArgs e)  
  2.      {  
  3.          wxmessage wx = GetWxMessage();  
  4.          string res = "";  
  5.   
  6.          if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")  
  7.          {//刚关注时的时间,用于欢迎词  
  8.              string content = "";  
  9.              content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";  
  10.              res = sendTextMessage(wx, content);  
  11.          }  
  12.          else  
  13.          {  
  14.              if (wx.MsgType == "text" && wx.Content == "你好")  
  15.              {  
  16.                  res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  17.              }  
  18.              else if (wx.MsgType == "voice")//识别消息类型为语音  
  19.              {  
  20.                  res = sendTextMessage(wx, wx.Recognition);//wx.Recognition就是语音识别的结果了,我们直接引用,以文本形式反馈就OK了  
  21.   
  22.              }  
  23.              else  
  24.              {  
  25.                  res = sendTextMessage(wx, "你好,未能识别消息!");  
  26.              }  
  27.          }  
  28.   
  29.          Response.Write(res);  
  30.      }  
  31.   
  32.  private wxmessage GetWxMessage()  
  33.      {  
  34.          wxmessage wx = new wxmessage();  
  35.          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
  36.          XmlDocument xml = new XmlDocument();  
  37.          xml.Load(str);  
  38.          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
  39.          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
  40.          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
  41.          if (wx.MsgType.Trim() == "text")  
  42.          {  
  43.              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
  44.          }  
  45.          if (wx.MsgType.Trim() == "event")  
  46.          {  
  47.              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
  48.          }  
  49.          if (wx.MsgType.Trim() == "voice")//如果是语音消息的话就把识别结果赋值给实体类的相应属性Recognition   
  50.          {  
  51.              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;  
  52.          }  
  53.            
  54.          return wx;  
  55.      }  
  56.   
  57.   
  58.      /// <summary>    
  59.      /// 发送文字消息    
  60.      /// </summary>    
  61.      /// <param name="wx">获取的收发者信息    
  62.      /// <param name="content">内容    
  63.      /// <returns></returns>    
  64.      private string sendTextMessage(wxmessage wx, string content)  
  65.      {  
  66.          string res = string.Format(@" ",  
  67.              wx.FromUserName, wx.ToUserName, DateTime.Now, content);  
  68.          return res;  
  69.      } 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐