Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

51 lines
1.7KB

  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
  29. elif self.msg_type == "voice":
  30. self.ctype = ContextType.TEXT
  31. self.content = xmlData.find("Recognition").text # 接收语音识别结果
  32. # other voice_to_text method not implemented yet
  33. if self.content == None:
  34. self.content = "你好"
  35. elif self.msg_type == "image":
  36. # not implemented yet
  37. self.pic_url = xmlData.find("PicUrl").text
  38. self.media_id = xmlData.find("MediaId").text
  39. elif self.msg_type == "event":
  40. self.content = xmlData.find("Event").text
  41. else: # video, shortvideo, location, link
  42. # not implemented
  43. pass