From fc33d5c63f75f0a5a690401d68028a0394dba270 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 20 Jun 2023 17:40:35 +0300 Subject: [PATCH] Add the company handlers. --- src/handlers/__init__.py | 12 ++++++++++++ src/handlers/company.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/handlers/company.py diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py index b6eea86..de78f74 100644 --- a/src/handlers/__init__.py +++ b/src/handlers/__init__.py @@ -3,9 +3,11 @@ import simplematrixbotlib as botlib from src.handlers.help import help_handler from src.handlers.city import city_list_handler, select_city_handler +from src.handlers.company import company_list_handler, select_company_handler from src.handlers.user import register_handler from src.fmt.city_formatter import CityFormatter +from src.fmt.company_formatter import CompanyFormatter def setup(bot: botlib.Bot, prefix: str): @bot.listener.on_message_event @@ -34,3 +36,13 @@ def setup(bot: botlib.Bot, prefix: str): 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()) + # A registered user required + + if match.command('company-list'): + await company_list_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id) + + if match.command('select-company'): + formatter = CompanyFormatter() + company_name = formatter.get_name_from_parts(match.args()) + await select_company_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, company_name=company_name) + diff --git a/src/handlers/company.py b/src/handlers/company.py new file mode 100644 index 0000000..abafb93 --- /dev/null +++ b/src/handlers/company.py @@ -0,0 +1,34 @@ +import simplematrixbotlib as botlib +import aiohttp +import asyncio +import ujson +from src.config.dots_bot_api_config import DotsBotApiConfig + +from src.api.v2.company_api import CompanyAPI +from src.fetcher.v2.api_fetcher import ApiFetcher +from src.fmt.company_formatter import CompanyFormatter + +dots_bot_api_config = DotsBotApiConfig() + +async def company_list_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str): + + session = aiohttp.ClientSession(json_serialize=ujson.dumps) + apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session) + formatter = CompanyFormatter() + companyAPI = CompanyAPI(apiFetcher, formatter) + + msg: str = await companyAPI.get_objects_message(sender) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) + +async def select_company_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str, company_name: str): + + session = aiohttp.ClientSession(json_serialize=ujson.dumps) + apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session) + formatter = CompanyFormatter() + companyAPI = CompanyAPI(apiFetcher, formatter) + + msg: str = await companyAPI.select_object_message(company_name, sender) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) +