message.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. TEXT = (1, "文本")
  9. AUDIO = (2, "音频")
  10. IMAGE = (3, "图片")
  11. VIDEO = (4, "视频")
  12. MINI_PROGRAM = (5, "小程序")
  13. LINK = (6, "链接")
  14. ACTIVE_TRIGGER = (101, "主动触发器")
  15. AGGREGATION_TRIGGER = (102, "消息聚合触发器")
  16. def __init__(self, code, description):
  17. self.code = code
  18. self.description = description
  19. def __repr__(self):
  20. return f"{self.__class__.__name__}.{self.name}"
  21. class MessageChannel(Enum):
  22. CORP_WECHAT = (1, "企业微信")
  23. MINI_PROGRAM = (2, "小程序")
  24. SYSTEM = (101, "系统内部")
  25. def __init__(self, code, description):
  26. self.code = code
  27. self.description = description
  28. def __repr__(self):
  29. return f"{self.__class__.__name__}.{self.name}"
  30. class Message(BaseModel):
  31. id: int
  32. type: MessageType
  33. channel: MessageChannel
  34. staff_id: Optional[str] = None
  35. user_id: str
  36. content: Optional[str] = None
  37. timestamp: int
  38. @staticmethod
  39. def build(type, channel, staff_id, user_id, content, timestamp):
  40. return Message(
  41. id=0,
  42. type=type,
  43. channel=channel,
  44. staff_id=staff_id,
  45. user_id=user_id,
  46. content=content,
  47. timestamp=timestamp
  48. )