resend_lost_message.py 4.6 KB

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