resend_lost_message.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. from datetime import datetime
  5. import re
  6. from pqai_agent import configs
  7. from pqai_agent.mq_message import MessageChannel, MqMessage, MessageType
  8. from pqai_agent.message_queue_backend import AliyunRocketMQQueueBackend
  9. from pqai_agent.user_manager import MySQLUserRelationManager
  10. config = configs.get()
  11. def main():
  12. wecom_db_config = config['storage']['user_relation']
  13. user_db_config = config['storage']['user']
  14. user_relation_manager = MySQLUserRelationManager(
  15. user_db_config['mysql'], wecom_db_config['mysql'],
  16. config['storage']['staff']['table'],
  17. user_db_config['table'],
  18. wecom_db_config['table']['staff'],
  19. wecom_db_config['table']['relation'],
  20. wecom_db_config['table']['user']
  21. )
  22. send_queue = AliyunRocketMQQueueBackend(
  23. config['mq']['endpoints'],
  24. config['mq']['instance_id'],
  25. config['mq']['send_topic'],
  26. has_consumer=False, has_producer=True
  27. )
  28. message_type_map = {
  29. 'MessageType.TEXT': MessageType.TEXT,
  30. 'MessageType.VOICE': MessageType.VOICE,
  31. }
  32. """
  33. log格式
  34. 2025-05-02 07:17:15,869 - agent _send_response[200] - WARNING - staff[1688857241615085] user[7881299501048462]: response[MessageType.TEXT] 早上好呀!感谢您的祝福~您这发送祝福信息的爱好真不错,您一般都喜欢给哪些人发祝福呀?
  35. 2025-05-02 07:17:15,949 - agent _send_response[209] - WARNING - staff[1688857241615085] user[7881299501048462]: skip reply
  36. """
  37. # 从后往前读取指定的日志文件在2025-05-07 07:00:00后的日志,解析内容为skip reply的日志,找到其前一条同一staff和user的response日志,解析其MessageType、response内容、timestamp
  38. # 查询userid的tags,如果包含"04W4-AA-1", "04W4-AA-2", "04W4-AA-3", "04W4-AA-4"其中之一,则将response组装为Message,放入发送队列
  39. # 记录发送的userid,如果同一用户已经发送过,则不再发送
  40. # 发送所需代码为:
  41. # self.send_queue.produce(
  42. # Message.build(message_type, MessageChannel.CORP_WECHAT,
  43. # staff_id, user_id, response, current_ts)
  44. # )
  45. log_name = '/var/log/agent_service/service.log'
  46. processed_users = set()
  47. target_tags = {"04W4-AA-1", "04W4-AA-2", "04W4-AA-3", "04W4-AA-4"}
  48. cutoff_time = datetime.strptime("2025-05-07 07:35:00", "%Y-%m-%d %H:%M:%S")
  49. with open(log_name, "r", encoding="utf-8") as log_file:
  50. logs = log_file.readlines()[::-1] # Reverse the logs for backward processing
  51. for i, log in enumerate(logs):
  52. if ": response[" in log:
  53. match = re.search(r"staff\[(\d+)\] user\[(\d+)\]", log)
  54. if not match:
  55. continue
  56. staff_id, user_id = match.groups()
  57. processed_users.add(user_id)
  58. elif "skip reply" in log:
  59. match = re.search(r"staff\[(\d+)\] user\[(\d+)\]", log)
  60. if not match:
  61. continue
  62. staff_id, user_id = match.groups()
  63. # Find the preceding response log
  64. for prev_log in logs[i + 1:]:
  65. if f"staff[{staff_id}] user[{user_id}]: response[" in prev_log:
  66. response_match = re.search(
  67. r": response\[(.*?)\] (.*)", prev_log
  68. )
  69. if not response_match:
  70. break
  71. message_type, response = response_match.groups()
  72. message_type = message_type_map[message_type]
  73. timestamp_match = re.search(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})", prev_log)
  74. if not timestamp_match:
  75. break
  76. timestamp = datetime.strptime(timestamp_match.group(1), "%Y-%m-%d %H:%M:%S")
  77. if timestamp <= cutoff_time:
  78. break
  79. # Query user tags
  80. user_tags = set(user_relation_manager.get_user_tags(user_id))
  81. if not target_tags.intersection(user_tags):
  82. break
  83. # Check if user has already been processed
  84. if user_id in processed_users:
  85. break
  86. message = MqMessage.build(message_type, MessageChannel.CORP_WECHAT,
  87. staff_id, user_id, response, int(timestamp.timestamp() * 1000))
  88. print(message)
  89. # Send the message
  90. send_queue.produce(message)
  91. processed_users.add(user_id)
  92. break
  93. if __name__ == '__main__':
  94. main()