"""Helper script to fetch chat_id for users who message the bot. Usage: 1. Ensure TELEGRAM_BOT_TOKEN is set in your environment or in a .env file. 2. Run the script: python scripts/get_telegram_chat_id.py 3. Send a message to @Krushi_Mitra_Bot from the farmer's Telegram account. 4. The script will print recent updates with chat IDs. This script polls Telegram's getUpdates endpoint once and prints chat ids. """ import os import requests from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') if not TOKEN: print('Please set TELEGRAM_BOT_TOKEN in environment or .env') raise SystemExit(1) URL = f'https://api.telegram.org/bot{TOKEN}/getUpdates' resp = requests.get(URL, params={'timeout': 5}) if resp.status_code != 200: print('Failed to fetch updates:', resp.status_code, resp.text) raise SystemExit(1) data = resp.json() if not data.get('ok'): print('Telegram API error:', data) raise SystemExit(1) results = data.get('result', []) if not results: print('No recent updates. Ask the farmer to send a message to the bot and retry.') else: print('Recent updates:') for u in results: update_id = u.get('update_id') msg = u.get('message') or u.get('edited_message') or {} chat = msg.get('chat', {}) print(f"update_id={update_id} chat.id={chat.get('id')} chat.username={chat.get('username')} name={chat.get('first_name')} {chat.get('last_name')}") print('\nTip: copy the chat.id value and use the admin endpoint POST /admin/register_telegram/ with JSON {"chat_id": }')