Browse Source

更新框架
更新图片api便于远程提交图片消息

GXX 3 months ago
parent
commit
e81d739537
3 changed files with 41 additions and 4 deletions
  1. 4 1
      chat_model.py
  2. 35 2
      main.py
  3. 2 1
      msg_send_save.py

+ 4 - 1
chat_model.py

@@ -9,7 +9,7 @@ import os
 
 # 全局变量在这里改写:
 max_length = 20
-max_pic_see = 1
+max_pic_see = 0
 
 prompt_pic = config_get("prompt_pic")
 prompt_lora = config_get("prompt_lora")
@@ -46,6 +46,9 @@ def AI_chat(group_id ,word ,prompt):
             recent_list[list_num] = "发送了一张图片," + str(pic_see(url)).replace("\n", "")
             pic_num +=1
     for i in range(len(recent_list)):
+        #新增逻辑,处理掉所有没必要的链接内容
+        if recent_list[i].startswith("图片内容:"):
+            recent_list[i] = "图片内容"
         if i% 2 == 0:
             recent += recent_list[i] + ":"
         else:

+ 35 - 2
main.py

@@ -1,4 +1,4 @@
-from flask import Flask, request
+from flask import Flask, request, abort, send_file
 import pandas as pd
 from openai import OpenAI
 import requests
@@ -277,7 +277,6 @@ def chat():
         elif notice_type == "notify" and data['target_id'] == data['self_id']:
             user_id = data['user_id']
             print(f"我被{user_id}戳了一下!")
-            send_group_poke(group_id,user_id)
             if get_week()=="星期四":
                 # 触发疯狂星期四文案
                 # 生成一个0到99之间的随机整数,控制特殊文案概率为40%
@@ -286,10 +285,44 @@ def chat():
                 send_group_message_word(group_id,get_v50()) if rand_num < 30 else send_group_message_word(group_id,"₍^ >ヮ<^₎")
             else:
                 send_group_message_word(group_id,"₍^ >ヮ<^₎")
+            send_group_poke(group_id, user_id)
     elif data_type == "meta_event":
         if data["meta_event_type"] == "heartbeat" and data["status"]['online'] == True:
             last_heartbeat_time = data['time']
     return ""
 
+
+# 允许的图片文件扩展名
+ALLOWED_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'}
+
+
+@app.route('/pic', methods=['GET'])
+def pic_send():
+    # 获取URL参数中的path
+    image_path = request.args.get('path')
+
+    # 检查path参数是否存在
+    if not image_path:
+        abort(400, description="缺少path参数")
+
+    # 检查文件是否存在
+    if not os.path.exists(image_path):
+        abort(404, description="图片文件不存在")
+
+    # 检查文件是否为图片类型
+    file_ext = os.path.splitext(image_path)[1].lower()
+    if file_ext not in ALLOWED_EXTENSIONS:
+        abort(400, description="不支持的文件类型,仅允许图片文件")
+
+    # 检查是否为文件(不是目录)
+    if not os.path.isfile(image_path):
+        abort(400, description="指定路径不是文件")
+
+    try:
+        # 返回图片文件
+        return send_file(image_path)
+    except Exception as e:
+        abort(500, description=f"服务器错误:{str(e)}")
+
 if __name__ == '__main__':
     app.run(host='0.0.0.0', port=3100)

+ 2 - 1
msg_send_save.py

@@ -35,6 +35,7 @@ def send_group_message_pic(group_id, user_id, image_path):
     """
     给特定群聊的特定人发送图片, image_path为图片url,可以是本地文件也可以是网络地址
     """
+    file_path = "http://host.docker.internal:3100/pic" + f"?path=/{image_path}"
     response = requests.post('http://127.0.0.1:3000/send_group_msg', json={
         'group_id': group_id,
         'message': [{
@@ -45,7 +46,7 @@ def send_group_message_pic(group_id, user_id, image_path):
         },{
             'type': 'image',
             'data': {
-                'file': image_path
+                'file': file_path
             }
         }]
     })