阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

微信公众号-语音消息

115次阅读
没有评论

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

微信公众号 - 语音消息

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-05-26发表,共计2155字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中