45 lines
1.6KB

  1. # -*- coding: utf-8 -*-#
  2. # filename: receive.py
  3. import xml.etree.ElementTree as ET
  4. from bridge.context import ContextType
  5. from channel.chat_message import ChatMessage
  6. from common.log import logger
  7. def parse_xml(web_data):
  8. if len(web_data) == 0:
  9. return None
  10. xmlData = ET.fromstring(web_data)
  11. return WeChatMPMessage(xmlData)
  12. class WeChatMPMessage(ChatMessage):
  13. def __init__(self, xmlData):
  14. super().__init__(xmlData)
  15. self.to_user_id = xmlData.find('ToUserName').text
  16. self.from_user_id = xmlData.find('FromUserName').text
  17. self.create_time = xmlData.find('CreateTime').text
  18. self.msg_type = xmlData.find('MsgType').text
  19. try:
  20. self.msg_id = xmlData.find('MsgId').text
  21. except:
  22. self.msg_id = self.from_user_id+self.create_time
  23. self.is_group = False
  24. # reply to other_user_id
  25. self.other_user_id = self.from_user_id
  26. if self.msg_type == 'text':
  27. self.ctype = ContextType.TEXT
  28. self.content = xmlData.find('Content').text.encode("utf-8")
  29. elif self.msg_type == 'voice':
  30. self.ctype = ContextType.TEXT
  31. self.content = xmlData.find('Recognition').text.encode("utf-8") # 接收语音识别结果
  32. elif self.msg_type == 'image':
  33. # not implemented
  34. self.pic_url = xmlData.find('PicUrl').text
  35. self.media_id = xmlData.find('MediaId').text
  36. elif self.msg_type == 'event':
  37. self.content = xmlData.find('Event').text
  38. else: # video, shortvideo, location, link
  39. # not implemented
  40. pass