franksoo commited on
Commit
0129d33
·
1 Parent(s): 20668f0

fix: install Hermes from Git

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -1,59 +1,61 @@
1
  import os
2
  import hashlib
 
3
  from fastapi import FastAPI, Request
4
  from fastapi.responses import PlainTextResponse
 
5
  import gradio as gr
6
 
7
- # ================== 持久化目录(保留!)==================
8
- os.makedirs("/data/workspace", exist_ok=True)
9
- os.makedirs("/data/memory", exist_ok=True)
10
 
11
- # ================== 企业微信配置 ==================
12
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
 
 
13
 
14
- # ================== FastAPI ==================
15
- app = FastAPI(title="Hermes Agent + WeCom + 持久化")
16
-
17
- # ================== 企业微信回调验证(100% 必过版)==================
18
  @app.get("/gateway/wecom")
19
- async def wecom_verify(request: Request):
20
  try:
 
21
  msg_signature = request.query_params.get("msg_signature", "")
22
  timestamp = request.query_params.get("timestamp", "")
23
  nonce = request.query_params.get("nonce", "")
24
- echostr = request.query_params.get("echostr", "")
25
 
 
26
  arr = sorted([WECOM_TOKEN, timestamp, nonce])
27
  check_str = ''.join(arr)
28
  digest = hashlib.sha1(check_str.encode()).hexdigest()
29
-
30
- if digest == msg_signature:
31
- return PlainTextResponse(echostr)
32
- except:
33
- pass
34
- return PlainTextResponse("ok")
35
-
 
 
 
 
 
 
 
 
 
 
 
36
  @app.post("/gateway/wecom")
37
- async def wecom_post():
38
  return PlainTextResponse("success")
39
 
40
- # ================== Gradio 界面 ==================
41
- def chat(message, history):
42
- return "", history + [[message, "系统运行中 \n持久化已启用 \n企业微信已连接"]]
43
-
44
- with gr.Blocks(title="Hermes Agent") as demo:
45
- gr.Markdown("# 🤖 Hermes Agent")
46
- gr.Markdown("✅ 持久化已启用 | ✅ 企业微信已配置 | ✅ NVIDIA模型就绪")
47
- chatbot = gr.Chatbot(height=500)
48
- msg = gr.Textbox()
49
- clear = gr.Button("清空")
50
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
51
- clear.click(lambda: None, None, chatbot)
52
 
53
- # 挂载 Gradio
54
  gr.mount_gradio_app(app, demo, path="/")
55
 
56
- # ================== 启动 ==================
57
  if __name__ == "__main__":
58
  import uvicorn
59
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import os
2
  import hashlib
3
+ import base64
4
  from fastapi import FastAPI, Request
5
  from fastapi.responses import PlainTextResponse
6
+ from Crypto.Cipher import AES
7
  import gradio as gr
8
 
9
+ app = FastAPI(title="企业微信官方回调")
 
 
10
 
11
+ # ========== 从 HF Secrets 读取(必须和企业微信后台一致)==========
12
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
13
+ WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "")
14
+ CORPID = os.getenv("CORPID", "") # 企业微信企业ID
15
 
16
+ # ========== 企业微信 URL 校验(官方文档标准实现)==========
 
 
 
17
  @app.get("/gateway/wecom")
18
+ async def verify_url(request: Request):
19
  try:
20
+ # 1. 获取参数
21
  msg_signature = request.query_params.get("msg_signature", "")
22
  timestamp = request.query_params.get("timestamp", "")
23
  nonce = request.query_params.get("nonce", "")
24
+ echostr_enc = request.query_params.get("echostr", "")
25
 
26
+ # 2. 签名校验
27
  arr = sorted([WECOM_TOKEN, timestamp, nonce])
28
  check_str = ''.join(arr)
29
  digest = hashlib.sha1(check_str.encode()).hexdigest()
30
+ if digest != msg_signature:
31
+ return PlainTextResponse("sign error")
32
+
33
+ # 3. AES 解密 echostr(官方必须步骤!)
34
+ aes_key = base64.b64decode(WECOM_AES_KEY + "=")
35
+ cipher = AES.new(aes_key, AES.MODE_CBC, iv=aes_key[:16])
36
+ decrypted = cipher.decrypt(base64.b64decode(echostr_enc))
37
+ pad = ord(decrypted[-1:])
38
+ decrypted = decrypted[:-pad]
39
+ msg = decrypted[16:].decode()
40
+
41
+ # 4. 原样返回明文(官方要求)
42
+ return PlainTextResponse(msg)
43
+
44
+ except Exception as e:
45
+ return PlainTextResponse(str(e))
46
+
47
+ # 消息接收
48
  @app.post("/gateway/wecom")
49
+ async def receive_msg():
50
  return PlainTextResponse("success")
51
 
52
+ # ========== Gradio 界面 ==========
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("# ✅ 企业微信机器人就绪")
55
+ gr.Markdown(f"回调地址:https://franksoo-agent.hf.space/gateway/wecom")
 
 
 
 
 
 
 
 
56
 
 
57
  gr.mount_gradio_app(app, demo, path="/")
58
 
 
59
  if __name__ == "__main__":
60
  import uvicorn
61
  uvicorn.run(app, host="0.0.0.0", port=7860)