generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: add the /secret command for encrypted-message #382
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,163 @@ | ||
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, logger): | ||
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, timeout=10, 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 Exception as e: | ||
logger.error(e) | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super, super minor thing which we don't need to change, but the URL https://encrypted-message.cdssandbox.xyz might be better to live in a variable or secret since you are anyways using it in multiple places throughout your code. Again super minor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - I hardcoded it for now but should be made into an env in the future