template_io.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. import os
  3. from utils.config_io import if_mission_done, if_mission_fail, load_config_from_json, load_lora_options, load_animal_options
  4. from utils.logger import logger
  5. # 从文件中加载模板
  6. def load_template(template_name, reply_message_id = None):
  7. template_path = f"template/{template_name}.json"
  8. with open(template_path, 'r', encoding='utf-8') as f:
  9. template = json.load(f)
  10. if template_name in ['choose_card', 'upload_card', 'start_card']:
  11. if template_name == 'choose_card' or template_name == 'upload_card':
  12. index = 3
  13. elif template_name == 'start_card':
  14. index = 4
  15. lora_options= load_animal_options()
  16. if reply_message_id:
  17. config_data = load_config_from_json(reply_message_id)
  18. template['elements'][index]['actions'][0]['initial_option'] = config_data['action']
  19. template['elements'][index]['actions'][1]['initial_option'] = config_data['lora']
  20. for option, value in lora_options.items():
  21. template['elements'][index]['actions'][1]['options'].append(
  22. {
  23. "text": {
  24. "tag": "lark_md",
  25. "content": option
  26. },
  27. "value": value
  28. }
  29. )
  30. return template
  31. # 加载用户已选选项
  32. def load_initial_option_from_config(reply_message_id, card_content):
  33. config_data = load_config_from_json(reply_message_id)
  34. card_content['elements'][4]['actions'][0]['initial_option'] = config_data['action']
  35. card_content['elements'][4]['actions'][1]['initial_option'] = config_data['lora']
  36. return card_content
  37. # 保存waiting card
  38. def save_process_card(card_content, reply_message_id):
  39. output_directory = f"data/input_images/{reply_message_id}"
  40. os.makedirs(output_directory, exist_ok=True)
  41. json_path = os.path.join(output_directory, "process_card.json")
  42. with open(json_path, 'w', encoding='utf-8') as f:
  43. json.dump(card_content, f, ensure_ascii=False, indent=4)
  44. # 更新waiting card
  45. def update_process_card(reply_message_id, img_key, upload_key):
  46. output_directory = f"data/input_images/{reply_message_id}"
  47. os.makedirs(output_directory, exist_ok=True)
  48. json_path = os.path.join(output_directory, "process_card.json")
  49. with open(json_path, 'r', encoding='utf-8') as f:
  50. card_content = json.load(f)
  51. # 更新图片
  52. for column in card_content['elements'][1]['columns']:
  53. for i, element in enumerate(column["elements"]):
  54. if element.get("img_key") == img_key:
  55. column["elements"].append(
  56. {
  57. "tag": "img",
  58. "img_key": upload_key,
  59. "alt": {
  60. "tag": "plain_text",
  61. "content": ""
  62. },
  63. "mode": "fit_horizontal",
  64. "preview": True
  65. }
  66. )
  67. break
  68. config_data = load_config_from_json(reply_message_id)
  69. user_open_id = config_data['user_open_id']
  70. # 更新任务状态
  71. if if_mission_fail(reply_message_id):
  72. logger.error(reply_message_id + "任务失败")
  73. card_content['header'] = {
  74. "title": {
  75. "tag": "plain_text",
  76. "content": "任务执行出现错误,请检查"
  77. },
  78. "template": "red"
  79. }
  80. card_content['elements'][0] = {
  81. "tag": "div",
  82. "text": {
  83. "content": f"<at id=\"{user_open_id}\"></at>---------------/(ㄒoㄒ)/~~",
  84. "tag": "lark_md",
  85. }
  86. }
  87. del card_content['elements'][4]
  88. del card_content['elements'][4]
  89. else:
  90. if if_mission_done(reply_message_id):
  91. logger.info(reply_message_id + "任务成功")
  92. card_content['header'] = {
  93. "title": {
  94. "tag": "plain_text",
  95. "content": "任务完成"
  96. },
  97. "template": "green"
  98. }
  99. card_content['elements'][0] = {
  100. "tag": "div",
  101. "text": {
  102. "content": f"<at id=\"{user_open_id}\"></at>---------------o(* ̄▽ ̄*)ブ",
  103. "tag": "lark_md"
  104. }
  105. }
  106. del card_content['elements'][4]
  107. del card_content['elements'][4]
  108. with open(json_path, 'w', encoding='utf-8') as f:
  109. json.dump(card_content, f, ensure_ascii=False, indent=4)
  110. return card_content