回复用户语音消息识别
from flask import Flask,request
import xmltodict
import time
app = Flask(__name__)
@app.route('/wechat8000',methods=['GET','POST'])
def wechat():
if request.method == 'POST':
xml = request.data
req = xmltodict.parse(xml)['xml']
msg_type = req.get('MsgType')
if 'text' == msg_type:
resp = {
'ToUserName':req.get('FromUserName',''),
'FromUserName':req.get('ToUserName',''),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content':req.get('Content')
}
elif 'voice' == msg_type:
resp = {
'ToUserName':req.get('FromUserName',''),
'FromUserName':req.get('ToUserName',''),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content':req.get('Recognition',u'无法识别')
}
else:
resp = {
'ToUserName':req.get('FromUserName',''),
'FromUserName':req.get('ToUserName',''),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content':'I LOVE ITCAST'
}
xml = xmltodict.unparse({'xml':resp})
return xml
if __name__ == '__main__':
app.run(port=8000,debug=True)