parent
50abd54276
commit
eda62c9989
@ -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
|
||||||
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in new issue