diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py index c1c0298..3672251 100644 --- a/src/handlers/__init__.py +++ b/src/handlers/__init__.py @@ -5,6 +5,7 @@ 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.category import category_list_handler +from src.handlers.cart import cart_list_handler, add_item_handler from src.handlers.user import register_handler from src.fmt.city_formatter import CityFormatter @@ -50,3 +51,14 @@ def setup(bot: botlib.Bot, prefix: str): if match.command('category-list'): await category_list_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id) + if match.command('cart'): + await cart_list_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id) + + if match.command('add-item'): + formatter = CompanyFormatter() + line = formatter.get_name_from_parts(match.args()) + + name, count = line.split(',') + count = int(count) + await add_item_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, item_name=name, count=count) + diff --git a/src/handlers/cart.py b/src/handlers/cart.py new file mode 100644 index 0000000..8a59bda --- /dev/null +++ b/src/handlers/cart.py @@ -0,0 +1,35 @@ +import simplematrixbotlib as botlib +import aiohttp +import asyncio +import ujson +from src.config.dots_bot_api_config import DotsBotApiConfig + +from src.api.v2.cart_api import CartAPI +from src.fetcher.v2.api_fetcher import ApiFetcher +from src.fmt.item_formatter import ItemFormatter +from src.fmt.cart_item_formatter import CartItemFormatter + +dots_bot_api_config = DotsBotApiConfig() + +async def cart_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 = CartItemFormatter() + cartAPI = CartAPI(apiFetcher, formatter) + + msg: str = await cartAPI.get_objects_message(sender) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) + +async def add_item_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str, item_name: str, count: int): + + session = aiohttp.ClientSession(json_serialize=ujson.dumps) + apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session) + formatter = ItemFormatter() + cartAPI = CartAPI(apiFetcher, formatter) + + msg: str = await cartAPI.add_object_message(item_name, count, sender) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) +