2024-03-29 09:55:51 +01:00
package integrations
import (
"encoding/json"
"fmt"
2024-03-31 20:04:48 +02:00
"log"
2024-03-31 09:50:09 +02:00
"strconv"
2024-03-29 09:55:51 +01:00
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type TelegramIntegration struct { }
type TelegramNotificationChannelConfig struct {
Id int64 ` json:"id" `
ApiKey string ` json:"apiKey" `
TargetChannel int64 ` json:"targetChannel" `
}
func ( s TelegramIntegration ) ProcessEvent ( evt * ContactPointEvent , rawConfig string ) error {
config := TelegramNotificationChannelConfig { }
err := json . Unmarshal ( [ ] byte ( rawConfig ) , & config )
if err != nil {
return fmt . Errorf ( "failed to parse telegram integration config - %w" , err )
}
bot , err := tgbotapi . NewBotAPI ( config . ApiKey )
if err != nil {
return err
}
switch evt . Type {
case ContactPointEventAlertTriggered :
data := evt . AlertTriggeredEvent
2024-03-31 20:04:48 +02:00
if data . SensorValueCondition != nil {
text := fmt . Sprintf ( "🚨 %s is at {value}" , data . Sensor . Name )
2024-03-29 20:49:08 +01:00
2024-03-31 20:04:48 +02:00
if data . Alert . CustomMessage != "" {
text = data . Alert . CustomMessage
}
text = strings . Replace ( text , "{value}" , strconv . FormatFloat ( data . LastValue , 'f' , - 1 , 64 ) , - 1 )
msg := tgbotapi . NewMessage ( config . TargetChannel , text )
msg . ParseMode = "Markdown"
_ , err = bot . Send ( msg )
return err
2024-03-29 09:55:51 +01:00
}
2024-03-31 20:04:48 +02:00
if data . SensorLastContactCondition != nil {
text := fmt . Sprintf ( "🚨 %s has not reported in for last %s %s" , data . Sensor . Name , strconv . FormatFloat ( data . SensorLastContactCondition . Value , 'f' , - 1 , 64 ) , data . SensorLastContactCondition . ValueUnit )
if data . Alert . CustomMessage != "" {
text = data . Alert . CustomMessage
}
msg := tgbotapi . NewMessage ( config . TargetChannel , text )
msg . ParseMode = "Markdown"
_ , err = bot . Send ( msg )
return err
}
log . Println ( "No condition found for alert triggered event" )
return nil
case ContactPointEventAlertResolved :
data := evt . AlertResolvedEvent
if data . SensorValueCondition != nil {
text := fmt . Sprintf ( "✅ %s is at {value}" , data . Sensor . Name )
2024-03-31 20:47:43 +02:00
if data . Alert . CustomResolvedMessage != "" {
text = data . Alert . CustomResolvedMessage
2024-03-31 20:04:48 +02:00
}
text = strings . Replace ( text , "{value}" , strconv . FormatFloat ( data . LastValue , 'f' , - 1 , 64 ) , - 1 )
msg := tgbotapi . NewMessage ( config . TargetChannel , text )
msg . ParseMode = "Markdown"
_ , err = bot . Send ( msg )
return err
}
if data . SensorLastContactCondition != nil {
text := fmt . Sprintf ( "✅ %s has reported in last %s %s" , data . Sensor . Name , strconv . FormatFloat ( data . SensorLastContactCondition . Value , 'f' , - 1 , 64 ) , data . SensorLastContactCondition . ValueUnit )
2024-04-28 07:23:18 +02:00
if data . Alert . CustomResolvedMessage != "" {
text = data . Alert . CustomResolvedMessage
2024-03-31 20:04:48 +02:00
}
msg := tgbotapi . NewMessage ( config . TargetChannel , text )
msg . ParseMode = "Markdown"
_ , err = bot . Send ( msg )
return err
}
log . Println ( "No condition found for alert triggered event" )
return nil
2024-03-29 09:55:51 +01:00
2024-03-31 09:50:09 +02:00
case ContactPointEventTest :
msg := tgbotapi . NewMessage ( config . TargetChannel , "Test message from Basic Sensor Receiver" )
_ , err = bot . Send ( msg )
return err
2024-03-29 09:55:51 +01:00
}
return nil
}
func ( s TelegramIntegration ) ValidateConfig ( rawConfig string ) error {
config := TelegramNotificationChannelConfig { }
return json . Unmarshal ( [ ] byte ( rawConfig ) , & config )
}