2021-04-09 10:17:38 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-04-12 19:47:21 +02:00
|
|
|
from idm.api.proto.chat_manager_service_pb2 import Chat as ChatPb
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.bot.application import TelegramApplication
|
|
|
|
from nexus.translations import t
|
|
|
|
from telethon import Button
|
|
|
|
|
|
|
|
top_languages = {
|
|
|
|
'am': '🇪🇹',
|
|
|
|
'ar': '🇦🇪',
|
|
|
|
'de': '🇩🇪',
|
|
|
|
'en': '🇬🇧',
|
|
|
|
'es': '🇪🇸',
|
|
|
|
'fa': '🇮🇷',
|
|
|
|
'hi': '🇮🇳',
|
|
|
|
'id': '🇮🇩',
|
|
|
|
'it': '🇮🇹',
|
|
|
|
'ja': '🇯🇵',
|
|
|
|
'ms': '🇲🇾',
|
|
|
|
'pb': '🇧🇷',
|
|
|
|
'ru': '🇷🇺',
|
|
|
|
'tg': '🇹🇯',
|
|
|
|
'uk': '🇺🇦',
|
|
|
|
'uz': '🇺🇿',
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean_emoji = {
|
|
|
|
False: '❎',
|
|
|
|
True: '✅️',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-11 16:39:25 +02:00
|
|
|
class SettingsWidget:
|
2021-04-09 10:17:38 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
application: TelegramApplication,
|
2021-04-12 19:47:21 +02:00
|
|
|
chat: ChatPb,
|
2021-04-09 10:17:38 +02:00
|
|
|
has_language_buttons: Optional[bool] = None,
|
|
|
|
is_group_mode: bool = False,
|
|
|
|
request_id: Optional[str] = None,
|
|
|
|
):
|
|
|
|
self.application = application
|
|
|
|
self.chat = chat
|
|
|
|
self.has_language_buttons = has_language_buttons
|
|
|
|
if self.has_language_buttons is None:
|
|
|
|
self.has_language_buttons = self.application.config['application']['views']['settings']['has_language_buttons']
|
|
|
|
self.is_group_mode = is_group_mode
|
|
|
|
self.request_id = request_id
|
|
|
|
self._actions = {
|
|
|
|
'sl': self._switch_language,
|
|
|
|
'ssm': self._switch_system_messaging,
|
|
|
|
'sd': self._switch_discovery,
|
2022-09-02 17:44:56 +02:00
|
|
|
'sc': self._switch_connectome,
|
2021-04-09 10:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async def _switch_language(self, target_language: str):
|
2021-04-11 16:39:25 +02:00
|
|
|
self.chat = await self.application.idm_client.update_chat(
|
2021-04-12 19:47:21 +02:00
|
|
|
chat_id=self.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
language=target_language,
|
|
|
|
request_id=self.request_id,
|
|
|
|
)
|
2021-04-11 16:39:25 +02:00
|
|
|
return self.chat
|
2021-04-09 10:17:38 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
async def _switch_connectome(self, is_connectome_enabled: str):
|
|
|
|
self.chat = await self.application.idm_client.update_chat(
|
|
|
|
chat_id=self.chat.chat_id,
|
|
|
|
is_connectome_enabled=bool(int(is_connectome_enabled)),
|
|
|
|
request_id=self.request_id,
|
|
|
|
)
|
|
|
|
return self.chat
|
|
|
|
|
2021-04-09 10:17:38 +02:00
|
|
|
async def _switch_system_messaging(self, is_system_messaging_enabled: str):
|
|
|
|
self.chat = await self.application.idm_client.update_chat(
|
2021-04-12 19:47:21 +02:00
|
|
|
chat_id=self.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
is_system_messaging_enabled=bool(int(is_system_messaging_enabled)),
|
|
|
|
request_id=self.request_id,
|
|
|
|
)
|
|
|
|
return self.chat
|
|
|
|
|
|
|
|
async def _switch_discovery(self, is_discovery_enabled: str):
|
|
|
|
self.chat = await self.application.idm_client.update_chat(
|
2021-04-12 19:47:21 +02:00
|
|
|
chat_id=self.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
is_discovery_enabled=bool(int(is_discovery_enabled)),
|
|
|
|
request_id=self.request_id,
|
|
|
|
)
|
|
|
|
return self.chat
|
|
|
|
|
|
|
|
async def process_action(self, action_id: str, data: str):
|
|
|
|
old_chat = self.chat
|
|
|
|
await self._actions[action_id](data)
|
|
|
|
return old_chat != self.chat
|
|
|
|
|
|
|
|
async def render(self):
|
2022-09-02 17:44:56 +02:00
|
|
|
text = t('SETTINGS_TEMPLATE', self.chat.language).format(
|
2021-04-09 10:17:38 +02:00
|
|
|
bot_version=self.application.config['application']['bot_version'],
|
|
|
|
nexus_version=self.application.config['application']['nexus_version'],
|
|
|
|
language=top_languages.get(self.chat.language, self.chat.language),
|
|
|
|
)
|
|
|
|
if not self.is_group_mode and self.application.config['application']['views']['settings']['has_discovery_button']:
|
2022-09-02 17:44:56 +02:00
|
|
|
text = f"{text}\n\n{t('NEXUS_DISCOVERY_DESCRIPTION', self.chat.language)}"
|
|
|
|
if not self.is_group_mode and self.application.config['application']['views']['settings']['has_connectome_button']:
|
|
|
|
text = f"{text}\n\n{t('NEXUS_CONNECTOME_DESCRIPTION', self.chat.language)}"
|
2021-04-09 10:17:38 +02:00
|
|
|
buttons = []
|
|
|
|
if self.has_language_buttons:
|
|
|
|
buttons.append([])
|
|
|
|
for language in sorted(top_languages):
|
|
|
|
if len(buttons[-1]) >= 4:
|
|
|
|
buttons.append([])
|
|
|
|
buttons[-1].append(
|
|
|
|
Button.inline(
|
|
|
|
text=top_languages[language],
|
|
|
|
data=f'/settings_sl_{language}'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.is_group_mode:
|
|
|
|
return text, buttons
|
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
last_line = []
|
2021-04-09 10:17:38 +02:00
|
|
|
if self.application.config['application']['views']['settings']['has_discovery_button']:
|
2022-09-02 17:44:56 +02:00
|
|
|
last_line.append(Button.inline(
|
|
|
|
text=(
|
|
|
|
f'{t("DISCOVERY_OPTION", self.chat.language)}: '
|
|
|
|
f'{boolean_emoji[self.chat.is_discovery_enabled]}'
|
|
|
|
),
|
|
|
|
data=f'/settings_sd_{1 - int(self.chat.is_discovery_enabled)}'
|
|
|
|
))
|
|
|
|
if self.application.config['application']['views']['settings']['has_connectome_button']:
|
|
|
|
last_line.append(Button.inline(
|
|
|
|
text=(
|
|
|
|
f'{t("CONNECTOME_OPTION", self.chat.language)}: '
|
|
|
|
f'{boolean_emoji[self.chat.is_connectome_enabled]}'
|
|
|
|
),
|
|
|
|
data=f'/settings_sc_{1 - int(self.chat.is_connectome_enabled)}'
|
|
|
|
))
|
|
|
|
if last_line:
|
|
|
|
buttons.append(last_line)
|
2021-04-09 10:17:38 +02:00
|
|
|
return text, buttons
|