1

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.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • 1
    Your current way is fine, although the `isinstance` check can be simplified to `e.geo`. – Lonami Sep 06 '21 at 18:06
  • @Lonami That is exactly what I was looking for! Thank you. You can compile this into an answer, I will accept it. Think it can be useful. – Yevhen Kuzmovych Sep 07 '21 at 08:30

1 Answers1

0

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
    ...
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48