组团学

微信公众号-语音消息

阅读 (356426)
  • 接收样例

    <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>

image20200518144837560.png

注意:测试平台需要开启语音识别

截屏2020051814_50_04.png

开通语音识别后,用户每次发送语音给公众号时,微信会在推送的语音消息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编码

  • 回复样例

    <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <Voice> <MediaId><![CDATA[media_id]]></MediaId> </Voice> </xml>

image20200518145132826.png

from django.shortcuts import render, HttpResponse from django.views.decorators.csrf import csrf_exempt import hashlib import xmltodict import time from myApp.accessToken import AccessToken def index(request): pass def responseXML(ToUserName, FromUserName, MsgType, **kwargs): resDict = { "ToUserName": ToUserName, "FromUserName": FromUserName, "CreateTime": int(time.time()), "MsgType": MsgType, } if MsgType == "text": resDict["Content"] = kwargs.get("Content") elif MsgType == "image": resDict["Image"] = {"MediaId": kwargs.get("MediaId")} elif MsgType == "voice": resDict["Voice"] = {"MediaId": kwargs.get("MediaId")} resXml = xmltodict.unparse({"xml": resDict}) return resXml @csrf_exempt def weixin(request): if request.method == "GET": pass else: pass if MsgType == "text": pass elif MsgType == "event": pass elif MsgType == "image": pass elif MsgType == "voice": Format = reqDict.get("Format") # 接收到的语音转换为文字 Recognition = reqDict.get("Recognition") MediaId = reqDict.get("MediaId") MsgId = reqDict.get("MsgId") resXml = responseXML(FromUserName, ToUserName, "voice", MediaId=MediaId) # resXml = responseXML(FromUserName, ToUserName, "text", Content=Recognition) return HttpResponse(resXml) else: pass def access(request): pass

8a3dabb0b5eff74c27f949bcf47b6bd2.jpg

需要 登录 才可以提问哦