#!/usr/bin/env python3 """ Get Telegram Chat ID for bot notifications Run this script, then send a message to your bot """ import requests import time # Your bot token BOT_TOKEN = "8555333979:AAH_oCYvbXEt6IQ0-qbFdb9kSjdqpyIJcqA" BASE_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" def get_bot_info(): """Get bot information""" try: response = requests.get(f"{BASE_URL}/getMe") if response.status_code == 200: bot_info = response.json() if bot_info['ok']: print("šŸ¤– Bot Information:") print(f" Name: {bot_info['result']['first_name']}") print(f" Username: @{bot_info['result']['username']}") print(f" Can read messages: {bot_info['result']['can_read_all_group_messages']}") return True print(f"āŒ Failed to get bot info: {response.text}") return False except Exception as e: print(f"āŒ Error: {e}") return False def get_updates(): """Get recent updates (messages) to extract chat ID""" try: response = requests.get(f"{BASE_URL}/getUpdates", timeout=30) if response.status_code == 200: updates = response.json() if updates['ok'] and updates['result']: print("\nšŸ“Ø Recent Messages:") for update in updates['result'][-5:]: # Show last 5 messages if 'message' in update: message = update['message'] chat = message['chat'] from_user = message.get('from', {}) print(f"\nšŸ’¬ Message from: {from_user.get('first_name', 'Unknown')}") print(f" Chat ID: {chat['id']}") print(f" Chat Type: {chat['type']}") print(f" Message: {message.get('text', 'N/A')}") print(f" Time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(message['date']))}") if chat['type'] == 'private': print(f"\nšŸŽÆ YOUR CHAT ID: {chat['id']}") print(" Use this in your .env file as TELEGRAM_CHAT_ID") return chat['id'] print("\nšŸ’” No recent messages found. Send a message to your bot and run this again.") return None else: print("\nšŸ’” No messages received yet. Please:") print(" 1. Open Telegram") print(" 2. Find your bot: Search for the username shown above") print(" 3. Send any message to the bot") print(" 4. Run this script again") return None else: print(f"āŒ Failed to get updates: {response.text}") return None except Exception as e: print(f"āŒ Error getting updates: {e}") return None def send_test_message(chat_id): """Send a test message to confirm chat ID works""" try: message = "āœ… Telegram integration test successful! Your chat ID is working." data = { 'chat_id': chat_id, 'text': message } response = requests.post(f"{BASE_URL}/sendMessage", data=data) if response.status_code == 200: print("\nšŸ“¤ Test message sent! Check your Telegram.") return True else: print(f"āŒ Failed to send test message: {response.text}") return False except Exception as e: print(f"āŒ Error sending test message: {e}") return False def main(): print("šŸ” Telegram Chat ID Finder") print("=" * 40) if not get_bot_info(): print("āŒ Bot token is invalid. Please check your token.") return print("\nā³ Waiting for messages... (you have 30 seconds)") print("šŸ“± Please send a message to your bot now...") chat_id = get_updates() if chat_id: print(f"\nšŸŽ‰ Found your Chat ID: {chat_id}") if send_test_message(chat_id): print("\nāœ… Setup complete!") print("\nšŸ“ Add this to your .env file:") print(f"TELEGRAM_BOT_TOKEN={BOT_TOKEN}") print(f"TELEGRAM_CHAT_ID={chat_id}") print("\nšŸ“„ Or update your existing .env:") print("TELEGRAM_CHAT_ID=your_chat_id_here") print(f" ↑ replace with: {chat_id}") else: print("\nāš ļø Chat ID found but test message failed.") print(" You can still use the chat ID, but double-check your setup.") else: print("\nāŒ No chat ID found.") print(" Make sure to send a message to your bot and run this script again.") if __name__ == "__main__": main()