File size: 15,340 Bytes
909b715
 
 
 
 
ecfa3ec
 
909b715
 
 
 
 
 
 
 
 
 
 
 
ecfa3ec
 
909b715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecfa3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909b715
 
 
 
ecfa3ec
 
 
 
 
 
 
 
909b715
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import logging
import os
import io
import html
import aiohttp
import socket
from urllib.parse import urlparse
from dotenv import load_dotenv  # <--- NEW IMPORT
from telegram import Update, constants
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters

# ==========================================
# โš™๏ธ CONFIGURATION
# ==========================================

# 1. Load environment variables from the .env file
load_dotenv() 

# 2. Retrieve values
BOT_TOKEN = os.getenv("BOT_TOKEN", "").strip().replace('"', '').replace("'", "")
BACKEND_API_URL = os.getenv("EXTERNAL_ANALYSIS_API_URL", "").strip().replace('"', '').replace("'", "")
API_KEY = os.getenv("API_KEY")

# Check if critical vars are missing
if not BOT_TOKEN or not BACKEND_API_URL:
    raise ValueError("โŒ Error: BOT_TOKEN or BACKEND_API_URL is missing from .env file")

# ==========================================
# ๐Ÿ“ LOGGING SETUP
# ==========================================
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

# ==========================================
# ๐Ÿง  REPORT FORMATTER (JSON -> HTML)
# ==========================================
def format_analysis_report(data):
    """
    Converts the complex Backend JSON into a readable HTML Telegram message.
    """
    try:
        # --- HEADER ---
        tag = data.get("tag", "Analysis")
        overall_summary = data.get("overall_summary", "No summary provided.")
        source_cred_list = data.get("source_credibility_summary", [])
        
        # Determine icon based on tag content
        tag_lower = tag.lower()
        if "true" in tag_lower or "verified" in tag_lower:
            icon = "๐ŸŸข"
        elif "false" in tag_lower or "misinfo" in tag_lower or "fake" in tag_lower:
            icon = "๐Ÿ”ด"
        else:
            icon = "โš ๏ธ"
        
        # Start building the message
        message = f"<b>๐Ÿšจ VERIFACT ANALYSIS REPORT</b>\n"
        message += "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        message += f"<b>Result:</b> {icon} <b>{html.escape(tag.upper())}</b>\n"
        
        # Source Credibility Summary (Average)
        if source_cred_list:
            total_score = 0
            count = 0
            for item in source_cred_list:
                if isinstance(item, dict) and 'credibility_score' in item:
                    try:
                        total_score += int(item['credibility_score'])
                        count += 1
                    except (ValueError, TypeError):
                        pass
            
            if count > 0:
                avg_score = int(total_score / count)
                # Determine label based on average
                if avg_score >= 80:
                    cred_label = "High"
                elif avg_score >= 60:
                    cred_label = "Moderate"
                else:
                    cred_label = "Low"
                message += f"<b>Source Credibility:</b> {cred_label} ({avg_score}%)\n"

        message += "\n<b>๐Ÿ“ Summary:</b>\n"
        message += f"<i>{html.escape(overall_summary)}</i>\n\n"

        # --- CLAIMS ANALYSIS ---
        claims = data.get("analyzed_claims", [])
        if claims:
            message += "<b>๐Ÿ” CLAIMS ANALYSIS</b>\n"
            message += "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
            
            for i, claim in enumerate(claims, 1):
                claim_text = claim.get("claim_text", "N/A")
                conclusion = claim.get("conclusion", "N/A")
                
                message += f"<b>{i}๏ธโƒฃ Claim:</b> \"{html.escape(claim_text)}\"\n"
                message += f"<b>๐Ÿ’ก Conclusion:</b> {html.escape(conclusion)}\n"
                
                # Evidence
                supporting = claim.get("supporting_evidence", [])
                opposing = claim.get("opposing_evidence", [])
                
                if supporting:
                    message += "<b>โœ… Supporting Evidence:</b>\n"
                    for ev in supporting:
                        src = html.escape(ev.get('source', 'Unknown'))
                        summ = html.escape(ev.get('summary', ''))
                        # Try to shorten source URL for display if it's a URL
                        if src.startswith('http'):
                            from urllib.parse import urlparse
                            try:
                                domain = urlparse(src).netloc
                                src_display = domain
                            except:
                                src_display = "Link"
                        else:
                            src_display = src
                            
                        message += f"โ€ข {summ} <i>({src_display})</i>\n"
                
                if opposing:
                    message += "<b>โŒ Opposing Evidence:</b>\n"
                    for ev in opposing:
                        src = html.escape(ev.get('source', 'Unknown'))
                        summ = html.escape(ev.get('summary', ''))
                        # Try to shorten source URL for display
                        if src.startswith('http'):
                            from urllib.parse import urlparse
                            try:
                                domain = urlparse(src).netloc
                                src_display = domain
                            except:
                                src_display = "Link"
                        else:
                            src_display = src

                        message += f"โ€ข {summ} <i>({src_display})</i>\n"
                
                message += "\n"

        # --- FACT CHECKS ---
        all_fact_checks = []
        for claim in claims:
            all_fact_checks.extend(claim.get("fact_checking_results", []))
            
        # Filter out "None" URLs or empty results
        valid_fact_checks = [fc for fc in all_fact_checks if fc.get('url') and fc.get('url') != "None"]

        if valid_fact_checks:
            message += "<b>๐Ÿ”— FACT CHECKS</b>\n"
            seen_urls = set()
            for fc in valid_fact_checks:
                url = fc.get('url', '#')
                if url not in seen_urls:
                    # Use inference or source name if available, else domain
                    source = fc.get('source', 'Fact Check')
                    if source == 'Fact Check' and url != '#':
                         from urllib.parse import urlparse
                         try:
                             source = urlparse(url).netloc
                         except:
                             pass
                    
                    source = html.escape(source)
                    message += f"โ€ข <a href='{url}'>{source}</a>\n"
                    seen_urls.add(url)
            message += "\n"

        # --- SOURCE CREDIBILITY DETAILS ---
        if source_cred_list:
             message += "<b>๐Ÿ›ก๏ธ SOURCE CREDIBILITY</b>\n"
             for item in source_cred_list[:5]: # Limit to top 5 to avoid spam
                 url = item.get('url', '')
                 score = item.get('credibility_score', 'N/A')
                 category = item.get('category', 'Unknown')
                 
                 # Extract domain
                 domain = "Unknown Source"
                 if url:
                     from urllib.parse import urlparse
                     try:
                         domain = urlparse(url).netloc
                     except:
                         domain = url
                 
                 message += f"โ€ข <b>{domain}</b>: {category} ({score})\n"
             message += "\n"

        # --- REVERSE IMAGE SEARCH (Optional) ---
        ris = data.get("reverse_image_search_data")
        if ris:
            ris_summary = ris.get("summary", "")
            matched = ris.get("matched_links", [])
            
            if ris_summary or matched:
                message += "<b>๐Ÿ–ผ๏ธ IMAGE ANALYSIS</b>\n"
                if ris_summary:
                    message += f"{html.escape(ris_summary)}\n"
                
                if matched:
                    for match in matched[:3]: 
                        domain = html.escape(match.get('domain', 'Link'))
                        url = match.get('url', '#')
                        date = html.escape(match.get('date', ''))
                        message += f"โ€ข <a href='{url}'>{domain}</a> ({date})\n"

        message += "\n<i>๐Ÿค– Analysis generated by Verifact</i>"
        return message

    except Exception as e:
        logger.error(f"Formatting Error: {e}")
        return "โš ๏ธ <b>Format Error:</b> Data received, but could not be displayed properly."

