Email notification with Sendgrid

Hi,

I’m running into trouble creating an emal alert using sendgrid using the example from documentation

import "http"

// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"

// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")

numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and r.level == "crit")
|> count()

numberOfCrits
  |> map(fn: (r) => (if r._value > 3 then {
    r with _value: http.post(
      url: "https://api.sendgrid.com/v3/mail/send",
      headers: {"Content-Type": "application/json", Authorization: "Bearer ${SENDGRID_APIKEY}"},
      data: bytes(v: "{
    \"personalizations\": [{
      \"to\": [{
       \"email\": \”jane.doe@example.com\"}],
       \"subject\": \”InfluxData critical alert\"
        }],
      \"from\": {\"email\": \"john.doe@example.com\"},
      \"content\": [{
        \"type\": \"text/plain\",
         \"value\": \”Example alert text\"
    }]
    }\""))} else {r with _value: 0}))

I stored my sendcloud API key using the CLI for my organization.
Even if I change the email in the script above, when I’m trying to save the script I get the following error

Failed to create new task: invalid AST: loc 0:0-0:0: unknown operator "<INVALID_OP>

Any ideas?

Thanks!
Martin

@hermiot Are you running this as a task or just a normal query?

It looks like you have a mix of standard double-quotes and smart double-quotes (in front of jane here), that could be causing a problem.

Same in a few other places.

And also it looks like you have an extra \" at the end of your JSON that shouldn’t be there.

1 Like

I just went through all the email examples in the docs, and most of them had syntax issues. I have a PR that fixes them. Try this:

import "http"

// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"

// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")

numberOfCrits = from(bucket: "_monitoring")
	|> range(start: -task.every)
	|> filter(fn: (r) => r.measurement == "statuses" and r.level == "crit")
	|> count()

numberOfCrits
  |> map(fn: (r) => (if r._value > 3 then {
    r with _value: http.post(
      url: "https://api.sendgrid.com/v3/mail/send",
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer ${SENDGRID_APIKEY}"
      },
      data: bytes(v: "{
        \"personalizations\": [
          {
            \"to\": [
              {
                \"email\": \"jane.doe@example.com\"
              }
            ]
          }
        ],
        \"from\": {
          \"email\": \"john.doe@example.com\"
        },
        \"subject\": \"InfluxDB critical alert\",
        \"content\": [
          {
            \"type\": \"text/plain\",
            \"value\": \"There have been ${string(v: r._value)} critical statuses.\"
          }
        ]
      }"))} else {r with _value: 0}))
1 Like

Thank you very much for helping me. I thought it has something to do with the synthax but I’m just starting with flux.
Eveything is working now! :slight_smile:

Did anybody use sendgrid so far?
I dont get an error, when running the task but I don’t get an email also.
I changed the r._value > 3 to r._value >=1 so every crit alert should be reportet.
The API Key I’m using has full access and works when I test it via sendgrid API documentation website.
Thaks!

@hermiot Are you certain that you have critical statuses?

Yes, Slack is working fine. Also HTTP Post.
But I can’t get the task, using the example, running.

I don’t know if http.post() has any response error code handling. I’m wondering if there’s an error in the payload. Can you try a manual API call (outside of Flux) using the same request body to see if it returns an error?