SoFunction
Updated on 2024-12-19

python wechat public number of concern public number auto-reply

We know that once we use the developer mode, we can not use the auto-reply function in the public platform, that is to say, the attention auto-reply function can only be written by yourself.

As shown in the picture, we can't use this feature directly.

So moving on to the last blog, we finished the keyword autoresponder feature thatblog address

The mechanism for implementing autoresponders is:
Once the user gives the public number a trigger (it could be a piece of text, or an image, etc., or it could be a follow), the backend will receive a corresponding XML message, and all we need to do is to parse it and respond to it.

Then if you follow a public number, the public backend will receive an XML message like this.

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>

The parameters are as follows:

Above, then it is easy, we just parse the XML, get MsgType for event, then to determine whether the event is "subscribe" if so, to reply to the message can be.

The key code is as follows: (see previous blog post for specific code)

def parse_xml(web_data):
 if len(web_data) == 0:
  return None
 xmlData = (web_data)
 msg_type = ('MsgType').text
 ...# Omit a part
 elif msg_type == 'event':# Judged as an event message
  #print('event')
  return EventMsg(xmlData)
class EventMsg(Msg):
 def __init__(self, xmlData):
  Event.__init__(self, xmlData)
   = ('Event').text# Take the contents of the Event parameter
if  == 'event':
     #print('yes')
     event = 
     if event == 'subscribe':#Determine if it's a concern and reply
      content = "。。。"
      replyMsg = (toUser, fromUser, content)
      return ()

This is the whole content of this article.