共计 2155 个字符,预计需要花费 6 分钟才能阅读完成。
-
接收样例
<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 数据包如下
<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>
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
正文完
星哥玩云-微信公众号