Search code examples
pythonpython-3.xtelegram-bottelethon

Telethon receive location from user


What is a proper way to listen to "send location" messages from the user? My solution is to filter out messages by type of their media (process_location):

@bot.on(events.NewMessage(pattern=commands('/start')))
async def send_welcome(event: events.NewMessage.Event):
    await event.respond("Hello, I'm your helper bot!", buttons=[
        Button.request_location('Send location to detect timezone', resize=True),
    ])


@bot.on(events.NewMessage(func=lambda e: isinstance(e.media, MessageMediaGeo)))
async def process_location(event: events.NewMessage.Event):
    geo_data = event.media
    ...

But is there a better way to distinguish location messages? Couldn't find it in the docs.


Solution

  • As @Lonami suggested, I used e.geo:

    @bot.on(events.NewMessage(func=lambda e: e.geo))
    async def process_location(event: events.NewMessage.Event):
        geo_data = event.media
        ...