2021-09-25 16:49:11 +02:00
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
from config.contents import mention_failed
|
|
|
|
from entity.user import User
|
2021-09-28 21:28:33 +02:00
|
|
|
from exception.invalidArgumentException import InvalidArgumentException
|
2021-09-25 16:49:11 +02:00
|
|
|
from repository.userRepository import UserRepository
|
|
|
|
from telegram.ext.callbackcontext import CallbackContext
|
|
|
|
from telegram.ext.commandhandler import CommandHandler
|
|
|
|
from telegram.update import Update
|
|
|
|
|
|
|
|
from handler.abstractHandler import AbstractHandler
|
|
|
|
|
|
|
|
|
|
|
|
class MentionHandler(AbstractHandler):
|
2021-09-28 17:03:11 +02:00
|
|
|
bot_handler: CommandHandler
|
|
|
|
user_repository: UserRepository
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2021-09-28 17:03:11 +02:00
|
|
|
self.bot_handler = CommandHandler('everyone', self.handle)
|
|
|
|
self.user_repository = UserRepository()
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
def handle(self, update: Update, context: CallbackContext) -> None:
|
2021-09-28 21:28:33 +02:00
|
|
|
try:
|
|
|
|
updateData = self.get_update_data(update, context)
|
|
|
|
except InvalidArgumentException as e:
|
|
|
|
return self.reply_markdown(update, str(e))
|
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
users = self.user_repository.get_all_for_chat(updateData.chat_id)
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
if users:
|
2021-09-28 21:28:33 +02:00
|
|
|
return self.reply_markdown(update, self.build_mention_message(users))
|
2021-09-25 16:49:11 +02:00
|
|
|
|
2021-09-28 21:28:33 +02:00
|
|
|
self.reply_markdown(update, mention_failed)
|
2021-09-25 16:49:11 +02:00
|
|
|
|
2021-09-28 17:03:11 +02:00
|
|
|
def get_bot_handler(self) -> CommandHandler:
|
|
|
|
return self.bot_handler
|
2021-09-25 16:49:11 +02:00
|
|
|
|
2021-09-28 21:28:33 +02:00
|
|
|
def build_mention_message(self, users: Iterable[User]) -> str:
|
2021-09-25 16:49:11 +02:00
|
|
|
result = ''
|
|
|
|
|
|
|
|
for user in users:
|
2021-09-28 21:28:33 +02:00
|
|
|
result += f'*[{user.username}](tg://user?id={user.user_id})* '
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
return result
|