Speech recognition is an advanced feature that can only be implemented after real-name authentication. The authentication fee is RMB 300/year. If you are a developer, you can apply for a test account. To implement this function, we need to use the Accesstoken mentioned in chapter 2. First, we need to create a message class. This analogy adds an attribute.
class wxmessage { public string FromUserName { get; set; } public string ToUserName { get; set; } public string MsgType { get; set; } public string EventName { get; set; } public string Content { get; set; } public string Recognition { get; set; } public string EventKey { get; set; } }
Speech recognition is a built-in function that is very powerful and requires no additional operations:
Protected void Page_Load (object sender, EventArgs e) {wxmessage wx = GetWxMessage (); string res = ""; if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "subscribe") {// The time when you just followed, used for the welcome word string content = ""; content = "/: rose welcomes Beijing Yongjie youxin Technology Co., Ltd./: rose \ n to directly reply "hello"; res = sendTextMessage (wx, content);} else {if (wx. msgType = "text" & wx. content = "hello") {res = sendTextMessage (wx, "Hello, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else if (wx. msgType = "voice") // recognize the message type as voice {res = sendTextMessage (wx, wx. recognition); // wx. recognition is the result of speech Recognition. We can directly reference it and provide feedback in the form of text.} else {res = sendTextMessage (wx, "Hello, failed to recognize the message! ") ;}} Response. write (res);} private wxmessage GetWxMessage () {wxmessage wx = new wxmessage (); StreamReader str = new StreamReader (Request. inputStream, System. text. encoding. UTF8); XmlDocument xml = new XmlDocument (); xml. load (str); wx. toUserName = xml. selectSingleNode ("xml "). selectSingleNode ("ToUserName "). innerText; wx. fromUserName = xml. selectSingleNode ("xml "). selectSingleNode ("FromUserName "). innerText; wx. msgType = xml. selectSingleNode ("xml "). selectSingleNode ("MsgType "). innerText; if (wx. msgType. trim () = "text") {wx. content = xml. selectSingleNode ("xml "). selectSingleNode ("Content "). innerText;} if (wx. msgType. trim () = "event") {wx. eventName = xml. selectSingleNode ("xml "). selectSingleNode ("Event "). innerText;} if (wx. msgType. trim () = "voice") // assign the Recognition result to the corresponding attribute Recognition {wx. recognition = xml. selectSingleNode ("xml "). selectSingleNode ("Recognition "). innerText;} return wx ;}