jertel / elastalert2

ElastAlert 2 is a continuation of the original yelp/elastalert project. Pull requests are appreciated!
https://elastalert2.readthedocs.org
Apache License 2.0
895 stars 282 forks source link

Added telegram message_thread_id option #1319

Closed polshe-v closed 9 months ago

polshe-v commented 9 months ago

Description

I wanted to sort alerts sent to Telegram, because I have different log sources and there are so many types of them, therefore so many chats. But having multiple Telegram chats for every source type is not convenient (chaotic chats order, difficult managing telegram_room_id in rules).

Now Telegram has a feature of supergroups/forums where you can create threads. Using this supergroup/forum I can have all my alerts in 1 chat and sort them in threads. So I decided to add an option for message_thread_id of Telegram sendMessage function.

Checklist

Questions or Comments

jertel commented 9 months ago

Thanks for this enhancement. The PR looks good. Can you confirm that alerts show up properly in Telegram when telegram_thread_id is not provided (default behavior)?

polshe-v commented 9 months ago

Yes, it works as usual

I performed following tests:

  1. Send alert to common group chat, no telegram_thread_id option in a rule. Result: works as usual, alert is sent to the group chat.
  2. Send alert to supergroup chat, no telegram_thread_id option in a rule. Result: alert is sent to general thread of supergroup.
  3. Send alert to common group chat, telegram_thread_id: 2 in a rule. Result: not working, because common group chat obviously has no threads. Writes an error like this: ERROR:elastalert:Error while running alert telegram: Error posting to Telegram: 400 Client Error: Bad Request for url: https://api.telegram.org/bot<BOT_TOKEN>/sendMessage. Details: {"ok":false,"error_code":400,"description":"Bad Request: message thread not found"}
  4. Send alert to supergroup chat, telegram_thread_id: 2 in a rule. Result: alert is sent to thread with ID 2.
jertel commented 9 months ago

Thank you for confirming.