chat_message.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. self_display_name: 自身的展示名,设置群昵称时,该字段表示群昵称
  21. _prepare_fn: 准备函数,用于准备消息的内容,比如下载图片等,
  22. _prepared: 是否已经调用过准备函数
  23. _rawmsg: 原始消息对象
  24. """
  25. class ChatMessage(object):
  26. msg_id = None
  27. create_time = None
  28. ctype = None
  29. content = None
  30. from_user_id = None
  31. from_user_nickname = None
  32. to_user_id = None
  33. to_user_nickname = None
  34. other_user_id = None
  35. other_user_nickname = None
  36. my_msg = False
  37. self_display_name = None
  38. is_group = False
  39. is_at = False
  40. actual_user_id = None
  41. actual_user_nickname = None
  42. at_list = None
  43. _prepare_fn = None
  44. _prepared = False
  45. _rawmsg = None
  46. def __init__(self, _rawmsg):
  47. self._rawmsg = _rawmsg
  48. def prepare(self):
  49. if self._prepare_fn and not self._prepared:
  50. self._prepared = True
  51. self._prepare_fn()
  52. def __str__(self):
  53. 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={}, at_list={}".format(
  54. self.msg_id,
  55. self.create_time,
  56. self.ctype,
  57. self.content,
  58. self.from_user_id,
  59. self.from_user_nickname,
  60. self.to_user_id,
  61. self.to_user_nickname,
  62. self.other_user_id,
  63. self.other_user_nickname,
  64. self.is_group,
  65. self.is_at,
  66. self.actual_user_id,
  67. self.actual_user_nickname,
  68. self.at_list
  69. )