SoFunction
Updated on 2024-11-15

python wechat public number development simple process implementation

In this article, we share a simple process of python WeChat public development for your reference, the details are as follows

There are a lot of WeChat public number development tutorials online, but they are years old, and many of them are plagiarized from other people's, and the content is almost exactly the same. Really speechless. Only to summarize the development of some simple process.

First, register a WeChat public number, this will not go into detail.

Second, log in to the background, enter the basic configuration in the development, configure the next server


Fill in the url and token, the url is the address of the server, the token is self-defined

III. Login server development

A lot of online tutorials use what sina sae ah, webpy are a long time ago. Now a lot of things have changed, so I did not use, I use Ali's server as well as flask for the back end.

The code is as follows

# coding:utf-8
from hashlib import sha1
from flask import Flask, request
 
token = 'xxxxxx'
 
app = Flask(__name__)
 
def get_update(token, timestamp, nonce):
 arguments = ''
 for k in sorted([token, timestamp, nonce]):
  arguments = arguments + str(k)
 m = sha1()
 (('utf8'))
 return ()
 
def check_signature():
 signature = ('signature', '')
 timestamp = ('timestamp', '')
 nonce = ('nonce', '')
 check = get_update(token, timestamp, nonce)
 return True if check == signature else False
 
def parse_xml(data):
 try:
  import  as ET
 except ImportError:
  import  as ET
  root = (data)
  datas = '<xml>'
  for child in root:
   if  == 'ToUserName':
    toUser = 
    datas += '<FromUserName>%s</FromUserName>' % toUser
   elif  == 'FromUserName':
    fromUser = 
    datas += '<ToUserName>%s</ToUserName>' % fromUser
   else:
    datas += '<' +  + '>'
    datas += 
    datas += '</' +  + '>'
  datas += '</xml>'
  return datas
 
@('/weixin', methods=['GET', 'POST'])
def weixinInterface():
 if check_signature:
  if  == 'GET':
   echostr = ('echostr', '')
   return echostr
  elif  == 'POST':
   data = 
   msg = parse_xml(data)
   return msg
 else:
  return 'signature error'
 
if __name__ == '__main__':
 (host='0.0.0.0')

In the beginning, then WeChat will let you verify the url filled in, the verification method is through the input timestamp timestamp, random number nonce, token pre-agreed, echostr random string, and the signature signature, you need to according to the timestamp, the random number, the value of the token for the dictionary order, and then encrypted with sha1 to get the signature, check whether the signature is consistent. Check whether the signature is consistent, if so return the random string echostr. If the verification is successful, you can submit, after the submission of the server configuration can be enabled.

The above code also includes the return of information if the user sends a message to the public number, but of course it is only very simple to parse the xml and construct the xml. other more complex functions need to consult the documentation of WeChat.

To this article on the python weibo public development simple process to achieve the article is introduced to this, more related python weibo public development process content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!