meg_handler.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import json
  2. from handler.card_handler import choose_updated_card
  3. from utils.config_io import add_to_user_reply, if_has_processed, load_config_from_json, save_config_to_json
  4. from utils.image_io import create_image_path, save_image
  5. from utils.request_api import get_img, reply_message, update_message
  6. from utils.logger import logger
  7. from utils.template_io import load_template
  8. def handle_meg(meg, event_id, user_open_id):
  9. if 'message_id' in meg and 'content' in meg:
  10. # 正常消息,保存message_id可用于回复
  11. message_id = meg['message_id']
  12. if not if_has_processed(event_id):
  13. content = json.loads(meg['content'])
  14. if 'content' in content:
  15. # 为富文本消息
  16. rich_text_content = content['content']
  17. if if_at_bot_rich_text(rich_text_content):
  18. # @了机器人
  19. if if_has_img(rich_text_content):
  20. # 有图片
  21. response_data = img_in_meg_card(rich_text_content, if_has_download = False)
  22. res = reply_message(message_id, response_data)
  23. if res:
  24. reply_msg_id = res.json()['data']['message_id']
  25. # 保存到全局config防止重复回复
  26. add_to_user_reply(event_id, message_id, reply_msg_id)
  27. # 保存user信息到msg_config
  28. config_data = load_config_from_json(reply_msg_id)
  29. config_data['user_message'] = message_id
  30. config_data['user_open_id'] = user_open_id
  31. save_config_to_json(config_data, reply_msg_id)
  32. # 下载图片
  33. download_img_in_content(message_id, reply_msg_id, rich_text_content)
  34. # 更新卡片
  35. config_data = load_config_from_json(reply_msg_id)
  36. config_data['if_download'] = True
  37. save_config_to_json(config_data, reply_msg_id)
  38. if config_data['action'] != "" and config_data['lora'] != "":
  39. update_card_data = choose_updated_card(reply_msg_id, config_data)
  40. update_data ={
  41. "msg_type": "interactive",
  42. }
  43. update_data['content'] = json.dumps(update_card_data)
  44. else:
  45. update_data = img_in_meg_card(rich_text_content, if_has_download = True, reply_message_id = reply_msg_id)
  46. update_message(reply_msg_id, update_data)
  47. else:
  48. # 未@机器人
  49. return
  50. else:
  51. # 处理过的消息
  52. logger.info(message_id + '已处理过')
  53. return
  54. else:
  55. # 非正常消息
  56. return
  57. # 判断消息中是否@了机器人
  58. def if_at_bot_rich_text(content):
  59. for section in content:
  60. for item in section:
  61. if item.get('tag') == 'at' and item.get('user_name') == 'Stable Diffusion Of HOXI':
  62. return True
  63. return False
  64. # 判断消息中是否有图片
  65. def if_has_img(content):
  66. for section in content:
  67. for item in section:
  68. if item.get('tag') == 'img':
  69. return True
  70. return False
  71. # 下载用户信息中的图片
  72. def download_img_in_content(message_id, reply_message_id, content):
  73. for section in content:
  74. for item in section:
  75. if item.get('tag') == 'img':
  76. img_key = item.get('image_key')
  77. create_image_path(reply_message_id, img_key)
  78. for section in content:
  79. for item in section:
  80. if item.get('tag') == 'img':
  81. img_key = item.get('image_key')
  82. response = get_img(message_id, img_key)
  83. if response.status_code == 200:
  84. save_image(response.content, reply_message_id, img_key)
  85. logger.info(img_key + "保存成功")
  86. # 生成交互卡片消息
  87. def img_in_meg_card(content, if_has_download = False, reply_message_id = None):
  88. response_data ={
  89. "msg_type": "interactive",
  90. }
  91. card_content = load_template('choose_card' if if_has_download else 'upload_card', reply_message_id)
  92. image_columns = []
  93. for section in content:
  94. for item in section:
  95. if item.get('tag') == 'img':
  96. image_columns.append(
  97. {
  98. "tag": "column",
  99. "width": "weighted",
  100. "weight": 1,
  101. "vertical_align": "top",
  102. "elements":
  103. [
  104. {
  105. "tag": "img",
  106. "img_key": item.get('image_key'),
  107. "alt": {
  108. "tag": "plain_text",
  109. "content": ""
  110. },
  111. "mode": "fit_horizontal",
  112. "preview": True
  113. },
  114. ]
  115. }
  116. )
  117. card_content['elements'][1]['columns'] = image_columns
  118. response_data['content'] = json.dumps(card_content)
  119. return response_data