Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

chat_message.py 2.3KB

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