如何通过Telegram Bot获取用户位置的经纬度?
How to Get User Latitude and Longitude via Telegram Bot (Using Deep Links)
Hey there! Let me walk you through how to grab a user's latitude and longitude using a Telegram Bot, including how to use a deep link (which I think is what you're referring to with the "Link module") to trigger the location request. I'll use Python with the python-telegram-bot library—super straightforward for this kind of task.
What You'll Need First
- A Telegram Bot token: Chat with @BotFather on Telegram, send
/newbot, and follow the prompts to get your unique token. - Install the required library via pip:
pip install python-telegram-bot==20.7
Full Implementation Code
This code will let you send users a link to trigger a location request, then capture and display their latitude and longitude once they share it:
from telegram import Update, ReplyKeyboardMarkup, KeyboardButton from telegram.ext import ( ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ) # Replace this with your actual bot token from @BotFather BOT_TOKEN = "YOUR_BOT_TOKEN_HERE" async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): user = update.effective_user # Check if the /start command has our location request parameter if context.args and context.args[0] == "request_location": # Create a keyboard with a button that asks for the user's location location_button = KeyboardButton(text="Share Your Location", request_location=True) reply_markup = ReplyKeyboardMarkup([[location_button]], resize_keyboard=True) await update.message.reply_html( f"Hi {user.mention_html()}! Please share your location using the button below:", reply_markup=reply_markup ) else: # Send the default start message with our deep link deep_link = f"https://t.me/{context.bot.username}?start=request_location" await update.message.reply_html( f"Hi {user.mention_html()}! Click this link to share your location: {deep_link}" ) async def handle_location(update: Update, context: ContextTypes.DEFAULT_TYPE): # Extract latitude and longitude from the incoming location message user_location = update.effective_message.location latitude = user_location.latitude longitude = user_location.longitude # You can modify this part to save coordinates to a database or process them further await update.message.reply_html( f"Got it! Your coordinates are:\nLatitude: <b>{latitude}</b>\nLongitude: <b>{longitude}</b>" ) # Hide the location request keyboard after we get the data await update.message.reply_text("Thanks for sharing!", reply_markup=ReplyKeyboardMarkup.remove_keyboard()) def main(): # Initialize the bot application application = ApplicationBuilder().token(BOT_TOKEN).build() # Add handlers for commands and location messages application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.LOCATION, handle_location)) # Start polling for updates application.run_polling() if __name__ == "__main__": main()
How It Works
- Deep Link Trigger: When users click the link
https://t.me/YourBotUsername?start=request_location, the bot detects therequest_locationargument in the/startcommand and sends a custom keyboard with a "Share Your Location" button. - Location Capture: Once the user taps the button and shares their location, the
handle_locationfunction pulls thelatitudeandlongitudevalues from the message. You can easily adapt this function to store the coordinates in a database, use them for mapping, or any other logic you need.
Quick Notes
- Don't forget to replace
YOUR_BOT_TOKEN_HEREwith the actual token you got from @BotFather. - The
python-telegram-botv20 uses async/await syntax, so you'll need Python 3.7 or newer. - Feel free to tweak the button text, messages, or post-location logic to fit your bot's specific use case.
内容的提问来源于stack exchange,提问作者Saeed Heidarizarei
相关产品推荐
相关产品推荐

