前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信公众号-图片消息

微信公众号-图片消息

作者头像
星哥玩云
发布2022-09-14 18:23:15
1.5K0
发布2022-09-14 18:23:15
举报
文章被收录于专栏:开源部署

接收样例

代码语言: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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档