# ==========================================
# ๐Ÿ“ก BACKEND CONNECTOR
# ==========================================
async def query_backend_pipeline(form_data):
    """
    Sends Multipart Form Data (Text + Files) to Cloud Run.
    """
    headers = {}
    if API_KEY:
        headers["Authorization"] = f"Bearer {API_KEY}"
    
    timeout = aiohttp.ClientTimeout(total=60)

    async with aiohttp.ClientSession(timeout=timeout) as session:
        try:
            async with session.post(BACKEND_API_URL, data=form_data, headers=headers) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    error_text = await response.text()
                    logger.error(f"Backend Error {response.status}: {error_text}")
                    return None
        except Exception as e:
            logger.error(f"Connection Error: {e}")
            return None

# ==========================================
# ๐ŸŽฎ BOT HANDLERS
# ==========================================

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    welcome_text = (
        "๐Ÿ‘‹ <b>Verifact Forwarding Bot</b>\n\n"
        "I am connected to the misinformation analysis pipeline.\n"
        "Forward me any <b>Text</b> or <b>Image</b> to verify it."
    )
    await context.bot.send_message(chat_id=update.effective_chat.id, text=welcome_text, parse_mode='HTML')

async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_text = update.message.text
    
    await context.bot.send_chat_action(chat_id=update.effective_chat.id, action=constants.ChatAction.TYPING)
    
    status_msg = await context.bot.send_message(
        chat_id=update.effective_chat.id, 
        text="๐Ÿ“ก <i>Verifact is analyzing text...</i>",
        parse_mode='HTML'
    )

    data = aiohttp.FormData()
    data.add_field('text', user_text)
    data.add_field('source', 'Telegram')

    json_response = await query_backend_pipeline(data)

    if json_response:
        report = format_analysis_report(json_response)
        await context.bot.edit_message_text(
            chat_id=update.effective_chat.id,
            message_id=status_msg.message_id,
            text=report,
            parse_mode='HTML',
            disable_web_page_preview=True
        )
    else:
        await context.bot.edit_message_text(
            chat_id=update.effective_chat.id,
            message_id=status_msg.message_id,
            text="โš ๏ธ <b>System Error:</b> The pipeline is currently unreachable or timed out.",
            parse_mode='HTML'
        )

