Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /notifications /telegram.py
| """ | |
| Telegram Integration fΓΌr Zen-AI-Pentest | |
| """ | |
| import logging | |
| from typing import Dict, Final | |
| import requests | |
| logger = logging.getLogger(__name__) | |
| TELEGRAM_API_BASE: Final[str] = "https://api.telegram.org" | |
| class TelegramNotifier: | |
| """Sendet Benachrichtigungen an Telegram""" | |
| def __init__(self, bot_token: str, chat_id: str) -> None: | |
| """ | |
| Initialize Telegram notifier. | |
| Args: | |
| bot_token: Bot token from @BotFather | |
| chat_id: Chat ID or channel name (e.g., @channelname) | |
| """ | |
| self.bot_token = bot_token | |
| self.chat_id = chat_id | |
| self.api_url = f"{TELEGRAM_API_BASE}/bot{bot_token}" | |
| def send_message(self, text: str, parse_mode: str = "Markdown") -> bool: | |
| """Sendet Text-Nachricht""" | |
| payload = { | |
| "chat_id": self.chat_id, | |
| "text": text, | |
| "parse_mode": parse_mode, | |
| } | |
| try: | |
| response = requests.post( | |
| f"{self.api_url}/sendMessage", | |
| json=payload, | |
| timeout=10, | |
| ) | |
| if response.status_code == 200: | |
| return True | |
| logger.error(f"Telegram API error: {response.text}") | |
| return False | |
| except requests.RequestException as e: | |
| logger.error(f"Telegram send error: {e}") | |
| return False | |
| def send_scan_completed( | |
| self, | |
| scan_id: int, | |
| target: str, | |
| findings_count: int, | |
| critical_count: int, | |
| ) -> bool: | |
| """Sendet Scan-Completion Benachrichtigung""" | |
| icon = "π΄" if critical_count > 0 else "π " if findings_count > 0 else "π’" | |
| message = ( | |
| f"{icon} *Scan Completed*\n\n" | |
| f"π *Target:* `{target}`\n" | |
| f"π *Scan ID:* `{scan_id}`\n" | |
| f"π *Findings:* {findings_count}\n" | |
| f"π₯ *Critical:* {critical_count}\n\n" | |
| f"_Generated by Zen-AI-Pentest_" | |
| ) | |
| return self.send_message(message) | |
| def send_critical_finding(self, finding: Dict) -> bool: | |
| """Sendet Critical Finding Alert""" | |
| message = ( | |
| f"π¨ *CRITICAL FINDING*\n\n" | |
| f"*Title:* {finding.get('title', 'Unknown')}\n" | |
| f"*Target:* `{finding.get('target', 'N/A')}`\n" | |
| f"*Tool:* {finding.get('tool', 'Unknown')}\n" | |
| f"*CVSS:* {finding.get('cvss_score', 'N/A')}\n\n" | |
| f"{finding.get('description', '')[:300]}..." | |
| ) | |
| return self.send_message(message) | |
| def test_connection(self) -> bool: | |
| """Testet die Verbindung zum Bot""" | |
| try: | |
| response = requests.get(f"{self.api_url}/getMe", timeout=10) | |
| if response.status_code == 200: | |
| data = response.json() | |
| logger.info(f"Connected to bot: {data['result']['username']}") | |
| return True | |
| return False | |
| except requests.RequestException as e: | |
| logger.error(f"Connection test failed: {e}") | |
| return False | |
| def telegram_notify_scan_complete(scan_id: int, target: str, findings_count: int, critical_count: int) -> str: | |
| """Sendet Telegram-Benachrichtigung bei Scan-Abschluss""" | |
| import os | |
| bot_token = os.getenv("TELEGRAM_BOT_TOKEN") | |
| chat_id = os.getenv("TELEGRAM_CHAT_ID") | |
| if not bot_token or not chat_id: | |
| return "Telegram not configured (TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID missing)" | |
| notifier = TelegramNotifier(bot_token, chat_id) | |
| success = notifier.send_scan_completed(scan_id, target, findings_count, critical_count) | |
| return "Notification sent" if success else "Failed to send notification" | |
Xet Storage Details
- Size:
- 3.69 kB
- Xet hash:
- a7c7ce3ccfeeda1fe4ec917e792b2d0cb2f91c67a9525d7f76dc9ede16fe8382
Β·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.