Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

87 lines
2.5KB

  1. """
  2. 本类表示聊天消息,用于对itchat和wechaty的消息进行统一的封装。
  3. 填好必填项(群聊6个,非群聊8个),即可接入ChatChannel,并支持插件,参考TerminalChannel
  4. ChatMessage
  5. msg_id: 消息id (必填)
  6. create_time: 消息创建时间
  7. ctype: 消息类型 : ContextType (必填)
  8. content: 消息内容, 如果是声音/图片,这里是文件路径 (必填)
  9. from_user_id: 发送者id (必填)
  10. from_user_nickname: 发送者昵称
  11. to_user_id: 接收者id (必填)
  12. to_user_nickname: 接收者昵称
  13. other_user_id: 对方的id,如果你是发送者,那这个就是接收者id,如果你是接收者,那这个就是发送者id,如果是群消息,那这一直是群id (必填)
  14. other_user_nickname: 同上
  15. is_group: 是否是群消息 (群聊必填)
  16. is_at: 是否被at
  17. - (群消息时,一般会存在实际发送者,是群内某个成员的id和昵称,下列项仅在群消息时存在)
  18. actual_user_id: 实际发送者id (群聊必填)
  19. actual_user_nickname:实际发送者昵称
  20. _prepare_fn: 准备函数,用于准备消息的内容,比如下载图片等,
  21. _prepared: 是否已经调用过准备函数
  22. _rawmsg: 原始消息对象
  23. """
  24. class ChatMessage(object):
  25. msg_id = None
  26. create_time = None
  27. ctype = None
  28. content = None
  29. from_user_id = None
  30. from_user_nickname = None
  31. to_user_id = None
  32. to_user_nickname = None
  33. other_user_id = None
  34. other_user_nickname = None
  35. my_msg = False
  36. is_group = False
  37. is_at = False
  38. actual_user_id = None
  39. actual_user_nickname = None
  40. _prepare_fn = None
  41. _prepared = False
  42. _rawmsg = None
  43. def __init__(self, _rawmsg):
  44. self._rawmsg = _rawmsg
  45. def prepare(self):
  46. if self._prepare_fn and not self._prepared:
  47. self._prepared = True
  48. self._prepare_fn()
  49. def __str__(self):
  50. return "ChatMessage: id={}, create_time={}, ctype={}, content={}, from_user_id={}, from_user_nickname={}, to_user_id={}, to_user_nickname={}, other_user_id={}, other_user_nickname={}, is_group={}, is_at={}, actual_user_id={}, actual_user_nickname={}".format(
  51. self.msg_id,
  52. self.create_time,
  53. self.ctype,
  54. self.content,
  55. self.from_user_id,
  56. self.from_user_nickname,
  57. self.to_user_id,
  58. self.to_user_nickname,
  59. self.other_user_id,
  60. self.other_user_nickname,
  61. self.is_group,
  62. self.is_at,
  63. self.actual_user_id,
  64. self.actual_user_nickname,
  65. )