63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
|
from typing import Union
|
||
|
|
||
|
from bson.son import SON
|
||
|
from convopyro import listen_message
|
||
|
from pykeyboard import ReplyButton, ReplyKeyboard
|
||
|
from pyrogram.types import Message, ReplyKeyboardRemove
|
||
|
|
||
|
from classes.location import Location
|
||
|
from classes.pyroclient import PyroClient
|
||
|
from modules.database import col_locations
|
||
|
from modules.search_name import search_name
|
||
|
|
||
|
|
||
|
async def search_nearby(app: PyroClient, message: Message) -> Union[Location, None]:
|
||
|
query = {
|
||
|
"location": {
|
||
|
"$within": {
|
||
|
"$center": [
|
||
|
[message.location.longitude, message.location.latitude],
|
||
|
app.config["search"]["radius"],
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
locations = await col_locations.find(query).limit(6).to_list()
|
||
|
|
||
|
if len(locations) == 0:
|
||
|
await message.reply_text(
|
||
|
"Could not find any locations nearby. Let's try using the name search."
|
||
|
)
|
||
|
return await search_name(app, message)
|
||
|
|
||
|
keyboard = ReplyKeyboard(resize_keyboard=True, row_width=2)
|
||
|
keyboard.add(*[ReplyButton(db_record["name"]) for db_record in locations])
|
||
|
|
||
|
await message.reply_text(
|
||
|
"Select the location using the keyboard", reply_markup=keyboard
|
||
|
)
|
||
|
|
||
|
while True:
|
||
|
answer = await listen_message(app, message.chat.id, 300)
|
||
|
location: Union[Location, None] = None
|
||
|
|
||
|
if answer is None or answer.text == "/cancel":
|
||
|
await message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
|
||
|
return
|
||
|
|
||
|
for db_record in locations:
|
||
|
if answer.text == db_record["name"]:
|
||
|
location = Location(**db_record)
|
||
|
break
|
||
|
|
||
|
if answer.text is None or location is None:
|
||
|
await answer.reply_text(
|
||
|
"Please, select a valid location using keyboard provided. Use /cancel if you want to cancel this operation."
|
||
|
)
|
||
|
continue
|
||
|
|
||
|
break
|
||
|
|
||
|
return location or await search_name(app, message)
|