devZenaight commited on
Commit
058db11
·
verified ·
1 Parent(s): 4c9a79b

Upload 5 files

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -1
  2. main.py +84 -0
Dockerfile CHANGED
@@ -9,4 +9,4 @@ COPY --chown=user . .
9
 
10
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
 
12
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
9
 
10
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
 
12
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import JSONResponse, PlainTextResponse
3
+ import os
4
+ import httpx
5
+ import base64
6
+
7
+ app = FastAPI()
8
+
9
+ @app.get("/")
10
+ def root():
11
+ return {"message": "Hello from WhatsApp ChatBot"}
12
+
13
+ @app.get("/ping")
14
+ def ping():
15
+ return {"pong": True}
16
+
17
+ @app.post("/test")
18
+ def test():
19
+ return {"status": "ok"}
20
+
21
+ @app.get("/webhook")
22
+ async def verify_webhook(request: Request):
23
+ params = request.query_params
24
+ mode = params.get("hub.mode")
25
+ token = params.get("hub.verify_token")
26
+ challenge = params.get("hub.challenge")
27
+ verify_token = os.getenv("WHATSAPP_TOKEN", "")
28
+
29
+ if mode == "subscribe" and token == verify_token and challenge:
30
+ return PlainTextResponse(content=challenge, status_code=200)
31
+ return JSONResponse(content={"error": "Invalid token"}, status_code=403)
32
+
33
+ @app.post("/webhook")
34
+ async def receive_webhook(request: Request):
35
+ data = await request.json()
36
+ try:
37
+ msg = data["entry"][0]["changes"][0]["value"]["messages"][0]
38
+ wa_id = msg["from"]
39
+ msg_type = msg["type"]
40
+
41
+ if msg_type == "image":
42
+ media_id = msg["image"]["id"]
43
+ image_bytes = await download_whatsapp_media(media_id)
44
+ if not image_bytes:
45
+ return {"status": "error", "message": "Failed to process image"}
46
+ b64_str = base64.b64encode(image_bytes).decode('utf-8')
47
+ reply = f"Base64: {b64_str[:10]}"
48
+ send_whatsapp_message(wa_id, reply)
49
+ return {"status": "success", "base64_first_10": b64_str[:10]}
50
+ else:
51
+ send_whatsapp_message(wa_id, "Please send an image.")
52
+ return {"status": "replied", "message": "Please send an image"}
53
+ except Exception as e:
54
+ print("Webhook error:", e)
55
+ return JSONResponse(content={"error": str(e)}, status_code=400)
56
+
57
+ async def download_whatsapp_media(media_id):
58
+ try:
59
+ with open("test.jpg", "rb") as f:
60
+ return f.read()
61
+ except FileNotFoundError:
62
+ print("test.jpg not found")
63
+ return None
64
+
65
+ def send_whatsapp_message(wa_id, text):
66
+ url = f"https://graph.facebook.com/v19.0/{os.getenv('PHONE_NUMBER_ID')}/messages"
67
+ headers = {
68
+ "Authorization": f"Bearer {os.getenv('WHATSAPP_API_TOKEN')}",
69
+ "Content-Type": "application/json"
70
+ }
71
+ payload = {
72
+ "messaging_product": "whatsapp",
73
+ "to": wa_id,
74
+ "type": "text",
75
+ "text": {"body": text}
76
+ }
77
+ httpx.post(url, headers=headers, json=payload)
78
+
79
+ @app.post("/send")
80
+ async def check_token():
81
+ token = os.getenv("WHATSAPP_TOKEN", "")
82
+ if token:
83
+ return {"token_loaded": True}
84
+ return JSONResponse(content={"error": "Token not found in environment"}, status_code=400)