Antalela commited on
Commit
fdb779d
·
1 Parent(s): d3b088c
Files changed (4) hide show
  1. .gitignore +0 -0
  2. Dockerfile +15 -0
  3. requirements.txt +1 -0
  4. router.py +36 -0
.gitignore ADDED
File without changes
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.10.9 image
2
+ FROM python:3.11.5
3
+
4
+ # Copy the current directory contents into the container at .
5
+ COPY . .
6
+
7
+ # Set the working directory to /
8
+ WORKDIR /
9
+
10
+ # Install requirements.txt
11
+ RUN pip install --no-cache-dir -r /requirements.txt
12
+
13
+
14
+ # Start the FastAPI app on port 7860, the default port expected by Spaces
15
+ CMD ["uvicorn", "router:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fastapi==0.111.0
router.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from fastapi.responses import PlainTextResponse
3
+ import os
4
+ from datetime import datetime
5
+
6
+ app = FastAPI()
7
+
8
+ # Get environment variables
9
+ VERIFY_TOKEN = os.getenv("WP_Token")
10
+
11
+ @app.get("/", response_class=PlainTextResponse)
12
+ async def verify_webhook(request: Request):
13
+ """
14
+ GET route for webhook verification (Facebook / Instagram style)
15
+ """
16
+ params = request.query_params
17
+ mode = params.get("hub.mode")
18
+ challenge = params.get("hub.challenge")
19
+ token = params.get("hub.verify_token")
20
+
21
+ if mode == "subscribe" and token == VERIFY_TOKEN:
22
+ print("WEBHOOK VERIFIED")
23
+ return challenge or ""
24
+ else:
25
+ raise HTTPException(status_code=403, detail="Forbidden")
26
+
27
+ @app.post("/")
28
+ async def receive_webhook(request: Request):
29
+ """
30
+ POST route for receiving webhook events
31
+ """
32
+ body = await request.json()
33
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
34
+ print(f"\n\nWebhook received {timestamp}\n")
35
+ print(body)
36
+ return PlainTextResponse(status_code=200, content="")