#!/usr/bin/env python3
"""
Get Telegram Chat ID Helper
This script helps you find your Telegram Chat ID for bot configuration.
"""
import sys
import asyncio
import logging
from pathlib import Path
# Add src directory to Python path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
try:
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
except ImportError:
print("ā python-telegram-bot not installed!")
print("š” Run: pip install python-telegram-bot")
sys.exit(1)
# Set up logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
class ChatIDBot:
"""Simple bot to get your Chat ID."""
def __init__(self, token: str):
self.token = token
self.application = None
self.found_chat_ids = set()
async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle any message and show chat information."""
chat_id = update.effective_chat.id
user = update.effective_user
message_text = update.message.text if update.message else "No text"
# Store found chat ID
self.found_chat_ids.add(chat_id)
# Respond with chat information
response = f"""
šÆ Found Your Chat Information!
š Chat ID: {chat_id}
š¤ User: {user.first_name} {user.last_name or ''}
š§ Username: @{user.username or 'None'}
š¬ Message: {message_text}
ā
Copy this Chat ID to your .env file:
TELEGRAM_CHAT_ID={chat_id}
"""
await update.message.reply_text(response, parse_mode='HTML')
# Print to console
print(f"\nš SUCCESS! Found Chat ID: {chat_id}")
print(f"š¤ User: {user.first_name} {user.last_name or ''}")
print(f"š§ Username: @{user.username or 'None'}")
print(f"\nš Add this to your .env file:")
print(f"TELEGRAM_CHAT_ID={chat_id}")
print(f"\nā
You can now stop this script and start your trading bot!")
async def start_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle the /start command."""
await self.handle_message(update, context)
async def run(self):
"""Run the Chat ID finder bot."""
self.application = Application.builder().token(self.token).build()
# Add handlers for any message or /start command
self.application.add_handler(CommandHandler("start", self.start_command))
self.application.add_handler(MessageHandler(filters.ALL, self.handle_message))
print("š¤ Chat ID finder bot is running...")
print("š¬ Now go to Telegram and send ANY message to your bot")
print("š Press Ctrl+C when done\n")
try:
await self.application.run_polling()
except KeyboardInterrupt:
print(f"\nš Chat ID finder stopped")
if self.found_chat_ids:
print(f"š Found Chat IDs: {', '.join(map(str, self.found_chat_ids))}")
else:
print("ā No Chat IDs found - make sure you messaged your bot!")
def main():
"""Main function."""
print("š Telegram Chat ID Finder\n")
# Get bot token
token = input("š¤ Enter your Telegram Bot Token: ").strip()
if not token:
print("ā No token provided!")
return
if ':' not in token:
print("ā Invalid token format! Should look like: 123456789:ABCdefGHIjklMNOPqrs")
return
print(f"\nā
Token looks valid!")
print(f"š Starting Chat ID finder bot...")
print(f"\nš± Instructions:")
print(f"1. Open Telegram")
print(f"2. Find your bot (search for the username you gave it)")
print(f"3. Send ANY message to your bot")
print(f"4. Come back here to see your Chat ID")
print(f"5. Copy the Chat ID to your .env file")
try:
bot = ChatIDBot(token)
asyncio.run(bot.run())
except Exception as e:
print(f"ā Error: {e}")
print("š” Make sure your bot token is correct")
if __name__ == "__main__":
main()