inCaller / prometheus_bot

Telegram bot for prometheus alerting
MIT License
394 stars 182 forks source link

add support send messages to telegram topics #101

Closed silverwinter313 closed 5 months ago

silverwinter313 commented 6 months ago

Hi, i check your issues https://github.com/inCaller/prometheus_bot/issues/92 and i need too this feature. Maybe your review my solution? Thank you in advance!

hryamzik commented 5 months ago

Can you confirm it's backward compatible?

silverwinter313 commented 5 months ago

Yes, because when we call function getID

topicid := getID(c, "topicid")
chatid := getID(c, "chatid")

I write condition for topic_id, if topic_id doesn't set, return 0 value.

if c.Param(param) == "" && param == "topicid" {
return 0
}

Why exactly topic_id=0, in telegram bot function NewMessage always set ReplyToMessageID: 0 for send message

// chatID is where to send it, text is the message text.
func NewMessage(chatID int64, text string) MessageConfig {
    return MessageConfig{
        BaseChat: BaseChat{
            ChatID:           chatID,
            ReplyToMessageID: 0,
        },
        Text:                  text,
        DisableWebPagePreview: false,
    }
} 

For confirmations additionally example:

My curl request for check work backward compatibility: curl -X POST -H "Content-Type: application/json" -d "@production_example.json" 127.0.0.1:9087/alert/$CHAT_ID Log messages:

  Alert: <a href="http://localhost.localdomain:9090/graph?g0.expr=linux_loadavg%7Bmode%3D%2215min%22%7D&#43;%3E&#43;%282&#43;%2A&#43;10&#43;%2A&#43;4%29&amp;g0.tab=0">Test Fisic measure, from KN</a>
  Current value:38.24 EN
  Severity: Warning ⚠️
  Active from: 26/01/2017 19:30:00

2024/02/08 15:39:05 +-----------------------------------------------------------+
[GIN] 2024/02/08 - 15:39:05 | 200 |  235.353458ms |       127.0.0.1 | POST     "/alert/$CHAT_ID"

Message in telegram chat:

Screenshot 2024-02-08 at 15 43 54

Curl request for check topic: curl -X POST -H "Content-Type: application/json" -d "@production_example.json" 127.0.0.1:9087/alert/$CHAT_ID/$TOPIC_ID Log messages:

  Alert: <a href="http://localhost.localdomain:9090/graph?g0.expr=linux_loadavg%7Bmode%3D%2215min%22%7D&#43;%3E&#43;%282&#43;%2A&#43;10&#43;%2A&#43;4%29&amp;g0.tab=0">Test Fisic measure, from KN</a>
  Current value:38.24 EN
  Severity: Warning ⚠️
  Active from: 26/01/2017 19:30:00

2024/02/08 15:45:56 +-----------------------------------------------------------+
[GIN] 2024/02/08 - 15:45:57 | 200 |  419.795791ms |       127.0.0.1 | POST     "/alert/$CHAT_ID/$TOPIC_ID"

Message in telegram topic:

Screenshot 2024-02-08 at 15 46 25
silverwinter313 commented 5 months ago

I write in readme.md how to use this

hryamzik commented 5 months ago

Great, thanks, I'm going to merge it now. Can't verify myself but the code looks fine to me.