接收样例
代码语言:javascript复制<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>
注意:测试平台需要开启语音识别
开通语音识别后,用户每次发送语音给公众号时,微信会在推送的语音消息XML数据包中,增加一个Recongnition字段(注:由于客户端缓存,开发者开启或者关闭语音识别功能,对新关注者立刻生效,对已关注用户需要24小时生效。开发者可以重新关注此帐号进行测试)。开启语音识别后的语音XML数据包如下
代码语言:javascript复制<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编码
回复样例
代码语言:javascript复制<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[media_id]]></MediaId>
</Voice>
</xml>
代码语言:javascript复制<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, HttpResponse
<span class="hljs-keyword">from</span> django.views.decorators.csrf <span class="hljs-keyword">import</span> csrf_exempt
<span class="hljs-keyword">import</span> hashlib
<span class="hljs-keyword">import</span> xmltodict
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">from</span> myApp.accessToken <span class="hljs-keyword">import</span> AccessToken
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">responseXML</span><span class="hljs-params">(ToUserName, FromUserName, MsgType, **kwargs)</span>:</span>
resDict = {
<span class="hljs-string">"ToUserName"</span>: ToUserName,
<span class="hljs-string">"FromUserName"</span>: FromUserName,
<span class="hljs-string">"CreateTime"</span>: int(time.time()),
<span class="hljs-string">"MsgType"</span>: MsgType,
}
<span class="hljs-keyword">if</span> MsgType == <span class="hljs-string">"text"</span>:
resDict[<span class="hljs-string">"Content"</span>] = kwargs.get(<span class="hljs-string">"Content"</span>)
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"image"</span>:
resDict[<span class="hljs-string">"Image"</span>] = {<span class="hljs-string">"MediaId"</span>: kwargs.get(<span class="hljs-string">"MediaId"</span>)}
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"voice"</span>:
resDict[<span class="hljs-string">"Voice"</span>] = {<span class="hljs-string">"MediaId"</span>: kwargs.get(<span class="hljs-string">"MediaId"</span>)}
resXml = xmltodict.unparse({<span class="hljs-string">"xml"</span>: resDict})
<span class="hljs-keyword">return</span> resXml
<span class="hljs-meta">@csrf_exempt</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">weixin</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">if</span> MsgType == <span class="hljs-string">"text"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"event"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"image"</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"voice"</span>:
Format = reqDict.get(<span class="hljs-string">"Format"</span>)
<span class="hljs-comment"># 接收到的语音转换为文字</span>
Recognition = reqDict.get(<span class="hljs-string">"Recognition"</span>)
MediaId = reqDict.get(<span class="hljs-string">"MediaId"</span>)
MsgId = reqDict.get(<span class="hljs-string">"MsgId"</span>)
resXml = responseXML(FromUserName, ToUserName, <span class="hljs-string">"voice"</span>, MediaId=MediaId)
<span class="hljs-comment"># resXml = responseXML(FromUserName, ToUserName, "text", Content=Recognition)</span>
<span class="hljs-keyword">return</span> HttpResponse(resXml)
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">pass</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">access</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">pass</span>