File size: 999 Bytes
7b82dd9 afaa85d 7b82dd9 56ffd2b 6b0ce4c a6ff621 56ffd2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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"} |