From fa6d734be90c65233b94d1c1b9999020ff574326 Mon Sep 17 00:00:00 2001 From: Sam Lai <70988+slai@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:46:29 +0100 Subject: [PATCH] Simplify 'send email' code examples (#3257) * Simplify code examples * use json.encode instead of bytes * drop explicit string conversion in interpolation * remove obsolete note --- .../influxdb/v2.0/monitor-alert/send-email.md | 94 +++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/content/influxdb/v2.0/monitor-alert/send-email.md b/content/influxdb/v2.0/monitor-alert/send-email.md index da2c03dce..fba2dbf8f 100644 --- a/content/influxdb/v2.0/monitor-alert/send-email.md +++ b/content/influxdb/v2.0/monitor-alert/send-email.md @@ -43,12 +43,6 @@ Send an alert email using a third-party service, such as [SendGrid](https://send - Use the `map()` function to evaluate the criteria to send an alert using `http.post()`. - Specify your email service `url` (endpoint), include applicable request `headers`, and verify your request `data` format follows the format specified for your email service. -{{% note %}} -#### Escape double quotes in your request body -To successfully byte-encode request bodies (`data`) in the `http.post()` function, -escape all double-quote characters with a backslash (`\"`). -{{% /note %}} - #### Examples {{< tabs-wrapper >}} @@ -66,6 +60,7 @@ The example below uses the SendGrid API to send an alert email when more than 3 ```js import "http" +import "json" // Import the Secrets package if you store your API key as a secret. // For detail on how to do this, see Step 4 above. @@ -88,27 +83,27 @@ numberOfCrits "Content-Type": "application/json", "Authorization": "Bearer ${SENDGRID_APIKEY}" }, - data: bytes(v: "{ - \"personalizations\": [ + data: json.encode(v: { + "personalizations": [ { - \"to\": [ + "to": [ { - \"email\": \"jane.doe@example.com\" + "email": "jane.doe@example.com" } ] } ], - \"from\": { - \"email\": \"john.doe@example.com\" + "from": { + "email": "john.doe@example.com" }, - \"subject\": \"InfluxDB critical alert\", - \"content\": [ + "subject": "InfluxDB critical alert", + "content": [ { - \"type\": \"text/plain\", - \"value\": \"There have been ${string(v: r._value)} critical statuses.\" + "type": "text/plain", + "value": "There have been ${r._value} critical statuses." } ] - }"))} else {r with _value: 0})) + }))} else {r with _value: 0})) ``` {{% /tab-content %}} @@ -124,6 +119,7 @@ Your AWS SES request, including the `url` (endpoint), authentication, and the st ```js import "http" +import "json" // Import the Secrets package if you store your API credentials as secrets. // For detail on how to do this, see Step 4 above. @@ -149,27 +145,27 @@ numberOfCrits "Content-Type": "application/json", "Authorization": "Bearer ${AWS_AUTH_ALGORITHM}${AWS_CREDENTIAL}${AWS_SIGNED_HEADERS}${AWS_CALCULATED_SIGNATURE}" }, - data: bytes(v: "{ - \"Content\": { - \"Simple\": { - \"Body\": { - \"Text\": { - \"Charset\": \"UTF-8\", - \"Data\": \"There have been ${string(v: r._value)} critical statuses.\" + data: json.encode(v: { + "Content": { + "Simple": { + "Body": { + "Text": { + "Charset": "UTF-8", + "Data": "There have been ${r._value} critical statuses." } }, - \"Subject\": { - \"Charset\": \"UTF-8\", - \"Data\": \"InfluxDB critical alert\" + "Subject": { + "Charset": "UTF-8", + "Data": "InfluxDB critical alert" } } }, - \"Destination\": { - \"ToAddresses\": [ - \"john.doe@example.com\" + "Destination": { + "ToAddresses": [ + "john.doe@example.com" ] } - }"))} else {r with _value: 0})) + }))} else {r with _value: 0})) ``` For details on the request syntax, see [SendEmail API v2 reference](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_SendEmail.html). @@ -187,6 +183,7 @@ To view your Mailjet API credentials, sign in to Mailjet and open the [API Key M ```js import "http" +import "json" // Import the Secrets package if you store your API keys as secrets. // For detail on how to do this, see Step 4 above. @@ -204,25 +201,25 @@ numberOfCrits = from(bucket: "_monitoring") numberOfCrits |> map(fn: (r) => (if r._value > 3 then { - r with _value: http.post( + r with _value: http.post( url: "https://api.mailjet.com/v3.1/send", headers: { "Content-type": "application/json", "Authorization": "Basic ${MAILJET_APIKEY}:${MAILJET_SECRET_APIKEY}" }, - data: bytes(v: "{ - \"Messages\": [{ - \"From\": { - \"Email\": \"jane.doe@example.com\" + data: json.encode(v: { + "Messages": [{ + "From": { + "Email": "jane.doe@example.com" }, - \"To\": [{ - \"Email\": \"john.doe@example.com\" + "To": [{ + "Email": "john.doe@example.com" }], - \"Subject\": \"InfluxDB critical alert\", - \"TextPart\": \"There have been ${string(v: r._value)} critical statuses.\", - \"HTMLPart\": \"
There have been ${string(v: r._value)} critical statuses.\" + "Subject": "InfluxDB critical alert", + "TextPart": "There have been ${r._value} critical statuses.", + "HTMLPart": "
There have been ${r._value} critical statuses."
}]
- }"))} else {r with _value: 0}))
+ }))} else {r with _value: 0}))
```
{{% /tab-content %}}
@@ -239,6 +236,7 @@ To view your Mailgun API keys, sign in to Mailjet and open [Account Security - A
```js
import "http"
+import "json"
// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
@@ -262,12 +260,12 @@ numberOfCrits
"Content-type": "application/json",
"Authorization": "Basic api:${MAILGUN_APIKEY}"
},
- data: bytes(v: "{
- \"from\": \"Username