File size: 2,116 Bytes
07fbc67
 
 
 
 
f169c98
07fbc67
 
 
 
 
 
 
 
 
f169c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07fbc67
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
from fastapi import Request, status
from fastapi.responses import JSONResponse, Response
from fastapi.exceptions import HTTPException
from app.services.cache import MessageCache

from app.services.download_media import download_whatsapp_media
from app.utils.handle_message import handle_message
from app.utils.load_env import ACCESS_TOKEN
import time

logger = logging.getLogger(__name__)
message_cache = MessageCache()
user_chats = {}


# async def webhook(request: Request):
#     request_id = f"req_{int(time.time()*1000)}"

#     payload = await request.json()
#     logger.info(f"Processing webhook request {payload}")
#     processed_count = 0
#     error_count = 0
#     results = []

#     entries = payload.get("entry", [])
#     for entry in entries:
#         entry_id = entry.get("id")
#         logger.info(f"Processing entry_id: {entry_id}")

#         changes = entry.get("changes", [])
#         for change in changes:
#             messages = change.get("value", {}).get("messages", [])
#             media = {}
#             for message in messages:
#                 logger.info(f"Processing message: {message}")
#                 response = await handle_message(message=message, user_chats = user_chats, message_cache = message_cache, access_token = ACCESS_TOKEN)

#                 results.append(response)

#     response_data = {
#         "request_id": request_id,
#         # "processed": processed_count,
#         # "errors": error_count,
#         "results": results,
#     }

#     logger.info(f"Webhook processing completed - Processed: {processed_count}, Errors: {error_count}")
#     return JSONResponse(content=response_data, status_code=status.HTTP_200_OK)

async def verify_webhook(request: Request):
    mode = request.query_params.get('hub.mode')
    token = request.query_params.get('hub.verify_token')
    challenge = request.query_params.get('hub.challenge')

    if mode == 'subscribe' and token == 'test':
        return Response(content=challenge, media_type="text/plain")
    else:
        raise HTTPException(status_code=403, detail="Verification failed")