File size: 5,669 Bytes
3566f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8515b6b
 
 
 
3566f32
8515b6b
f1e54f5
 
 
d202667
 
f1e54f5
 
 
 
d202667
3566f32
8515b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3566f32
8515b6b
3566f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8515b6b
3566f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8515b6b
 
3566f32
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# routes/api/whatsapp_webhook.py
from fastapi import APIRouter, Request, HTTPException, status
from fastapi.responses import JSONResponse
import logging
import json

# Import your function to send messages back
from components.gateways.headlines_to_wa import fetch_cached_headlines, send_to_whatsapp

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

router = APIRouter()

# WhatsApp/Gupshup webhook endpoint
@router.post("/message-received")
async def whatsapp_webhook_receiver(request: Request):
    """
    Receives incoming messages from Gupshup WhatsApp webhook.
    Sends a daily news digest if the user sends a specific command.
    """
    try:
        # <<< FIX 1: Assume JSON body directly for incoming_message >>>
        body_bytes = await request.body()
        body_str = body_bytes.decode("utf-8")
        logging.info(f"Raw webhook body received: {body_str}")

        try:
            incoming_message = json.loads(body_str)
        except json.JSONDecodeError:
            logging.error("❌ Failed to decode webhook body as JSON")
            return JSONResponse(status_code=400, content={"error": "Invalid JSON format"})


        from_number = None
        message_text = None

        # <<< FIX 2: Robustly extract data from the nested structure >>>
        entries = incoming_message.get('entry', [])
        for entry in entries:
            changes = entry.get('changes', [])
            for change in changes:
                # We are interested in changes related to 'messages'
                if change.get('field') == 'messages':
                    value = change.get('value', {})
                    messages_list = value.get('messages', [])
                    
                    for msg in messages_list:
                        # Check if it's a text message and extract sender/body
                        if msg.get('type') == 'text':
                            from_number = msg.get('from')
                            message_text = msg.get('text', {}).get('body')
                            break # Found text message, exit inner loop
                    if from_number and message_text:
                        break # Found message, exit middle loop
            if from_number and message_text:
                break # Found message, exit outer loop

        # Check if sender and message text were successfully extracted
        if not from_number or not message_text:
            logging.warning("Received webhook without valid sender or message text in expected structure.")
            return JSONResponse(status_code=200, content={"status": "ignored", "message": "Missing sender or text"})

        logging.info(f"Message from {from_number}: {message_text}")

        # Check for specific commands to send the digest
        if message_text.lower().strip() == "digest":
            logging.info(f"User {from_number} requested daily digest.")
            
            # Fetch the digest headlines
            full_message_text = fetch_cached_headlines()

            if full_message_text.startswith("❌") or full_message_text.startswith("⚠️"):
                logging.error(f"Failed to fetch digest for {from_number}: {full_message_text}")
                # Send an error message back to the user
                send_to_whatsapp(f"Sorry, I couldn't fetch the news digest today. {full_message_text}", destination_number=from_number)
                return JSONResponse(status_code=500, content={"status": "error", "message": "Failed to fetch digest"})
            
            # Send the digest back to the user who requested it
            # <<< FIX 3: Pass from_number as destination_number to send_to_whatsapp >>>
            result = send_to_whatsapp(full_message_text, destination_number=from_number)

            if result.get("status") == "success":
                logging.info(f"✅ Successfully sent digest to {from_number}.")
                return JSONResponse(status_code=200, content={"status": "success", "message": "Digest sent"})
            else:
                logging.error(f"❌ Failed to send digest to {from_number}: {result.get('error')}")
                send_to_whatsapp(f"Sorry, I couldn't send the news digest to you. Error: {result.get('error', 'unknown')}", destination_number=from_number)
                return JSONResponse(status_code=500, content={"status": "error", "message": "Failed to send digest"})
        else:
            logging.info(f"Received unhandled message from {from_number}: '{message_text}'")
            return JSONResponse(status_code=200, content={"status": "ignored", "message": "No action taken for this command"})

    except Exception as e:
        logging.error(f"Error processing webhook: {e}", exc_info=True)
        return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})

# Gupshup webhook verification endpoint (GET request with 'hub.mode' and 'hub.challenge')
@router.get("/message-received")
async def whatsapp_webhook_verify(request: Request):
    """
    Endpoint for Gupshup webhook verification.
    """
    mode = request.query_params.get("hub.mode")
    challenge = request.query_params.get("hub.challenge")

    if mode == "subscribe" and challenge:
        logging.info(f"Webhook verification successful. Challenge: {challenge}")
        # Challenge needs to be returned as an integer, not string
        return JSONResponse(status_code=200, content=int(challenge)) 
    else:
        logging.warning(f"Webhook verification failed. Mode: {mode}, Challenge: {challenge}")
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Verification failed")