117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package integrations
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"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
|
|
|
|
if data.SensorValueCondition != nil {
|
|
text := fmt.Sprintf("🚨 %s is at {value}", data.Sensor.Name)
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
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 ContactPointEventTest:
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, "Test message from Basic Sensor Receiver")
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s TelegramIntegration) ValidateConfig(rawConfig string) error {
|
|
config := TelegramNotificationChannelConfig{}
|
|
return json.Unmarshal([]byte(rawConfig), &config)
|
|
}
|