Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add english translation for ds webhook #13

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions plugins/incs2chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# noinspection PyUnresolvedReferences
import env
import config
import ujson


def correct_message_entity(entities: list[MessageEntity] | None,
Expand All @@ -34,6 +35,30 @@ def correct_message_entity(entities: list[MessageEntity] | None,
return entities


def translate_text(text: str):
headers = {"Authorization": f"DeepL-Auth-Key {config.DEEPL_TOKEN}", 'Content-Type': 'application/json'}
data = {'text': [text], 'target_lang': 'EN', "source_lang": "RU"}

r = requests.post('https://api-free.deepl.com/v2/translate', headers=headers, json=data)
if r.status_code == 200:
content = ujson.loads(r.text)
return content['translations'][0]['text']

logging.error('Failed to translate text')
logging.error(f'{text=}')
logging.error(f'{r.status_code=}, {r.reason=}')


def post_ds_webhook(text: str, url: str):
headers = {"Content-Type": "application/json"}
payload = {"content": text}
r = requests.post(url, json=payload, headers=headers)
if r.status_code != 204: # Discord uses 204 as a success code (yikes)
logging.error('Failed to post to Discord webhook.')
logging.error(f'{text}')
logging.error(f'{r.status_code=}, {r.reason=}')


@Client.on_message(filters.chat(config.INCS2CHAT) & filters.command("ban"))
async def ban(client: Client, message: Message):
chat = await client.get_chat(config.INCS2CHAT)
Expand Down Expand Up @@ -138,18 +163,14 @@ async def echo(client: Client, message: Message):


@Client.on_message(filters.channel & filters.chat(config.INCS2CHANNEL))
async def repost_to_discord(_, message: Message):
if message.text is None and message.caption is None:
return message.continue_propagation()
async def forward_to_discord(_, message: Message):

payload = {"content": message.text if message.text is not None else message.caption} # todo: attachments support?
headers = {"Content-Type": "application/json"} # todo: sending embeds?
text = message.text if message.text is not None else message.caption # todo: attachments support? // idi nahooy

if text:
post_ds_webhook(text, config.DS_WEBHOOK_URL)

r = requests.post(config.DS_WEBHOOK_URL, json=payload, headers=headers)
if r.status_code != 204: # Discord uses 204 as a success code (yikes)
logging.error('Failed to post to Discord webhook.')
logging.error(f'{message=}')
logging.error(f'{r.status_code=}, {r.reason=}')
post_ds_webhook(translate_text(text), config.DS_WEBHOOK_URL_EN)

message.continue_propagation()

Expand Down
Loading