TmpApi / app.py
tanbushi's picture
Update app.py
6b0ce4c verified
raw
history blame contribute delete
999 Bytes
from fastapi import FastAPI, Request
import uvicorn
import hashlib
import base64
import hmac
app = FastAPI()
@app.get("/")
def greet_json():
return {"Hello": "World!"}
@app.post("/dingtalk/webhook")
async def receive_msg(request: Request):
# 验证签名(若启用加签)
# return {"Hello": "post!"}
body = await request.json()
return body
return {"Hello": "post!"}
timestamp = request.headers.get("timestamp")
sign = request.headers.get("sign")
secret = "your_robot_secret"
string_to_sign = f"{timestamp}\n{secret}"
hmac_code = hmac.new(secret.encode(), string_to_sign.encode(), hashlib.sha256).digest()
calc_sign = base64.b64encode(hmac_code).decode()
if sign != calc_sign: return {"code": 403}
# 解析消息体
body = await request.json()
sender_id = body.get("senderId")
msg_content = body.get("text", {}).get("content")
print(f"收到来自{sender_id}的消息:{msg_content}")
return {"msg": "success"}