aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2020-12-21 04:41:27 +0300
committerGravatar Hassan Abouelela <[email protected]>2020-12-21 04:41:27 +0300
commit9d8ca77b1f18891cef88e9b8235b41172067f9b0 (patch)
tree4f9ba4026002a9e69b916bb73c9aa9fce02cf758
parentAdds Missing Annotation (diff)
Adds and Documents Webhook Message Variables
Adds better parsing and formatting for webhook message variables, and documents them in SCHEMA.md. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--SCHEMA.md18
-rw-r--r--backend/routes/forms/submit.py12
2 files changed, 27 insertions, 3 deletions
diff --git a/SCHEMA.md b/SCHEMA.md
index b55635f..fa5f247 100644
--- a/SCHEMA.md
+++ b/SCHEMA.md
@@ -33,9 +33,21 @@ In this document:
| `WEBHOOK_ENABLED` | The form should notify the webhook. Has no effect if no webhook is set. |
### Meta options
-| Field | Description | Example |
-| --------- | ---------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
-| `webhook` | Mapping of webhook url and message. Message can use `_USER_MENTION_` to mention the submitting user. | `"webhook": {"url": "https://discord.com/api/webhooks/id/key", "message": "Hello World! _USER_MENTION_"}` |
+| Field | Description | Example |
+| --------- | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
+| `webhook` | Mapping of webhook url and message. Message can use certain [context variables](#webhook-variables). | `"webhook": {"url": "https://discord.com/api/webhooks/id/key", "message": "{user} submitted a form."}` |
+
+
+#### Webhook Variables
+The following variables can be used in a webhook's message. The variables must be wrapped by braces (`{}`).
+
+| Name | Description |
+| ------------- | ---------------------------------------------------------------------------- |
+| `user` | A discord mention of the user submitting the form, or "User" if unavailable. |
+| `response_id` | ID of the submitted response. |
+| `form` | Name of the submitted form. |
+| `form_id` | ID of the submitted form. |
+| `time` | ISO submission timestamp. |
### Form question
diff --git a/backend/routes/forms/submit.py b/backend/routes/forms/submit.py
index 3b22155..5c0cfdd 100644
--- a/backend/routes/forms/submit.py
+++ b/backend/routes/forms/submit.py
@@ -197,6 +197,18 @@ class SubmitForm(Route):
# Set hook message
message = form.meta.webhook.message
if message:
+ # Available variables, see SCHEMA.md
+ ctx = {
+ "user": mention,
+ "response_id": response.id,
+ "form": form.name,
+ "form_id": form.id,
+ "time": response.timestamp,
+ }
+
+ for key in ctx:
+ message = message.replace(f"{{{key}}}", str(ctx[key]))
+
hook["content"] = message.replace("_USER_MENTION_", mention)
# Post hook