Skip to content

Instantly share code, notes, and snippets.

@ilap
Last active July 16, 2024 11:43
Show Gist options
  • Save ilap/cb6d512694c3e4f2427f85e4caec8ad7 to your computer and use it in GitHub Desktop.
Save ilap/cb6d512694c3e4f2427f85e4caec8ad7 to your computer and use it in GitHub Desktop.
Grafana Telegram Alert

Config Telegrambot for grafana's alerts.

1. Create bot

Open Telegram and search for @BotFather user and message them the following:

You
/newbot 

BotFather
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

You
TypeYourBotName

BotFather
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

You
TypeYourBotNameBot

BotFather
Done! Congratulations on your new bot. You will find it at t.me/shelley_monitor_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
99...:AA......TvF8
Keep your token secure and store it safely, it can be used by anyone to control your bot.

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

2. Create a Channel and retrieve the channel's chat ID.

  • Create a channel in telegram and name it whatever you like. e.g. MyCardanoMonitoring or similar
  • Invite @BotFather to that channel as admin
  • Type at least one message, this is very important
  • Get the Chat id:
https://api.telegram.org/bot<YOUR API TOKE FROM ABOVE>/getUpdates
{"ok":true,"result":[{"update_id":1112223334445,
"channel_post":{"message_id":1,"chat":{"id":-<YOUR_CHAT_ID>,"title":"ShelleyMonitoring","type":"channel"},"date":1576534122,"text":"/bot","entities":[{"offset":0,"length":4,"type":"bot_command"}]}}]}

Config Telegram in Grafana

  • Login to grafana
  • Click to the left Bell icon
  • Add notification channel
  • Select Telegram
  • Enable/disable settings you preger
  • Put Telegram API token to he fiel
  • Add chat ID (it starts with -, and that needs too)
  • Click Test notification
  • Save it.

Add alert

  • Go to dashboard
  • Select "Jormungandr Monitor"
  • Select "Jormungandr Last Block Height", ckicj edit
  • Click Alarm icon
  • Click "Create Alert"
  • Edit the fields, see details below: Screen Shot 2019-12-20 at 8 14 31 pm
@drastys
Copy link

drastys commented Jan 20, 2020

Hello,
I dont have option Alert for Jormungandr items. Any idea pls?

EDIT: solved, not all type of visualization supported alerts.

@chrome112
Copy link

Привет, у вас есть ошибка.
Add chat ID (it starts with -, and that needs too) < "-" не нужно. >

@lightiv
Copy link

lightiv commented Jun 8, 2020

Thank you for this!!!!

@FireEmerald
Copy link

There is a mistake in your description.

Invite @BotFather to that channel as admin

You must invite your own bot, not bot father: @TypeYourBotNameBot

@ilap
Copy link
Author

ilap commented Jun 15, 2020 via email

@alexinthis
Copy link

Thanks for this post and thanks to the comments so far. I found I needed to do these three things differently from the post, some already mentioned by @chrome112 and @FireEmerald.

  1. It says you need to send at least one message. I found that the message had to have the word "bot" in it otherwise your response in the api url looks like this: {"ok":true,"result":[]}
  2. The chat ID needs to include the - when pasting into Grafana, ie -1003206419552
  3. Don't add @Botfather to your to your newly created channel, add the user name of your newly created bot.

@wbollock
Copy link

Can confirm all the changes @alexinthis mentioned are needed

@Sireesha1133
Copy link

Hi Team,

I am trying to get API key of Telegram Chat ID but it is giving null response. Can anyone help me on this
Please find the below response
{"ok":true,"result":[]}

@drastys
Copy link

drastys commented Mar 8, 2021

Hello,
you need to send some message to chat and then you should see details.
Tomas [DOLCA pool]

@ilap
Copy link
Author

ilap commented Mar 8, 2021

Hello,
you need to send some message to chat and then you should see details.
Tomas [DOLCA pool]

Hi Team,

I am trying to get API key of Telegram Chat ID but it is giving null response. Can anyone help me on this
Please find the below response
{"ok":true,"result":[]}

I am individual.:) Anyway as Thomas already said and it is already in the instructions:

  • Type at least one message, this is very important

@Rezasalami
Copy link

Hello,
i try to use telegram alert
but when clike on test i don't resive any message in my telegram
test notification is succes "Test notification sent"
i chek it by curl and i can send message and resive it in telegram
api key and chat_id is ok
how i can cheking grafana resive responce from telegram api

@adamdewey
Copy link

@Rezasalami - try clearing the 'For' field under the Rule.

That worked for me. Otherwise my alert always seemed to be stuck in 'Pending' state and never 'Alerting'

@BoogerMan2103
Copy link

BoogerMan2103 commented Nov 26, 2021

from https://stackoverflow.com/questions/50204633/allow-bot-to-access-telegram-group-messages

You have to use the @Botfather bot to set the Group Privacy off. This let the bot to access all the group messages.

By default, Group Privacy is enabled for bots. This setting has to be changed used the @Botfather bot >> Bot Settings >> Group Privacy >> Turn off

Please note that this change is not propagated automatically, so in case the bot is already in a group before turning off privacy mode, it will remain as it was before. To apply the changes, the bot should be removed and added again to the group(s).

@medzhidov
Copy link

You should invite NOT botfather but your created bot, which token you paste in url. Good luck 🍀

@bearded-coder-official
Copy link

How to add additional parameter message_thread_id to specify specific chat channel?

@yegle
Copy link

yegle commented Jul 24, 2023

How to add additional parameter message_thread_id to specify specific chat channel?

FWIW there's no way to specify a message_thread_id until someone contribute some code. The relevant code is https://github.com/grafana/grafana/blob/425c92a92b8059d1577d3e175d88a1862e6cc3d6/pkg/services/alerting/notifiers/telegram.go#L57-L63 which does not support an additional message_thread_id field.

@alinazemlev
Copy link

There is a mistake in your description.

Invite @BotFather to that channel as admin

You must invite your own bot, not bot father: @TypeYourBotNameBot

Thank you for your advice!

@pgin1234
Copy link

请问,最新grafana版本,如何才能使telegram告警发送到tg群聊?但是测试发送消息,可以正常发送到tg群聊,告警消息就没有发送到群聊?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment