语音识别这个功能属于高级功能,必须微信实名认证后才能实现,认证费用300元/年,如果你作为开发者可以申请测试帐号,也是可以的。首先建立一个微信消息类,这个类比之前多了一个属性。
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; }
}
语音识别是微信自带的功能,非常强大无需我们做过多的操作:
protected void page_load(object sender, eventargs e)
{
wxmessage wx = getwxmessage();
string res = "";
if (!string.isnullorempty(wx.eventname) && wx.eventname.trim() == "subscribe")
{//刚关注时的时间,用于欢迎词
string content = "";
content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";
res = sendtextmessage(wx, content);
}
else
{
if (wx.msgtype == "text" && wx.content == "你好")
{
res = sendtextmessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
}
else if (wx.msgtype == "voice")//识别消息类型为语音
{
res = sendtextmessage(wx, wx.recognition);//wx.recognition就是语音识别的结果了,我们直接引用,以文本形式反馈就ok了
}
else
{
res = sendtextmessage(wx, "你好,未能识别消息!");
}
}
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")//如果是语音消息的话就把识别结果赋值给实体类的相应属性recognition
{
wx.recognition = xml.selectsinglenode("xml").selectsinglenode("recognition").innertext;
}
return wx;
}
///
/// 发送文字消息
///
/// 获取的收发者信息
/// 内容
///
private string sendtextmessage(wxmessage wx, string content)
{
string res = string.format(@" ",
wx.fromusername, wx.tousername, datetime.now, content);
return res;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,
一念天堂一念地狱30784311