Add UserAPI for register new user.
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
@@ -3,6 +3,7 @@ import simplematrixbotlib as botlib
|
|||||||
|
|
||||||
from src.handlers.help import help_handler
|
from src.handlers.help import help_handler
|
||||||
from src.handlers.city import list_cities_handler
|
from src.handlers.city import list_cities_handler
|
||||||
|
from src.handlers.user import register_handler
|
||||||
|
|
||||||
def setup(bot: botlib.Bot, prefix: str):
|
def setup(bot: botlib.Bot, prefix: str):
|
||||||
@bot.listener.on_message_event
|
@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'):
|
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)
|
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())
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
Reference in New Issue
Block a user