From eda62c9989cfa5ba3ea4b08ea0af4767261f1b64 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 17 Jun 2023 13:01:04 +0300 Subject: [PATCH] Add UserAPI for register new user. --- src/api/v2/user_api.py | 45 ++++++++++++++++++++++++++++++++++++++++ src/handlers/__init__.py | 4 ++++ src/handlers/user.py | 31 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/api/v2/user_api.py create mode 100644 src/handlers/user.py diff --git a/src/api/v2/user_api.py b/src/api/v2/user_api.py new file mode 100644 index 0000000..dc5f794 --- /dev/null +++ b/src/api/v2/user_api.py @@ -0,0 +1,45 @@ +import aiohttp +import asyncio +import ujson + +class UserAPI: + def __init__(self, fetcher, formatter): + """ + Sets needed classes: + + Fetcher (ApiFetcher) - class that will fetch data. + Format (AbstractFormat) - class that will format response data. + """ + self.__fetcher, self.__formatter = fetcher, formatter + + async def register(self, username: str, matrix_username: str, phone: str): + endpoint = "/api/v2/register" + + status, json_data = await self.__fetcher.send_json(endpoint, { + 'username': username, + 'matrix_username': matrix_username, + 'phone': phone, + }) + + if status != 200: + return {"error": "Сталася помилка, спробуйте пізніше."} + + match json_data: + case {"ok": message}: + return {"ok": 'Користувач успішно створений.'} + case {"error": 'A user with the username already exists!!!'}: + return {"error": "Користувач вже існує, будь ласка, оберіть інше ім'я"} + case {"error": error_message}: + return {"error": error_message} + + + async def register_user_message(self, username: str, matrix_username: str, phone: str) -> str: + response = await self.register(username, matrix_username, phone) + + match response: + case {"ok": message}: + return message + + case {"error": error_message}: + return error_message + diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py index 08f611c..b1ce3a3 100644 --- a/src/handlers/__init__.py +++ b/src/handlers/__init__.py @@ -3,6 +3,7 @@ import simplematrixbotlib as botlib from src.handlers.help import help_handler from src.handlers.city import list_cities_handler +from src.handlers.user import register_handler def setup(bot: botlib.Bot, prefix: str): @bot.listener.on_message_event @@ -15,3 +16,6 @@ def setup(bot: botlib.Bot, prefix: str): if match.is_not_from_this_bot() and match.prefix() and 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.is_not_from_this_bot() and match.prefix() and 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/user.py b/src/handlers/user.py new file mode 100644 index 0000000..676afa2 --- /dev/null +++ b/src/handlers/user.py @@ -0,0 +1,31 @@ +import simplematrixbotlib as botlib +import aiohttp +import asyncio +import ujson +from src.config.dots_bot_api_config import DotsBotApiConfig + +from src.api.v2.user_api import UserAPI +from src.fetcher.v2.api_fetcher import ApiFetcher + +dots_bot_api_config = DotsBotApiConfig() + +async def register_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str, args): + count = len(args) + + if count == 0: + await bot.api.send_markdown_message(room_id=room_id, message= + """ +Введіть Ім'я користувача, username та телефон 380?????????. +Приклад: `!register username 380671231212`. + """) + return + + session = aiohttp.ClientSession(json_serialize=ujson.dumps) + apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session) + userAPI = UserAPI(apiFetcher, None) + + username, phone = args[0], args[1] + msg: str = await userAPI.register_user_message(username, sender, phone) + + await bot.api.send_markdown_message(room_id=room_id, message=msg) +