WeChat public number material management

Keywords: JSON xml curl Python

1, There are two ways to upload permanent picture materials:

1. direct uploading of public number material management

2. Send POST request

Use the curl command to upload an example:

curl -F media=@pro_nlp.jpg "http://file.api.weixin.qq.com/cgi-bin/material/add_material?access_token=14_3-OW6zGI-yD1LzYlhovytEU81rqH6gDIrYimb4xb_Jriwizb_5XPiaqP7PwCPrMICJ6COq0LS5Rrjxbm0NXpd_C3vhQWrbVMT_iniCvmMwPDfdO1Ekc0pFdlf78PHTgAEAEWI"

Use Python steps to upload an example:

# -*- coding: utf-8 -*-
# filename: media.py
#from basic import Basic
import urllib2
import poster.encode
from poster.streaminghttp import register_openers

class Media(object):
    def __init__(self):
        register_openers()
    #Upload pictures
    def uplaod(self, accessToken, filePath, mediaType):
        openFile = open(filePath, "rb")
        param = {'media': openFile}
        postData, postHeaders = poster.encode.multipart_encode(param)

        #postUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=" + mediaType
        postUrl = "http://file.api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=" + mediaType
        request = urllib2.Request(postUrl, postData, postHeaders)
        urlResp = urllib2.urlopen(request)
        print(urlResp.read())

if __name__ == '__main__':
    myMedia = Media()
    accessToken = "14_s9rzbBYHo-eI-hM10l4PuoPp2KSW7obCG3yhFpUn6jhxJlo_d-HC37c8NqUD8Ydfy1mnhD0nNCOgMPLwMb5sIsk6PlhAGatPO6pPhr3mvfOwHH8RgFJ7zCq_iZHb-Kubt8xWqzwn2hK4rQ5pBVPiAAAHBV"
    filePath = "/home/ytkj/temp/pro_nlp.jpg"   #Please fill in
    mediaType = "image"
    myMedia.uplaod(accessToken, filePath, mediaType)

 

2, Get the MediaID of the material

1. When uploading, you can get the media aid of the material directly

2. Use the interface to get the list of materials

# -*- coding: utf-8 -*-
# filename: material.py
import urllib2
import json
import poster.encode
from poster.streaminghttp import register_openers

class Material(object):
    def __init__(self):
        register_openers()
    #upload
    def uplaod(self, accessToken, filePath, mediaType):
        openFile = open(filePath, "rb")
        fileName = "hello"
        param = {'media': openFile, 'filename': fileName}
        #param = {'media': openFile}
        postData, postHeaders = poster.encode.multipart_encode(param)

        postUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (accessToken, mediaType)
        request = urllib2.Request(postUrl, postData, postHeaders)
        urlResp = urllib2.urlopen(request)
        print urlResp.read()
    #download
    def get(self, accessToken, mediaId):
        postUrl = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s" % accessToken
        postData = "{ \"media_id\": \"%s\" }" % mediaId
        urlResp = urllib2.urlopen(postUrl, postData)
        headers = urlResp.info().__dict__['headers']
        if ('Content-Type: application/json\r\n' in headers) or ('Content-Type: text/plain\r\n' in headers):
            jsonDict = json.loads(urlResp.read())
            print jsonDict
        else:
            buffer = urlResp.read()  # Binary of material
            mediaFile = file("test_media.jpg", "wb")
            mediaFile.write(buffer)
            print "get successful"
    #delete
    def delete(self, accessToken, mediaId):
        postUrl = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s" % accessToken
        postData = "{ \"media_id\": \"%s\" }" % mediaId
        urlResp = urllib2.urlopen(postUrl, postData)
        print urlResp.read()

    #Get material list
    def batch_get(self, accessToken, mediaType, offset=0, count=20):
        postUrl = ("https://api.weixin.qq.com/cgi-bin/material"
               "/batchget_material?access_token=%s" % accessToken)
        postData = ("{ \"type\": \"%s\", \"offset\": %d, \"count\": %d }"
                    % (mediaType, offset, count))
        urlResp = urllib2.urlopen(postUrl, postData)
        print urlResp.read()

if __name__ == '__main__':
    myMaterial = Material()
    accessToken = "14_-GQ-SnXSuyd-LZ0Q-iy0y4-Xlm0rGtwaRjf5C6UIjMGWqeQD1gM9lC8OL1ArLoT9fr59-ff1pZcj_5E9uUXbFY77cGiVvPYq6RoZMWeZpjEPqIW5cwaT5YVgkscaV9lTYpIcd8Ocla2MJiBfPOUaAEABAG"
    mediaType = "image"
    myMaterial.batch_get(accessToken, mediaType)

 

Three, public number reply message format

1. text

< XML > < tousername > <! [CDATA [touser]] >

2. pictures

<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[media_id]]></MediaId></Image></xml>

 

3. For other references, please note that there are errors in the wechat developer's documents, and there are more spaces in the reply message format

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543

 

 

Posted by mator on Sat, 21 Dec 2019 11:09:11 -0800