From 6bcaf782885b3fc9556b5be8a4ccc815f9a82a2f Mon Sep 17 00:00:00 2001 From: Akim Date: Fri, 26 Apr 2024 16:36:26 +0100 Subject: [PATCH 1/2] fixed incs2 channel handler issue fixed issue when message was longer than discord webhook limit --- plugins/incs2chat.py | 61 +++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/plugins/incs2chat.py b/plugins/incs2chat.py index 99c2613..092384f 100644 --- a/plugins/incs2chat.py +++ b/plugins/incs2chat.py @@ -80,7 +80,7 @@ def convert_message_to_markdown_text(message: Message) -> str: return ''.join(text) -def translate_text(text: str, source_lang: str = 'RU', target_lang: str = 'EN'): +def translate_text(text: str, source_lang: str = 'RU', target_lang: str = 'EN') -> str | None: headers = {'Authorization': f'DeepL-Auth-Key {config.DEEPL_TOKEN}', 'Content-Type': 'application/json'} data = {'text': [text], 'source_lang': source_lang, 'target_lang': target_lang} @@ -104,6 +104,44 @@ def post_to_discord_webhook(url: str, text: str): # todo: attachments support? logging.error(f'{r.status_code=}, {r.reason=}') +def process_ds_text(message: Message) -> list[str] | None: + logging.info(message) + + text_list = [convert_message_to_markdown_text(message)] + + # DS webhook limit 2000 symbols (ahui), hope tg limit is <4000 if more pls fix + if text_list[0]: + if len(text_list[0]) > 2000: + # splitting message into 2 similar-sized messages to save structure and beauty + last_newline_index = text_list[0][:len(text_list[0]) // 2].rfind('\n') + if last_newline_index != -1: + second_part = text_list[0][last_newline_index + 1:] + text_list.append(second_part) + text_list[0] = text_list[0][:last_newline_index] + + return text_list + + +def forward_to_discord(message: Message): + texts = process_ds_text(message) + logging.info(texts) + + if texts: + for text in texts: + post_to_discord_webhook(config.DS_WEBHOOK_URL, text) + post_to_discord_webhook(config.DS_WEBHOOK_URL_EN, translate_text(text, 'RU', 'EN')) + + message.continue_propagation() + + +async def cs_l10n_update(message: Message): + has_the_l10n_line = ((message.text and "Обновлены файлы локализации" in message.text) + or (message.caption and "Обновлены файлы локализации" in message.caption)) + + if has_the_l10n_line: + await message.reply_sticker('CAACAgIAAxkBAAID-l_9tlLJhZQSgqsMUAvLv0r8qhxSAAIKAwAC-p_xGJ-m4XRqvoOzHgQ') + + @Client.on_message(filters.chat(config.INCS2CHAT) & filters.command("ban")) async def ban(client: Client, message: Message): chat = await client.get_chat(config.INCS2CHAT) @@ -207,27 +245,14 @@ async def echo(client: Client, message: Message): return await reply_to.reply_voice(voice, quote=should_reply, caption=caption, caption_entities=entities) -@Client.on_message(filters.channel & filters.chat(config.INCS2CHANNEL)) -async def forward_to_discord(_, message: Message): - text = convert_message_to_markdown_text(message) - logging.info(text) - - if text: - post_to_discord_webhook(config.DS_WEBHOOK_URL, text) - post_to_discord_webhook(config.DS_WEBHOOK_URL_EN, translate_text(text, 'RU', 'EN')) - - message.continue_propagation() - - @Client.on_message(filters.linked_channel & filters.chat(config.INCS2CHAT)) -async def cs_l10n_update(_, message: Message): +async def handle_new_post(_, message: Message): is_sent_by_correct_chat = (message.sender_chat and message.sender_chat.id == config.INCS2CHANNEL) is_forwarded_from_correct_chat = (message.forward_from_chat and message.forward_from_chat.id == config.INCS2CHANNEL) - has_the_l10n_line = ((message.text and "Обновлены файлы локализации" in message.text) - or (message.caption and "Обновлены файлы локализации" in message.caption)) - if is_sent_by_correct_chat and is_forwarded_from_correct_chat and has_the_l10n_line: - await message.reply_sticker('CAACAgIAAxkBAAID-l_9tlLJhZQSgqsMUAvLv0r8qhxSAAIKAwAC-p_xGJ-m4XRqvoOzHgQ') + if is_sent_by_correct_chat and is_forwarded_from_correct_chat: + await cs_l10n_update(message) + forward_to_discord(message) @Client.on_message(filters.chat(config.INCS2CHAT) & filters.forwarded) From b376f3e9c56a4c82ef9c035f5825b1e072933094 Mon Sep 17 00:00:00 2001 From: Akim Date: Fri, 26 Apr 2024 16:42:45 +0100 Subject: [PATCH 2/2] fix --- plugins/incs2chat.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/incs2chat.py b/plugins/incs2chat.py index 092384f..6c0e041 100644 --- a/plugins/incs2chat.py +++ b/plugins/incs2chat.py @@ -131,8 +131,6 @@ def forward_to_discord(message: Message): post_to_discord_webhook(config.DS_WEBHOOK_URL, text) post_to_discord_webhook(config.DS_WEBHOOK_URL_EN, translate_text(text, 'RU', 'EN')) - message.continue_propagation() - async def cs_l10n_update(message: Message): has_the_l10n_line = ((message.text and "Обновлены файлы локализации" in message.text)