


We know that WeChat was originally designed for voice chat, which made it more popular. Therefore, voice recognition processing has naturally become an important way for WeChat communication. WeChat’s development interface also provides processing of voice message requests. . This article mainly introduces how to use speech recognition to process the entire event chain of the WeChat portal application developed in C#, making it more convenient and diversified to process user input in our WeChat account.
1. Definition of WeChat voice interface 0
WeChat’s API defines voice recognition in this way: When the voice recognition function is enabled, every time the user sends a voice to the official account, WeChat will In the pushed voice message XML packet, add a Recongnition field .
The voice message format is as follows.
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1357290913</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <MediaId><![CDATA[media_id]]></MediaId> <Format><![CDATA[Format]]></Format> <MsgId>1234567890123456</MsgId> </xml>
Parameters | Description |
---|---|
ToUserName | Development人微信 |
FromUserName | Sender account (an OpenID) |
CreateTime | Message creation time (integer) |
MsgType | Voice is voice |
MediaId | Voice Message media id, you can call the multimedia file download interface to pull data. |
Format | Voice format, such as amr, speex, etc. |
MsgID | Message id, 64 Bit integer |
根據(jù)以上微信接口的定義,我們可以定義一個(gè)實(shí)體類(lèi)來(lái)對(duì)消息的傳遞進(jìn)行處理,如下所示。
/// <summary> /// 接收的語(yǔ)音消息 /// </summary> [System.Xml.Serialization.XmlRoot(ElementName = "xml")] public class RequestVoice : BaseMessage { public RequestVoice() { this.MsgType = RequestMsgType.Voice.ToString().ToLower(); } /// <summary> /// 語(yǔ)音格式,如amr,speex等 /// </summary> public string Format { get; set; } /// <summary> /// 語(yǔ)音消息媒體id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)。 /// </summary> public string MediaId { get; set; } /// <summary> /// 消息ID /// </summary> public Int64 MsgId { get; set; } /// <summary> /// 語(yǔ)音識(shí)別結(jié)果,UTF8編碼 /// </summary> public string Recognition { get; set; } }
我們看到,這里我們最感興趣的是語(yǔ)音的識(shí)別結(jié)果,也就是Recognition的字段,這個(gè)就是微信服務(wù)器自動(dòng)根據(jù)用戶(hù)的語(yǔ)音轉(zhuǎn)換過(guò)來(lái)的內(nèi)容,我測(cè)試過(guò),識(shí)別率還是非常高的。
這個(gè)實(shí)體類(lèi),在整個(gè)微信應(yīng)用的消息傳遞中的關(guān)系如下所示:
2、語(yǔ)音的處理操作
明確了上面的語(yǔ)音對(duì)象實(shí)體,我們就可以看看它們之間是如何處理的。
微信消息的處理邏輯如下圖所示。
其中我們來(lái)看看語(yǔ)音的處理操作,我的代碼處理邏輯如下所示。
/// <summary> /// 對(duì)語(yǔ)音請(qǐng)求信息進(jìn)行處理 /// </summary> /// <param name="info">語(yǔ)音請(qǐng)求信息實(shí)體</param> /// <returns></returns> public string HandleVoice(Entity.RequestVoice info) { string xml = ""; // 開(kāi)通語(yǔ)音識(shí)別功能,用戶(hù)每次發(fā)送語(yǔ)音給公眾號(hào)時(shí), // 微信會(huì)在推送的語(yǔ)音消息XML數(shù)據(jù)包中,增加一個(gè)Recongnition字段。 if (!string.IsNullOrEmpty(info.Recognition)) { TextDispatch dispatch = new TextDispatch(); xml = dispatch.HandleVoiceText(info, info.Recognition); } else { xml = ""; } return xml; }
在這里,我先看看,是否獲得了微信的語(yǔ)音識(shí)別結(jié)果,如果獲得,那么這個(gè)時(shí)候,就是和處理用戶(hù)文本輸入的操作差不多了,因此把它轉(zhuǎn)給TextDispatch的處理類(lèi)進(jìn)行處理。
其中這里面的處理邏輯如下所示。
首先我根據(jù)識(shí)別結(jié)果,尋找是否用戶(hù)讀出了微信門(mén)戶(hù)的菜單名稱(chēng),如果根據(jù)語(yǔ)音結(jié)果找到對(duì)應(yīng)的菜單記錄,那么我們執(zhí)行菜單事件(如果是URL的View類(lèi)型菜單,我們沒(méi)辦法重定向到指定的鏈接,因此給出一個(gè)鏈接文本提示,給用戶(hù)單擊進(jìn)入;如果沒(méi)有找到菜單記錄,那么我們就把語(yǔ)音識(shí)別結(jié)果作為一般的事件進(jìn)行處理,如果事件邏輯沒(méi)有處理,那么我們最后給出一個(gè)默認(rèn)的語(yǔ)音應(yīng)答提示結(jié)果就可以了。
具體的處理代碼如下所示。
/// <summary> /// 如果用戶(hù)用語(yǔ)音讀出菜單的內(nèi)容,那么我們應(yīng)該先根據(jù)菜單對(duì)應(yīng)的事件觸發(fā),最后再交給普通事件處理 /// </summary> /// <param name="info"></param> /// <returns></returns> public string HandleVoiceText(BaseMessage info, string voiceText) { string xml = ""; MenuInfo menuInfo = BLLFactory<Menu>.Instance.FindByName(voiceText); if (menuInfo != null) { #region 如果找到菜單對(duì)象的處理 if (menuInfo.Type == "click") { //模擬單擊事件 RequestEventClick eventInfo = new RequestEventClick(); eventInfo.CreateTime = info.CreateTime; eventInfo.EventKey = menuInfo.Key; eventInfo.FromUserName = info.FromUserName; eventInfo.ToUserName = info.ToUserName; xml = base.DealEvent(eventInfo, eventInfo.EventKey); } else { //由于無(wú)法自動(dòng)切換到連接, //轉(zhuǎn)換為連接文本供用戶(hù)進(jìn)入 string content = string.Format("請(qǐng)單擊鏈接進(jìn)入<a href=\"{0}\">{1}</a> ", menuInfo.Url, menuInfo.Name); ResponseText textInfo = new ResponseText(info); textInfo.Content = content; xml = textInfo.ToXml(); } #endregion } else { //交給事件機(jī)制處理 if (string.IsNullOrEmpty(xml)) { xml = HandleText(info, voiceText); } } //最后如果沒(méi)有處理到,那么提示用戶(hù)的語(yǔ)音內(nèi)容 if (string.IsNullOrEmpty(xml)) { ResponseText textInfo = new ResponseText(info); textInfo.Content = string.Format("非常抱歉,您輸入的語(yǔ)音內(nèi)容沒(méi)有找到對(duì)應(yīng)的處理方式。您的語(yǔ)音內(nèi)容為:{0}", voiceText); xml = textInfo.ToXml(); } return xml; }
微信門(mén)戶(hù)測(cè)試界面效果如下所示。
? ? ? ? ? ? ? ? ?
? ?
?
為了方便對(duì)客戶(hù)會(huì)話的記錄,我的微信門(mén)戶(hù)后臺(tái),會(huì)記錄用戶(hù)的語(yǔ)音輸入內(nèi)容,如下所示。
?當(dāng)然,微信后臺(tái)的管理界面,也能夠查到相應(yīng)的語(yǔ)音記錄,界面如下所示。
以上就是我對(duì)微信語(yǔ)音的消息定義和事件處理的邏輯,其實(shí)語(yǔ)音是一個(gè)重要的輸入,如果正確的識(shí)別內(nèi)容,比手工輸入的效果更好,給用戶(hù)提供另外一種高效的輸入和事件處理操作。
這樣的處理模式,能夠使得我們整個(gè)微信門(mén)戶(hù)框架,不管是對(duì)于用戶(hù)的語(yǔ)音輸入,還是文本輸入,還是菜單事件的處理,都可以融為一體,實(shí)現(xiàn)更加完美的銜接。
更多C# develops WeChat portals and applications using voice processing相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

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)