No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

chat_message.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. is_group = False
  36. is_at = False
  37. actual_user_id = None
  38. actual_user_nickname = None
  39. _prepare_fn = None
  40. _prepared = False
  41. _rawmsg = None
  42. def __init__(self,_rawmsg):
  43. self._rawmsg = _rawmsg
  44. def prepare(self):
  45. if self._prepare_fn and not self._prepared:
  46. self._prepared = True
  47. self._prepare_fn()
  48. def __str__(self):
  49. 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(
  50. self.msg_id,
  51. self.create_time,
  52. self.ctype,
  53. self.content,
  54. self.from_user_id,
  55. self.from_user_nickname,
  56. self.to_user_id,
  57. self.to_user_nickname,
  58. self.other_user_id,
  59. self.other_user_nickname,
  60. self.is_group,
  61. self.is_at,
  62. self.actual_user_id,
  63. self.actual_user_nickname,
  64. )