File size: 2,450 Bytes
95f63e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging

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

def select_relevant_image(images: list, text: str) -> str:
    """Select a contextually relevant image from the list based on text."""
    if not images:
        logging.info("No images available, using empty media URL.")
        return ""
    
    # Prioritize images that are likely content-related (avoid logos, icons)
    for img in images:
        if not any(keyword in img.lower() for keyword in ["logo", "icon", "banner", "ad"]):
            logging.info(f"Selected image: {img}")
            return img
    
    # Fallback to first image if no clear content image
    logging.info(f"No content image found, using first image: {images[0]}")
    return images[0]

def build_rich_card(scraped_data: dict, summary: dict) -> dict:
    """Build the rich card JSON using only scraped data and summary."""
    logging.info(f"Building rich card with scraped_data: {scraped_data}, summary: {summary}")
    
    # Select relevant image
    media_url = select_relevant_image(scraped_data.get("images", []), scraped_data.get("text", ""))
    
    # Use scraped URL
    page_url = scraped_data.get("url", "")
    
    # Use summary description
    description = summary.get("description", "Explore news and insights.")
    
    rich_card = {
        "targets": [{"ids": [1368], "targetType": "humans"}],
        "text": description,
        "mediaType": "image",
        "media": media_url,
        "buttons": [
            {
                "type": "weburl",
                "title": "View Now",
                "payload": page_url
            },
            {
                "type": "postback",
                "title": "Learn More",
                "payload": "learn_more",
                "execute": None
            }
        ],
        "quickReplies": [
            {
                "type": "postback",
                "title": "Show Similar",
                "payload": "similar_content"
            },
            {
                "type": "call",
                "title": "Contact Support",
                "payload": "+12345678901"
            }
        ],
        "richCard": {
            "cardOrientation": "VERTICAL",
            "mediaHeight": "MEDIUM"
        }
    }
    logging.info(f"Generated rich card: {rich_card}")
    return rich_card