generated from cds-snc/project-template
-
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.
feat: add the /secret command for encrypted-message
- Loading branch information
1 parent
b3f3e8c
commit 5438cf0
Showing
6 changed files
with
485 additions
and
1 deletion.
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,18 @@ | ||
en-US: | ||
locale_button: "Français" | ||
locale_value: "en-US" | ||
locale_short: "en" | ||
submit: "Submit" | ||
close: "Close" | ||
title: "Share a new secret" | ||
label: "Secret" | ||
placeholder: "Enter your secret here" | ||
ttl: "Time to live" | ||
days: "days" | ||
day: "day" | ||
hours: "hours" | ||
hour: "hour" | ||
minutes: "minutes" | ||
minute: "minute" | ||
error: "There was an error creating your secret" | ||
link_available: "Your secret is available at the following link:" |
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,18 @@ | ||
fr-FR: | ||
locale_button: "English" | ||
locale_value: "fr-FR" | ||
locale_short: "fr" | ||
submit: "Soumettre" | ||
close: "Fermer" | ||
title: "Partager secret" | ||
label: "Secret" | ||
placeholder: "Entrez votre secret ici" | ||
ttl: "Durée de vie" | ||
days: "jours" | ||
day: "jour" | ||
hours: "heures" | ||
hour: "heure" | ||
minutes: "minutes" | ||
minute: "minute" | ||
error: "Il y avait une erreur en créant votre secret" | ||
link_available: "Votre secret est disponible au lien suivant:" |
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,162 @@ | ||
import i18n | ||
import requests | ||
import time | ||
from commands.utils import get_user_locale | ||
|
||
i18n.load_path.append("./commands/locales/") | ||
|
||
i18n.set("locale", "en-US") | ||
i18n.set("fallback", "en-US") | ||
|
||
|
||
def secret_command(client, ack, command, body): | ||
ack() | ||
if "user" in body: | ||
user_id = body["user"]["id"] | ||
else: | ||
user_id = body["user_id"] | ||
locale = get_user_locale(user_id, client) | ||
i18n.set("locale", locale) | ||
view = generate_secret_command_modal_view(command, user_id, locale) | ||
client.views_open(trigger_id=body["trigger_id"], view=view) | ||
|
||
|
||
def secret_view_handler(ack, client, view): | ||
ack() | ||
locale = view["blocks"][0]["elements"][0]["value"] | ||
i18n.set("locale", locale) | ||
secret = view["state"]["values"]["secret_input"]["secret_submission"]["value"] | ||
ttl = view["state"]["values"]["product"]["secret_ttl"]["selected_option"]["value"] | ||
|
||
# Encrypted message API | ||
endpoint = "https://encrypted-message.cdssandbox.xyz/encrypt" | ||
json = {"body": secret, "ttl": int(ttl) + int(time.time())} | ||
response = requests.post( | ||
endpoint, json=json, headers={"Content-Type": "application/json"} | ||
) | ||
|
||
try: | ||
id = response.json()["id"] | ||
url = f"https://encrypted-message.cdssandbox.xyz/{i18n.t('secret.locale_short')}/view/{id}" | ||
client.chat_postEphemeral( | ||
channel=view["private_metadata"], | ||
user=view["private_metadata"], | ||
text=f"{i18n.t('secret.link_available')} {url}", | ||
) | ||
except: | ||
client.chat_postEphemeral( | ||
channel=view["private_metadata"], | ||
user=view["private_metadata"], | ||
text=i18n.t("secret.error"), | ||
) | ||
return | ||
|
||
|
||
def handle_change_locale_button(ack, client, body): | ||
ack() | ||
if "user" in body: | ||
user_id = body["user"]["id"] | ||
else: | ||
user_id = body["user_id"] | ||
locale = body["actions"][0]["value"] | ||
if locale == "en-US": | ||
locale = "fr-FR" | ||
else: | ||
locale = "en-US" | ||
i18n.set("locale", locale) | ||
command = { | ||
"text": body["view"]["state"]["values"]["secret_input"]["secret_submission"][ | ||
"value" | ||
] | ||
} | ||
if command["text"] is None: | ||
command["text"] = "" | ||
view = generate_secret_command_modal_view(command, user_id, locale) | ||
client.views_update(view_id=body["view"]["id"], view=view) | ||
|
||
|
||
def generate_secret_command_modal_view(command, user_id, locale="en-US"): | ||
ttl_options = [ | ||
{"name": "7 " + i18n.t("secret.days"), "value": "604800"}, | ||
{"name": "3 " + i18n.t("secret.days"), "value": "259200"}, | ||
{"name": "1 " + i18n.t("secret.day"), "value": "86400"}, | ||
{"name": "12 " + i18n.t("secret.hours"), "value": "43200"}, | ||
{"name": "4 " + i18n.t("secret.hours"), "value": "14400"}, | ||
{"name": "1 " + i18n.t("secret.hour"), "value": "3600"}, | ||
{"name": "30 " + i18n.t("secret.minutes"), "value": "1800"}, | ||
{"name": "5 " + i18n.t("secret.minutes"), "value": "300"}, | ||
] | ||
|
||
options = [ | ||
{ | ||
"text": {"type": "plain_text", "text": i["name"]}, | ||
"value": i["value"], | ||
} | ||
for i in ttl_options | ||
] | ||
return { | ||
"type": "modal", | ||
"private_metadata": user_id, | ||
"callback_id": "secret_view", | ||
"title": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.title"), | ||
}, | ||
"submit": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.submit"), | ||
}, | ||
"blocks": [ | ||
{ | ||
"type": "actions", | ||
"block_id": "locale", | ||
"elements": [ | ||
{ | ||
"type": "button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.locale_button"), | ||
"emoji": True, | ||
}, | ||
"value": locale, | ||
"action_id": "secret_change_locale", | ||
} | ||
], | ||
}, | ||
{ | ||
"type": "input", | ||
"block_id": "secret_input", | ||
"label": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.label"), | ||
}, | ||
"element": { | ||
"type": "plain_text_input", | ||
"action_id": "secret_submission", | ||
"initial_value": command["text"], | ||
"placeholder": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.placeholder"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
"block_id": "product", | ||
"type": "input", | ||
"element": { | ||
"type": "static_select", | ||
"placeholder": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.ttl"), | ||
}, | ||
"options": options, | ||
"action_id": "secret_ttl", | ||
}, | ||
"label": { | ||
"type": "plain_text", | ||
"text": i18n.t("secret.ttl"), | ||
"emoji": 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
Oops, something went wrong.