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

fix: install Hermes from Git

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -1,48 +1,42 @@
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)
 
1
  import os
2
  import hashlib
3
+ from fastapi import FastAPI, Request
4
  from fastapi.responses import PlainTextResponse
 
5
 
6
  app = FastAPI(title="WeCom Callback")
7
 
8
+ # 企业微信配置(只需要 TOKEN
9
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
10
 
11
+ # ==================== 企业微信回调验证()====================
12
  @app.get("/gateway/wecom")
13
+ async def verify(request: Request):
 
 
 
 
 
14
  try:
15
+ # 获取参数
16
+ msg_signature = request.query_params.get("msg_signature", "")
17
+ timestamp = request.query_params.get("timestamp", "")
18
+ nonce = request.query_params.get("nonce", "")
19
+ echostr = request.query_params.get("echostr", "")
20
+
21
+ # 校验签名
22
+ arr = [WECOM_TOKEN, timestamp, nonce]
23
+ arr.sort()
24
+ check_str = "".join(arr)
25
+ digest = hashlib.sha1(check_str.encode()).hexdigest()
26
+
27
+ if digest == msg_signature:
28
+ return PlainTextResponse(echostr)
29
  else:
30
  return PlainTextResponse("invalid")
31
+ except Exception as e:
32
+ return PlainTextResponse(f"error: {str(e)}")
33
 
34
+ # 接收消息
35
  @app.post("/gateway/wecom")
36
+ async def message():
37
+ return PlainTextResponse("ok")
 
 
 
 
 
 
38
 
39
+ # 主页
40
+ @app.get("/")
41
+ async def root():
42
+ return {"status": "running", "wecom": "/gateway/wecom"}