共计 1559 个字符,预计需要花费 4 分钟才能阅读完成。
-
接收样例
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>
-
回复样例
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[你好]]></Content> </xml>
myApp.views.py
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")
resXml = xmltodict.unparse({"xml": resDict})
return resXml
@csrf_exempt
def weixin(request):
if request.method == "GET":
pass
else:
reqXml = request.body
reqDict = xmltodict.parse(reqXml)["xml"]
ToUserName = reqDict.get("ToUserName")
FromUserName = reqDict.get("FromUserName")
CreateTime = reqDict.get("CreateTime")
MsgType = reqDict.get("MsgType")
if MsgType == "text":
Content = reqDict.get("Content")
MsgId = reqDict.get("MsgId")
resXml = responseXML(FromUserName, ToUserName, "text", Content=Content + "!")
return HttpResponse(resXml)
else:
resXml = responseXML(FromUserName, ToUserName, "text", Content="数据类型错误")
return HttpResponse(resXml)
def access(request):
pass
说明:扫描测试平台上的二维码关注测试公众号,第一个 数据类型错误
是因为关注后进入公众号时的问题,后面会讲到关注和取消关注可以解决
正文完
星哥玩云-微信公众号