如何用Python Telegram Bot及API获取GetUpdates原始会话信息
telegram.ext in Python Hey there! I get that you're new to building Telegram Bots with Python and want to replicate the raw update data (including chat_id, user_id, and all other fields) you used to fetch via the browser's getUpdates API using the telegram.ext library. Let me walk you through two straightforward ways to do this right in your Python terminal.
First, make sure you have the latest version of the python-telegram-bot library installed (since telegram.ext is part of this package):
pip install python-telegram-bot --upgrade
Method 1: Fetch Raw Updates One-Time
This method mimics what you did in the browser—pulling the latest raw update data on demand:
from telegram import Update from telegram.ext import Updater # Replace with your actual Bot Token from @BotFather BOT_TOKEN = "your_bot_token_here" def main(): # Initialize the Updater with your bot token updater = Updater(BOT_TOKEN) # Fetch raw updates (offset=-1 gets the most recent update; timeout waits for new messages) raw_updates = updater.bot.get_updates(offset=-1, timeout=10) # Loop through each update to inspect the raw data for update in raw_updates: print("--- Raw Update Data ---") # Convert the Update object to a dictionary to see all fields (matches browser JSON) print(update.to_dict()) # Extract specific fields if needed, just like you did before if update.message: chat_id = update.message.chat.id user_id = update.message.from_user.id print(f"\nExtracted chat_id: {chat_id}, user_id: {user_id}") if __name__ == "__main__": main()
When you run this, it’ll pull the latest update and print the full raw data structure—exactly what you saw in the browser. The offset=-1 parameter ensures you only get the most recent update (adjust this if you want older ones), and timeout lets the script wait a few seconds for new messages if none exist yet.
Method 2: Listen for Real-Time Raw Updates
If you want to continuously listen for new updates (instead of fetching once), use a RawUpdateHandler to capture every incoming raw update as it happens:
from telegram import Update from telegram.ext import Updater, RawUpdateHandler BOT_TOKEN = "your_bot_token_here" def handle_raw_updates(update: Update, context): print("\n--- New Raw Update Received ---") # Print the full raw update dictionary print(update.to_dict()) # Extract key fields for quick reference if update.message: chat_id = update.message.chat.id user_id = update.message.from_user.id print(f"Chat ID: {chat_id}, User ID: {user_id}") def main(): updater = Updater(BOT_TOKEN) dispatcher = updater.dispatcher # Add the RawUpdateHandler to catch all incoming updates dispatcher.add_handler(RawUpdateHandler(handle_raw_updates)) # Start polling for updates updater.start_polling() print("Bot is running and listening for raw updates... Press Ctrl+C to stop.") updater.idle() if __name__ == "__main__": main()
This script will keep running in your terminal, printing every raw update as soon as it arrives. The RawUpdateHandler doesn’t filter anything—it passes the full Update object straight to your handler function, so you get all the same data as the browser’s getUpdates endpoint.
Both methods give you access to the complete raw session data you need, no filtered messages only. Just drop in your bot token and run the script!
内容的提问来源于stack exchange,提问作者Emmanuel Batse




