L
L
Limonardo2020-10-11 20:54:11
Python
Limonardo, 2020-10-11 20:54:11

How to get a city by telegram bot geodata?

Good afternoon! I started learning Python and decided to write my first telegram bot.
There was a problem, it is impossible to write a script that would determine the user's city by coordinates.
Below is the code:

import telebot
from telebot import types
import keyboard #
from geopy.geocoders import Nominatim

bot = telebot.TeleBot("1354013020:AAHfxxYu6JV32xcGKb7wh5xx5Lf8u_q12xs")

@bot.message_handler(commands=["start"])
def start (message):
# Keyboard with Request Location Button
keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
button_geo = types.KeyboardButton(text="Submit Location", request_location=True)
keyboard.add(button_geo)
bot.send_message(message.chat.id, "Share location", reply_markup=keyboard)

geolocator = Nominatim(user_agent = "name_of_your_app") #Get

location
@bot.message_handler(content_types=['location'])
def location (message) :
print(message.location.latitude, message.location.longitude) #Determine the

city
location = geolocator.reverse(message.location.latitude, message.location.longitude )
print(location.address) Resulting

in an error: reverse() takes 2 positional arguments but 3 were given
The error shows that message is an extra value for reverse() and only latitude and longitude should be passed, but only
I don't know how to select them from message.location.latitude, message.location.longitude .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-10-11
@SoreMix

Look at the usage example - https://geopy.readthedocs.io/en/stable/
Coordinates must be passed as the first parameter as a string, where the coordinates are separated by a space
You can, for example, like this

location = geolocator.reverse('{} {}'.format(message.location.latitude, message.location.longitude))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question