微信语音识别和语义理解接口

微信语音识别和语义理解接口

微信功能可谓强大,最近学习了下它的智能接口,顿时觉得微信特别高大上,居然能识别语音而且准确度还是挺高且还能理解语义。这次打算针对这个智能接口演示下通过语音查询天气预报的例子。

一 、 开启语音识别功能




二 、处理语音消息

请注意,开通语音识别后,用户每次发送语音给公众号时,微信会在推送的语音消息XML数据包中,增加一个Recongnition字段(注:由于客户端缓存,开发者开启或者关闭语音识别功能,对新关注者立刻生效,对已关注用户需要24小时生效。开发者可以重新关注此帐号进行测试)。开启语音识别后的语音XML数据包如下:

<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><Recognition><![CDATA[腾讯微信团队]]></Recognition><MsgId>1234567890123456</MsgId></xml>

多出的字段中,Format为语音格式,一般为amr,Recognition为语音识别结果,使用UTF8编码。


以上是微信开发文档的说明,其实就是开启语音识别后多了一个参数


String recognition=requestMap.get("Recognition");


三 、 封装语义理解接口

参照API:http://mp.weixin.qq.com/wiki/0/9b7e44d37062e3f014c0e05017eabf2c.html

public static String getWeatherSemInfo(String accessToken,String reqJson){String requestUrl="https://api.weixin.qq.com/semantic/semproxy/search?access_token=YOUR_ACCESS_TOKEN";requestUrl=requestUrl.replace("YOUR_ACCESS_TOKEN", accessToken);JSONObject json=CommonUtil.httpsRequest(requestUrl, "POST", reqJson);String city="上海";int errcode=json.getInt("errcode");if(0==errcode){JSONObject semantic=json.getJSONObject("semantic");JSONObject details=semantic.getJSONObject("details");JSONObject location=details.getJSONObject("location");city=location.getString("city");}else{System.out.println("语义理解失败");}return city;}

四、 微信识别语音并回复消息

String recognition=requestMap.get("Recognition");            //这里的token建议缓存起来或放数据库定期取最新的(这里就是掩饰下)String token=AdvancedUtil.getAccessToken("xxxxx","xxxxxx").getToken();        SemParams p=new SemParams();p.setAppid("xxxxx");p.setCategory("weather");p.setCity("昆明");p.setQuery(recognition);p.setUid(fromUserName);        String city=AdvancedUtil.getWeatherSemInfo(token,JSONObject.fromObject(p).toString());String weatherInfo=WeatherUtil.getWeatherResult(city);                respContent = weatherInfo;  

顺便贴下SemParams的代码出来

package com.debug.weixin.pojo;public class SemParams {private String query;private String city;private String category;private String appid;private String uid;public String getQuery() {return query;}public void setQuery(String query) {this.query = query;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}public String getAppid() {return appid;}public void setAppid(String appid) {this.appid = appid;}public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}}

来看下运行截图



如果对天气预报的获取有疑问请参考下面的文章:

http://www.voidcn.com/article/p-taaarlty-ob.html


以上程序只是简单的实现了功能,实际开发中情况会比这个复杂,例如要校验空值等各种逻辑判断,因此生产环境下的代码应该更加严谨,控制也应更加细腻。这样才能跻身于微信开发的高手行列。

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部