Simplify 'send email' code examples (#3257)

* Simplify code examples

* use json.encode instead of bytes
* drop explicit string conversion in interpolation

* remove obsolete note
pull/3283/head
Sam Lai 2021-10-18 23:46:29 +01:00 committed by GitHub
parent 653efaf245
commit fa6d734be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 48 deletions

View File

@ -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\": \"<h3>${string(v: r._value)} critical statuses</h3><p>There have been ${string(v: r._value)} critical statuses.\"
"Subject": "InfluxDB critical alert",
"TextPart": "There have been ${r._value} critical statuses.",
"HTMLPart": "<h3>${r._value} critical statuses</h3><p>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 <mailgun@YOUR_DOMAIN_NAME>\",
\"to\": \"email@example.com\",
\"subject\": \"InfluxDB critical alert\",
\"text\": \"There have been ${string(v: r._value)} critical statuses.\"
}"))} else {r with _value: 0}))
data: json.encode(v: {
"from": "Username <mailgun@YOUR_DOMAIN_NAME>",
"to": "email@example.com",
"subject": "InfluxDB critical alert",
"text": "There have been ${r._value} critical statuses."
}))} else {r with _value: 0}))
```
{{% /tab-content %}}