微信公众号-图片消息

2022-09-14 18:23:15 浏览数 (2)

接收样例

代码语言:javascript复制
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]]></PicUrl>
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>

回复样例

代码语言:javascript复制
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[media_id]]></MediaId>
</Image>
</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>
    resDict = {
        <span class="hljs-string">"ToUserName"</span>: ToUserName,
        <span class="hljs-string">"FromUserName"</span>: FromUserName,
        <span class="hljs-string">"CreateTime"</span>: int(time.time()),
        <span class="hljs-string">"MsgType"</span>: MsgType,
    }
    <span class="hljs-keyword">if</span> MsgType == <span class="hljs-string">"text"</span>:
        resDict[<span class="hljs-string">"Content"</span>] = kwargs.get(<span class="hljs-string">"Content"</span>)
    <span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"image"</span>:
        resDict[<span class="hljs-string">"Image"</span>] = {<span class="hljs-string">"MediaId"</span>: kwargs.get(<span class="hljs-string">"MediaId"</span>)}

    resXml = xmltodict.unparse({<span class="hljs-string">"xml"</span>: resDict})
    <span class="hljs-keyword">return</span> resXml

<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>:
            <span class="hljs-keyword">pass</span>
        <span class="hljs-keyword">elif</span> MsgType == <span class="hljs-string">"image"</span>:
            PicUrl = reqDict.get(<span class="hljs-string">"PicUrl"</span>)
            MediaId = reqDict.get(<span class="hljs-string">"MediaId"</span>)
            MsgId = reqDict.get(<span class="hljs-string">"MsgId"</span>)

            resXml = responseXML(FromUserName, ToUserName, <span class="hljs-string">"image"</span>, MediaId=MediaId)
            <span class="hljs-keyword">return</span> HttpResponse(resXml)
        <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>

0 人点赞