当用户关注和取消关注时MsgType的类型均为event
,我们可以在此时进行一些业务处理
接收样例:
代码语言:javascript复制<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</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>
<span class="hljs-keyword">pass</span>
<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>:
Event = reqDict.get(<span class="hljs-string">"Event"</span>)
<span class="hljs-keyword">if</span> Event == <span class="hljs-string">"subscribe"</span>:
<span class="hljs-comment"># 业务逻辑</span>
resXml = responseXML(FromUserName, ToUserName, <span class="hljs-string">"text"</span>, Content=<span class="hljs-string">"欢迎光临红浪漫,三楼男宾一位"</span>)
<span class="hljs-keyword">return</span> HttpResponse(resXml)
<span class="hljs-keyword">elif</span> Event == <span class="hljs-string">"unsubscribe"</span>:
<span class="hljs-comment"># 业务逻辑</span>
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">""</span>)
<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>