scalperBot / test /get_chat_id.py
nexusbert's picture
Upload 36 files
96e0cc2 verified
#!/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()