-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 82d9df8
Showing
8 changed files
with
714 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,44 @@ | ||
# general things to ignore | ||
build/ | ||
dist/ | ||
docs/_sources/ | ||
docs/.doctrees | ||
.eggs/ | ||
*.egg-info/ | ||
*.egg | ||
*.py[cod] | ||
__pycache__/ | ||
*.so | ||
*~ | ||
|
||
# virtualenv | ||
env*/ | ||
venv/ | ||
.venv* | ||
.env/ | ||
|
||
# codecov / coverage | ||
.coverage | ||
cov_* | ||
coverage.xml | ||
|
||
# due to using tox and pytest | ||
.tox | ||
.cache | ||
.pytest_cache/ | ||
.python-version | ||
pip | ||
.mypy_cache/ | ||
|
||
# JetBrains PyCharm settings | ||
.idea/ | ||
|
||
tmp.txt | ||
.DS_Store | ||
logs/ | ||
|
||
env.example | ||
*.db | ||
|
||
.pytype/ | ||
.env* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,7 @@ | ||
Copyright 2022 Kazuhiro Sera @seratch | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,26 @@ | ||
display_information: | ||
name: DeepL Document Translator | ||
description: Generates translated documents from the ones uploaded in Slack | ||
background_color: "#0b1f5c" | ||
features: | ||
bot_user: | ||
display_name: DeepL Document Translator | ||
always_online: true | ||
oauth_config: | ||
scopes: | ||
bot: | ||
- channels:history | ||
- chat:write | ||
- files:read | ||
- files:write | ||
- groups:history | ||
- reactions:read | ||
- im:write | ||
- im:history | ||
settings: | ||
event_subscriptions: | ||
bot_events: | ||
- reaction_added | ||
interactivity: | ||
is_enabled: true | ||
socket_mode_enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,218 @@ | ||
import io | ||
import logging | ||
import os | ||
import time | ||
|
||
import deepl | ||
import requests | ||
from slack_bolt import App, Say, BoltContext, Ack | ||
from slack_bolt.adapter.socket_mode import SocketModeHandler | ||
from slack_sdk import WebClient | ||
|
||
from languages import detect_lang | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
app = App(token=os.environ["SLACK_BOT_TOKEN"]) | ||
|
||
translator = deepl.Translator(os.getenv("DEEPL_AUTH_KEY")) | ||
|
||
|
||
@app.event("app_home_opened") | ||
def handle_app_home_opened_events(context: BoltContext, client: WebClient): | ||
usage = translator.get_usage() | ||
character_spent_percentage = ( | ||
round((usage.character.count / usage.character.limit) * 100, 1) | ||
if usage.character.limit | ||
else 0 | ||
) | ||
character_validity = ( | ||
":red_circle:" | ||
if usage.character.limit and usage.character.valid is False | ||
else ":large_blue_circle:" | ||
) | ||
character_limit_exceeded = ( | ||
":red_circle:" | ||
if usage.character.limit_exceeded is True | ||
else ":large_blue_circle:" | ||
) | ||
document_spent_percentage = ( | ||
round((usage.document.count / usage.document.limit) * 100, 1) | ||
if usage.document.limit | ||
else 0 | ||
) | ||
document_validity = ( | ||
":red_circle:" | ||
if usage.document.limit and usage.document.valid is False | ||
else ":large_blue_circle:" | ||
) | ||
document_limit_exceeded = ( | ||
":red_circle:" | ||
if usage.document.limit_exceeded is True | ||
else ":large_blue_circle:" | ||
) | ||
client.views_publish( | ||
user_id=context.user_id, | ||
view={ | ||
"type": "home", | ||
"blocks": [ | ||
{ | ||
"type": "section", | ||
"block_id": "header", | ||
"text": {"type": "mrkdwn", "text": "*DeepL API Usage*"}, | ||
"accessory": { | ||
"type": "button", | ||
"action_id": "link-button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "Go to DeepL Account Page", | ||
}, | ||
"url": "https://www.deepl.com/pro-account/usage", | ||
"value": "clicked", | ||
}, | ||
}, | ||
{"type": "divider"}, | ||
{ | ||
"type": "section", | ||
"fields": [ | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Characters*\n" | ||
f"Budget: {usage.character.limit or '-'}\n" | ||
f"Spent: {usage.character.count} ({character_spent_percentage}%)\n" | ||
f"Valid: {character_validity}\n" | ||
f"Limit Exceeded: {character_limit_exceeded}", | ||
}, | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Documents*\n" | ||
f"Budget: {usage.document.limit or '-'}\n" | ||
f"Spent: {usage.document.count or '-'} ({document_spent_percentage}%)\n" | ||
f"Valid: {document_validity}\n" | ||
f"Limit Exceeded: {document_limit_exceeded}", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
) | ||
|
||
|
||
@app.action("link-button") | ||
def handle_some_action(ack: Ack): | ||
ack() | ||
|
||
|
||
@app.event("reaction_added") | ||
def handle_reaction_added_events( | ||
event: dict, say: Say, client: WebClient, logger: logging.Logger | ||
): | ||
item = event.get("item", {}) | ||
if item.get("type") != "message": | ||
logger.debug("Skipped non-message type item") | ||
return | ||
channel, ts = item.get("channel"), item.get("ts") | ||
if channel is None or ts is None: | ||
logger.debug("Skipped because either channel or ts is missing") | ||
return | ||
lang = detect_lang(event) | ||
if lang is None: | ||
logger.debug( | ||
f"Skipped because no lang detected from the reaction ({event.get('reaction')})" | ||
) | ||
return | ||
replies = client.conversations_replies( | ||
channel=channel, | ||
ts=ts, | ||
inclusive=True, | ||
) | ||
messages = replies.get("messages") | ||
parent_message = messages[0] | ||
if parent_message.get("thread_ts") is not None and parent_message.get( | ||
"ts" | ||
) != parent_message.get("thread_ts"): | ||
logger.debug( | ||
f"Skipped because this is a reaction to any of the thread replies ({event.get('reaction')})" | ||
) | ||
return | ||
if parent_message.get("files") is None: | ||
logger.debug("Skipped because the parent message does not any files") | ||
return | ||
|
||
thread_ts = parent_message.get("ts") | ||
|
||
for file in parent_message.get("files"): | ||
original_name = file.get("name") | ||
original_title = file.get("title") | ||
elements = original_name.split(".") | ||
elements[-2] = elements[-2] "_" lang | ||
translated_file_name = ".".join(elements) | ||
|
||
already_translated = False | ||
for message in replies.get("messages"): | ||
# check if the translated file is already uploaded | ||
if ( | ||
message.get("files") is not None | ||
and message.get("files")[0].get("name") == translated_file_name | ||
): | ||
# This lang version is already published | ||
logger.debug( | ||
f"This {lang} version of {original_name} is already available" | ||
) | ||
already_translated = True | ||
break | ||
if already_translated: | ||
continue | ||
|
||
# call DeepL API | ||
input_document = requests.get( | ||
url=file.get("url_private_download"), | ||
headers={"Authorization": f"Bearer {client.token}"}, | ||
).content | ||
output_document = io.BytesIO() | ||
try: | ||
handle = translator.translate_document_upload( | ||
input_document, | ||
target_lang=lang.upper(), | ||
filename=translated_file_name, | ||
) | ||
|
||
try: | ||
status = translator.translate_document_get_status(handle) | ||
while status.ok and not status.done: | ||
secs = (status.seconds_remaining or 0) / 2.0 1.0 | ||
secs = max(1.0, min(secs, 60.0)) | ||
time.sleep(secs) | ||
status = translator.translate_document_get_status(handle) | ||
|
||
if status.ok: | ||
translator.translate_document_download(handle, output_document) | ||
except Exception as e: | ||
say( | ||
thread_ts=thread_ts, | ||
text=f"Failed to translate the document (error: {e})", | ||
) | ||
return | ||
|
||
if not status.ok: | ||
say( | ||
thread_ts=thread_ts, | ||
text=f"Failed to translate the document for some reason", | ||
) | ||
return | ||
|
||
output_document.seek(0) | ||
client.files_upload( | ||
title=f"{original_title} ({lang})", | ||
filename=translated_file_name, | ||
file=output_document, | ||
channels=[channel], | ||
thread_ts=thread_ts, | ||
initial_comment=f"Here is the {lang} translation of {original_name} :wave:", | ||
) | ||
finally: | ||
output_document.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start() |
Oops, something went wrong.