message.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. from enum import Enum, auto
  5. from typing import Optional
  6. from pydantic import BaseModel
  7. class MessageType(Enum):
  8. DEFAULT = (-1, "未分类的消息")
  9. TEXT = (1, "文本")
  10. VOICE = (2, "语音")
  11. GIF = (3, "GIF")
  12. IMAGE_GW = (4, "个微图片")
  13. IMAGE_QW = (5, "企微图片")
  14. MINI_PROGRAM = (6, "小程序")
  15. LINK = (7, "链接")
  16. SHI_PIN_HAO = (8, "视频号")
  17. NAME_CARD = (9, "名片")
  18. POSITION = (10, "位置")
  19. RED_PACKET = (11, "红包")
  20. FILE_GW = (12, "个微文件")
  21. FILE_QW = (13, "企微文件")
  22. VIDEO_GW = (14, "个微视频")
  23. VIDEO_QW = (15, "企微视频")
  24. AGGREGATION_MSG = (16, "聚合消息")
  25. ACTIVE_TRIGGER = (101, "主动触发器")
  26. AGGREGATION_TRIGGER = (102, "消息聚合触发器")
  27. def __init__(self, code, description):
  28. self.code = code
  29. self.description = description
  30. def __repr__(self):
  31. return f"{self.__class__.__name__}.{self.name}"
  32. class MessageChannel(Enum):
  33. CORP_WECHAT = (1, "企业微信")
  34. MINI_PROGRAM = (2, "小程序")
  35. SYSTEM = (101, "系统内部")
  36. def __init__(self, code, description):
  37. self.code = code
  38. self.description = description
  39. def __repr__(self):
  40. return f"{self.__class__.__name__}.{self.name}"
  41. class Message(BaseModel):
  42. id: int
  43. type: MessageType
  44. channel: MessageChannel
  45. sender: Optional[str] = None
  46. receiver: str
  47. content: Optional[str] = None
  48. timestamp: int
  49. ref_msg_id: Optional[int] = None
  50. @staticmethod
  51. def build(type, channel, sender, receiver, content, timestamp):
  52. return Message(
  53. id=0,
  54. type=type,
  55. channel=channel,
  56. sender=sender,
  57. receiver=receiver,
  58. content=content,
  59. timestamp=timestamp
  60. )