From acdf8f72de2784f7edf25d52c566f640ce1653f7 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 20 Jun 2023 15:14:00 +0300 Subject: [PATCH] Add the handler to select a city. --- src/handlers/__init__.py | 9 ++++++++- src/handlers/city.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py index dd79475..c41d64a 100644 --- a/src/handlers/__init__.py +++ b/src/handlers/__init__.py @@ -2,9 +2,11 @@ import nio import simplematrixbotlib as botlib from src.handlers.help import help_handler -from src.handlers.city import list_cities_handler +from src.handlers.city import list_cities_handler, select_city_handler from src.handlers.user import register_handler +from src.fmt.city_formatter import CityFormatter + def setup(bot: botlib.Bot, prefix: str): @bot.listener.on_message_event async def help_command(room: nio.rooms.MatrixRoom, message: nio.events.room_events.Event): @@ -22,6 +24,11 @@ def setup(bot: botlib.Bot, prefix: str): if match.command('cities-list'): await list_cities_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id) + if match.command('select-city'): + formatter = CityFormatter() + city_name = formatter.get_name_from_parts(match.args()) + await select_city_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, city_name=city_name) + if match.command('register'): await register_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, args=match.args()) diff --git a/src/handlers/city.py b/src/handlers/city.py index d841b44..771a18b 100644 --- a/src/handlers/city.py +++ b/src/handlers/city.py @@ -21,3 +21,14 @@ async def list_cities_handler(room_id: str, bot: botlib.Bot, sender: str, admin_ await bot.api.send_markdown_message(room_id=room_id, message=msg) +async def select_city_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str, city_name: str): + + session = aiohttp.ClientSession(json_serialize=ujson.dumps) + apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session) + formatter = CityFormatter() + cityAPI = CityAPI(apiFetcher, formatter) + + msg: str = await cityAPI.select_object_message(city_name, sender) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) +