mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 22:27:13 +08:00
wechatmp
This commit is contained in:
47
channel/wechatmp/receive.py
Normal file
47
channel/wechatmp/receive.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-#
|
||||
# filename: receive.py
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
def parse_xml(web_data):
|
||||
if len(web_data) == 0:
|
||||
return None
|
||||
xmlData = ET.fromstring(web_data)
|
||||
msg_type = xmlData.find('MsgType').text
|
||||
if msg_type == 'text':
|
||||
return TextMsg(xmlData)
|
||||
elif msg_type == 'image':
|
||||
return ImageMsg(xmlData)
|
||||
elif msg_type == 'event':
|
||||
return Event(xmlData)
|
||||
|
||||
|
||||
class Msg(object):
|
||||
def __init__(self, xmlData):
|
||||
self.ToUserName = xmlData.find('ToUserName').text
|
||||
self.FromUserName = xmlData.find('FromUserName').text
|
||||
self.CreateTime = xmlData.find('CreateTime').text
|
||||
self.MsgType = xmlData.find('MsgType').text
|
||||
self.MsgId = xmlData.find('MsgId').text
|
||||
|
||||
|
||||
class TextMsg(Msg):
|
||||
def __init__(self, xmlData):
|
||||
Msg.__init__(self, xmlData)
|
||||
self.Content = xmlData.find('Content').text.encode("utf-8")
|
||||
|
||||
|
||||
class ImageMsg(Msg):
|
||||
def __init__(self, xmlData):
|
||||
Msg.__init__(self, xmlData)
|
||||
self.PicUrl = xmlData.find('PicUrl').text
|
||||
self.MediaId = xmlData.find('MediaId').text
|
||||
|
||||
|
||||
class Event(object):
|
||||
def __init__(self, xmlData):
|
||||
self.ToUserName = xmlData.find('ToUserName').text
|
||||
self.FromUserName = xmlData.find('FromUserName').text
|
||||
self.CreateTime = xmlData.find('CreateTime').text
|
||||
self.MsgType = xmlData.find('MsgType').text
|
||||
self.Event = xmlData.find('Event').text
|
||||
Reference in New Issue
Block a user