|
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): |
|
|
|
|
|
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"} |