Spaces:
Paused
Paused
| #!/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() | |