async def handle_photo(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_chat_action(chat_id=update.effective_chat.id, action=constants.ChatAction.UPLOAD_PHOTO)
    
    status_msg = await context.bot.send_message(
        chat_id=update.effective_chat.id, 
        text="๐Ÿ“ก <i>Downloading media & analyzing...</i>",
        parse_mode='HTML'
    )

    try:
        photo = update.message.photo[-1]
        file_obj = await context.bot.get_file(photo.file_id)
        
        f_memory = io.BytesIO()
        await file_obj.download_to_memory(out=f_memory)
        f_memory.seek(0) 

        data = aiohttp.FormData()
        caption_text = update.message.caption if update.message.caption else "Image analysis request"
        
        data.add_field('text', caption_text)
        data.add_field('source', 'Telegram')
        data.add_field('file', f_memory, filename='telegram_image.jpg', content_type='image/jpeg')

        json_response = await query_backend_pipeline(data)

        if json_response:
            report = format_analysis_report(json_response)
            await context.bot.edit_message_text(
                chat_id=update.effective_chat.id,
                message_id=status_msg.message_id,
                text=report,
                parse_mode='HTML',
                disable_web_page_preview=True
            )
        else:
            await context.bot.edit_message_text(
                chat_id=update.effective_chat.id,
                message_id=status_msg.message_id,
                text="โš ๏ธ <b>Error:</b> Analysis failed or timed out.",
                parse_mode='HTML'
            )

    except Exception as e:
        logger.error(f"Image Handler Error: {e}")
        await context.bot.edit_message_text(
            chat_id=update.effective_chat.id,
            message_id=status_msg.message_id,
            text="โŒ <b>Error:</b> Could not process the image file.",
            parse_mode='HTML'
        )

# ==========================================
# ๐Ÿ› ๏ธ DIAGNOSTICS
# ==========================================
def check_network():
    """Checks DNS resolution for critical services."""
    logger.info("--- NETWORK DIAGNOSTICS ---")
    
    targets = [
        ("Telegram API", "api.telegram.org"),
        ("Google", "google.com")
    ]
    
    # Add Backend Host if parseable
    try:
        if BACKEND_API_URL:
            backend_host = urlparse(BACKEND_API_URL).netloc
            targets.append(("Backend API", backend_host))
    except:
        pass

    for name, host in targets:
        try:
            ip = socket.gethostbyname(host)
            logger.info(f"โœ… {name} ({host}) resolved to {ip}")
        except socket.gaierror as e:
            logger.error(f"โŒ {name} ({host}) DNS FAILURE: {e}")
        except Exception as e:
            logger.error(f"โŒ {name} ({host}) Unexpected Error: {e}")
            
    logger.info("---------------------------")

# ==========================================
# ๐Ÿš€ MAIN RUNNER
# ==========================================
if __name__ == '__main__':
    check_network()
    
    from telegram.request import HTTPXRequest
    
    # Use a robust request object with longer timeouts
    trequest = HTTPXRequest(connection_pool_size=8, read_timeout=20.0, write_timeout=20.0, connect_timeout=20.0)
    
    application = ApplicationBuilder().token(BOT_TOKEN).request(trequest).build()
    
    application.add_handler(MessageHandler(filters.COMMAND & filters.Regex(r'^/start$'), start))
    application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_text))
    application.add_handler(MessageHandler(filters.PHOTO, handle_photo))

    print(f"โœ… Bot is running.")
    print(f"๐Ÿ”— Connected to Backend: {BACKEND_API_URL}")
    
    application.run_polling()