package
0.195.1
Repository: https://github.com/influxcommunity/flux.git
Documentation: pkg.go.dev

# README

Telegram Package

Use this Flux Package to send a message to a Telegram channel using https://core.telegram.org/bots/api#sendmessage API.

The telegram API requires you to know a bot token and a channel ID. The following steps were initially used to test this package:

  1. Create a new account in an application downloaded from https://telegram.org , a phone number is required.
  2. Create a new bot: https://core.telegram.org/bots#creating-a-new-bot , you will receive a bot token at the end of the registration process.
  3. Create a new channel from the telegram application,
  4. Add the new bot to the channel as an Administrator, the only required permission is to post messages. See https://stackoverflow.com/questions/33126743/how-do-i-add-my-bot-to-a-channel to know more.
  5. Send any message to @YOUR bot in the channel. Then curl https://api.telegram.org/bot$token/getUpdates and look for the id of the channel, it is the channel argument in the telegram package functions.

telegram.message

message function sends a single message to a Telegram channel using the API descibed in https://core.telegram.org/bots/api#sendmessage. Arguments:

NameTypeDescription
urlstringURL of the telegram bot endpoint. Defaults to: "https://api.telegram.org/bot"
tokenstringTelegram bot token string, required.
channelstringID of the telegram channel, required.
textstringThe message text.
parseModestringParse mode of the message text per https://core.telegram.org/bots/api#formatting-options . Defaults to "MarkdownV2"
disableWebPagePreviewboolDisables preview of web links in the sent messages when "true". Defaults to "false"
silentboolMessages are sent silently (https://telegram.org/blog/channels-2-0#silent-messages) when "true". Defaults to "true"

Basic Example:

import "contrib/sranka/telegram"
import "influxdata/influxdb/secrets"

//this value can be stored in the secret-store()
token = secrets.get(key: "TELEGRAM_TOKEN")

lastReported =
  from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "statuses")
    |> last()
    |> tableFind(fn: (key) => true)
    |> getRecord(idx: 0)

telegram.message(
  token:token,
  channel: "-12345"
  text: "Great Scott!- Disk usage is: *${lastReported.status}*.",
)

telegram.endpoint

endpoint function creates a factory function that accepts a mapping function mapFn and creates a target function for pipeline |> that sends messages from table rows. The mapFn accepts a table row and returns an object with channel, text, and silent as defined in the telegram.message function arguments. Arguments:

NameTypeDescription
urlstringURL of the telegram bot endpoint. Defaults to: "https://api.telegram.org/bot"
tokenstringTelegram bot token string, required.
parseModestringParse mode of the message text per https://core.telegram.org/bots/api#formatting-options . Defaults to "MarkdownV2"
disableWebPagePreviewboolDisables preview of web links in the sent messages when "true". Defaults to "false"
"true"

Basic Example:

import "contrib/sranka/telegram"
import "influxdata/influxdb/secrets"

// this value can be stored in the secret-store()
token = secrets.get(key: "TELEGRAM_TOKEN")

lastReported =
from(bucket: "example-bucket")
  |> range(start: -1m)
  |> filter(fn: (r) => r._measurement == "statuses")
  |> last()
  |> tableFind(fn: (key) => true)
  |> telegram.endpoint(token: token)(mapFn: (r) => ({
          channel: "-12345", 
          text: "Great Scott!- Disk usage is: **${r.status}**.", 
          silent: true
        })
     )()

Contact