Simple Alert as a Task Example with telegram

Hi all,:wave: :wave:.

I am trying to do a custom check with a task in Influx DB V2.x. As you can see from the picture below, I’m taking a field from _measurement, and i would like it if the value goes below 40, then it will be “critical”, and i hope to infuse the alert task with telegram so that every time the value goes below 40, it will send a “critical” text to my telegram. I have tried to use the information from the internet, but I simply can’t do it. Does anyone have a rough idea of how to do it?

Much help is appreciated.

Here is an example i made but couldn’t run as a task

Hello @KaiJ17,
Thanks for your question.
What error are you getting?

I often debug flux tasks by making sure I can run it in the data explorer first.
Can you successfully:

  1. return data? add a yield() to check
  2. map? add a second yield() to check
  3. extract the value? use array.from() and yield() to return the value and verify
  4. send a message to telegram?

Hello, I would like do the same thing. Run a task with telegram, but i have some errors while compiling:

I have introduced the code of the alert followed of a few lines i have found on internet, but Im not sure if it is ok.

could someone help me? Thank you very much.

Hello :wave: :wave: @Anaisdg

I currently tried these few steps. First, for returning data, it is not very constant as sometimes it works and sometimes it doesn’t, as shown below, but for the 2nd and 3rd steps, I am not clear on what to put and where to put it. I hope you can guide me along, and lastly, for the 4th step, it can be sent to my telegram as you see from the picture below. Even though I save it as a task and it actually sends the message to the telegram, i still can’t save it as a task, and it gives me this error when i run the task.



As you can see, it sometimes doesn’t show the data, but sometimes it shows the data, so it is not very constant and this is the error i keep getting trying to run the task.

Thank you, much help appreciated!

Are you able to send data that hasn’t been interpolated?

import "contrib/sranka/telegram"

telegram.message(
    url: "https://api.telegram.org/bot",
    token: "S3crEtTel3gRamT0k3n",
    channel: "-12345",
    text: "hello",
    parseMode: "MarkdownV2",
    disableWebPagePreview: false,
    silent: true,
)

Have you confirmed that you are able to return a scalar value?
Try:

import "array"

send = MyMessage._value

array.from(
    rows: [
        {test: send}
    ],
)

Can you try storing the scalar in a new variable?
Like:

send = MyMessage._value

Hmm you might try to return a dummy table:

import "array"
array.from(rows: [{v: 0}]) |> yield(name: "trivial")

I’d also consider going directly to the tasks page, creating a dummy task with foo as the task, saving it, and then editing it by copy and pasting your code.

Can you copy and paste your code here too please?

Hi @Anaisdg ,

This is the code for the task:

import “contrib/sranka/telegram”

Mymem = from(bucket: “Testing”)

|> range(start: v.timeRangeStart, stop: v.timeRangeStop)

|> filter(fn: (r) => r["_measurement"] == "example")

|> filter(fn: (r) => r["_field"] == "value1")

|> last()

Alert = Mymem

|> map(

    fn: (r) => ({r with

        level: if r._value < 50.0 then

            "critical"

        else

            "normal",

    }),

)

Alertextract = Alert

|> findRecord(fn: (key) => true, idx: 0)

if Alertextract.level == “critical” then

telegram.message(

    url: "https://api.telegram.org/bot",

    token: "5130985963:AAGngJmQg0AzxK-Jw1rdo_wA9lQk-yYBfXE",

    channel: "-1001638205815",

    text: "CRIT",

    parseMode: "MarkdownV2",

    disableWebPagePreview: false,

    silent: true,

)

else

0

Thanks!

Hello @germangaggero and @KaiJ17
The issue above addresses the problem here. You have to invoke a stream.
The following worked for me:

import "contrib/sranka/telegram"
import "array"

data = from(bucket: "noaa")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "average_temperature")
  |> filter(fn: (r) => r["_field"] == "degrees")
  |> filter(fn: (r) => r["location"] == "coyote_creek")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")
  |> last()

Alert = data

|> map(

    fn: (r) => ({r with

        level: if r._value > 0.0 then

            "critical"

        else

            "normal",

    }),

)
|> yield(name: "alert")
Alertextract = Alert

|> findRecord(fn: (key) => true, idx: 0)
if Alertextract.level == "critical" then

telegram.message(

    url: "https://api.telegram.org/bot",

    token: "<mytoken>",

    channel: "-100<mychannelID>",

    text: "CRIT",

    parseMode: "MarkdownV2",

    disableWebPagePreview: false,

    silent: true,

)
else

0 

array.from(rows: [{foo: 0}]) 

Hi @Anaisdg :wave: :wave: ,

Thanks for the reply! i have tried using the code you have sent, so this is my steps taken:
I have already implemented your code and tried it, so these are the error i got.

First, when i enter the code into a script editor, i get this error back and not quite sure why this is happening even though yours works.

Internal error has errored - check server logs

Secondly , i run the code as a task and sometimes it works and sometimes it doesn’t. It is not very consistent in sending message to my telegram , even though my value is more than 0.

I am also trying to use this script as a task , but as you can see from the Task logs , you can see that there is also an error I’m not quite sure myself too as from the pictures below.

Would appreciate it if you can guide me along, Thank you! :grin: :grin:

import “contrib/sranka/telegram”
import “array”

option v = {field: “humidity”, timeRangeStart: -1h, timeRangeStop: now(), windowPeriod: 10000ms}

option task = {name: “name-assigned”, every: 5s}

data =
from(bucket: “My bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == “Mymeasurement”)
|> filter(fn: (r) => r["_field"] == “Myfield”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: “mean”)
|> last()

Alert =
data
|> map(
fn: (r) =>
({r with level:
if r._value > 0 then
“critical”
else
“normal”,
}),
)
|> yield(name: “alert”)

Alertextract =
Alert
|> findRecord(fn: (key) => true, idx: 0)

if Alertextract.level == “critical” then
telegram.message(
url: “https://api.telegram.org/bot”,
token: “Mytoken”,
channel: “MyChannel”,
text: “CRIT”,
parseMode: “MarkdownV2”,
disableWebPagePreview: false,
silent: true,
)
else
0

array.from(rows: [{foo: 0}])
|> to(bucket: “Mybucket”, org: “MyOrganization”)

Hello @KaiJ17,
What version of InfluxDB are you using?

you don’t need the to() function

Hi @Anaisdg,

I am using influxdb V2.0 for this task here!