2021-09-25 16:49:11 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-06 19:44:03 +02:00
|
|
|
from dataclasses import dataclass
|
2021-09-28 17:03:11 +02:00
|
|
|
|
2021-09-25 16:49:11 +02:00
|
|
|
import names
|
2021-11-12 12:23:54 +01:00
|
|
|
import re
|
2021-10-06 19:44:03 +02:00
|
|
|
from telegram.ext.callbackcontext import CallbackContext
|
|
|
|
from telegram.update import Update
|
2021-09-25 16:49:11 +02:00
|
|
|
|
2021-11-24 14:28:09 +01:00
|
|
|
from exception.invalidActionException import InvalidActionException
|
2021-10-08 15:25:47 +02:00
|
|
|
from validator.accessValidator import AccessValidator
|
2021-10-07 19:15:53 +02:00
|
|
|
from validator.groupNameValidator import GroupNameValidator
|
|
|
|
|
2021-09-25 16:49:11 +02:00
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
@dataclass
|
2021-10-11 17:20:39 +02:00
|
|
|
class InboundMessage:
|
2021-09-28 17:03:11 +02:00
|
|
|
user_id: str
|
|
|
|
chat_id: str
|
2021-10-06 19:44:03 +02:00
|
|
|
group_name: str
|
2021-09-25 16:49:11 +02:00
|
|
|
username: str
|
|
|
|
|
2021-10-14 20:24:58 +02:00
|
|
|
default_group: str = 'default'
|
|
|
|
|
2021-09-25 16:49:11 +02:00
|
|
|
@staticmethod
|
2021-10-11 17:20:39 +02:00
|
|
|
def create(update: Update, context: CallbackContext, group_specific: bool) -> InboundMessage:
|
2021-10-08 15:25:47 +02:00
|
|
|
user_id = str(update.effective_user.id)
|
|
|
|
AccessValidator.validate(user_id)
|
|
|
|
|
2021-11-24 14:28:09 +01:00
|
|
|
if update.edited_message:
|
|
|
|
raise InvalidActionException
|
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
chat_id = str(update.effective_chat.id)
|
2021-10-14 20:24:58 +02:00
|
|
|
group_name = InboundMessage.default_group
|
2021-10-06 19:44:03 +02:00
|
|
|
|
2021-11-12 12:23:54 +01:00
|
|
|
# done upon resolving a command action
|
2021-10-11 17:20:39 +02:00
|
|
|
if context.args and context.args[0] and group_specific:
|
2021-10-05 19:20:04 +02:00
|
|
|
group_name = str(context.args[0]).lower()
|
2021-09-28 23:14:14 +02:00
|
|
|
|
2021-10-07 19:15:53 +02:00
|
|
|
GroupNameValidator.validate(group_name)
|
2021-09-28 23:14:14 +02:00
|
|
|
|
2021-11-12 12:23:54 +01:00
|
|
|
# done upon resolving a message handler action
|
2021-11-24 14:28:09 +01:00
|
|
|
if '@' in update.message.text and update.message.text[0] != '/':
|
2021-11-12 12:23:54 +01:00
|
|
|
searched_message_part = [part for part in update.message.text.split(' ') if '@' in part][0]
|
2021-11-12 13:15:00 +01:00
|
|
|
group_name = re.sub(r'\W+', '', searched_message_part).lower()
|
2021-11-12 12:23:54 +01:00
|
|
|
|
|
|
|
if group_name in GroupNameValidator.FORBIDDEN_GROUP_NAMES:
|
|
|
|
group_name = InboundMessage.default_group
|
|
|
|
|
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-10-11 17:20:39 +02:00
|
|
|
return InboundMessage(user_id, chat_id, group_name, username)
|