2021-09-25 16:49:11 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
from dataclasses import dataclass
|
2021-09-28 21:28:33 +02:00
|
|
|
import re
|
2021-09-28 17:03:11 +02:00
|
|
|
|
2021-09-25 16:49:11 +02:00
|
|
|
import names
|
2021-09-28 21:28:33 +02:00
|
|
|
from telegram.ext.callbackcontext import CallbackContext
|
2021-09-25 16:49:11 +02:00
|
|
|
from telegram.update import Update
|
2021-09-28 21:28:33 +02:00
|
|
|
from entity.group import Group
|
|
|
|
|
|
|
|
from exception.invalidArgumentException import InvalidArgumentException
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
@dataclass
|
2021-09-25 16:49:11 +02:00
|
|
|
class UpdateData():
|
2021-09-28 17:03:11 +02:00
|
|
|
user_id: str
|
|
|
|
chat_id: str
|
2021-09-25 16:49:11 +02:00
|
|
|
username: str
|
|
|
|
|
|
|
|
@staticmethod
|
2021-09-28 21:28:33 +02:00
|
|
|
def create_from_arguments(update: Update, context: CallbackContext) -> UpdateData:
|
2021-09-28 17:03:11 +02:00
|
|
|
chat_id = str(update.effective_chat.id)
|
2021-09-28 21:28:33 +02:00
|
|
|
|
|
|
|
if context.args and context.args[0]:
|
2021-09-28 23:14:14 +02:00
|
|
|
group_name = str(context.args[0])
|
|
|
|
if not context.args[0].isalpha():
|
|
|
|
raise InvalidArgumentException(re.escape('Group name must contain only letters.'))
|
|
|
|
|
|
|
|
if context.args[0] == Group.default_name:
|
|
|
|
raise InvalidArgumentException(re.escape(f'Group can not be `{Group.default_name}`.'))
|
|
|
|
|
|
|
|
if len(context.args[0]) > 20:
|
|
|
|
raise InvalidArgumentException(re.escape(f'Group name length can not be greater than 20.'))
|
|
|
|
|
|
|
|
chat_id += f'~{context.args[0]}'.lower()
|
2021-09-28 21:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
user_id = str(update.effective_user.id)
|
2021-09-25 16:49:11 +02:00
|
|
|
username = update.effective_user.username or update.effective_user.first_name
|
|
|
|
|
|
|
|
if not username:
|
|
|
|
username = names.get_first_name()
|
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
return UpdateData(user_id, chat_id, username)
|