franksoo commited on
Commit
33c6d49
·
1 Parent(s): 0d26c71

fix: install Hermes from Git

Browse files
Files changed (1) hide show
  1. app.py +27 -40
app.py CHANGED
@@ -1,61 +1,48 @@
1
  import os
2
  import hashlib
3
- import hmac
4
- import base64
5
- from fastapi import FastAPI, Request, Response
6
  from fastapi.responses import PlainTextResponse
7
  import gradio as gr
8
 
9
- app = FastAPI(title="WeCom Gateway")
10
 
11
- # 企业微信配置(HF Secrets 读取)
12
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
13
- WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "")
14
- WECOM_AGENTID = os.getenv("WECOM_AGENTID", "")
15
 
16
- # ====================== 企业微信官方回调验(核心!)======================
17
  @app.get("/gateway/wecom")
18
- async def wecom_verify(
19
- msg_signature: str,
20
- timestamp: str,
21
- nonce: str,
22
- echostr: str
23
  ):
24
- # 企业微信回调验证逻辑(官方标准)
25
  try:
26
- # 排序
27
- arr = sorted([WECOM_TOKEN, timestamp, nonce])
28
- # 拼接
29
- s = "".join(arr)
30
- # SHA1 加密
31
- sha = hashlib.sha1()
32
- sha.update(s.encode("utf-8"))
33
- sign = sha.hexdigest()
34
-
35
- if sign == msg_signature:
36
- return PlainTextResponse(echostr) # 必须原样返回!
37
  else:
38
- return PlainTextContent("invalid signature", status_code=403)
39
- except Exception as e:
40
- return PlainTextResponse(str(e), status_code=500)
41
 
42
- # POST 接收消息(企业微信发消息会走这里)
43
  @app.post("/gateway/wecom")
44
- async def wecom_message():
45
  return PlainTextResponse("success")
46
 
47
- # ====================== Gradio WebUI ======================
48
- def chat(msg, history):
49
- history.append((msg, "✅ 企业微信机器人已运行"))
50
- return "", history
51
 
52
- with gr.Blocks() as demo:
53
- gr.Markdown("# ✅ 企业微信机器人已就绪")
54
- gr.Markdown(f"### 回调地址:https://{os.getenv('SPACE_HOST')}/gateway/wecom")
55
- gr.Chatbot(height=400)
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)
 
1
  import os
2
  import hashlib
3
+ from fastapi import FastAPI, Request, Query
 
 
4
  from fastapi.responses import PlainTextResponse
5
  import gradio as gr
6
 
7
+ app = FastAPI(title="WeCom Callback")
8
 
9
+ # 从 Secrets 读取(必须和企业微信后台一致
10
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
 
 
11
 
12
+ # ====================== 企业微信回调验能过!)======================
13
  @app.get("/gateway/wecom")
14
+ async def verify(
15
+ msg_signature: str = None,
16
+ timestamp: str = None,
17
+ nonce: str = None,
18
+ echostr: str = None
19
  ):
 
20
  try:
21
+ # 企业微信访问时会带这4个参数
22
+ data = [WECOM_TOKEN, timestamp, nonce]
23
+ data.sort()
24
+ data = ''.join(data)
25
+ res = hashlib.sha1(data.encode()).hexdigest()
26
+
27
+ if res == msg_signature:
28
+ return PlainTextResponse(echostr) # 必须原样返回
 
 
 
29
  else:
30
+ return PlainTextResponse("invalid")
31
+ except:
32
+ return PlainTextResponse("error")
33
 
34
+ # 处理消息
35
  @app.post("/gateway/wecom")
36
+ async def msg():
37
  return PlainTextResponse("success")
38
 
39
+ # ====================== Gradio ======================
40
+ with gr.Blocks():
41
+ pass
 
42
 
43
+ gr.mount_gradio_app(app, gr.Blocks(), "/")
 
 
 
 
 
44
 
45
+ # ====================== 启动 ======================
46
  if __name__ == "__main__":
47
  import uvicorn
48
  uvicorn.run(app, host="0.0.0.0", port=7860)