159 lines
3.6 KiB
Go
159 lines
3.6 KiB
Go
package integrations
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"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
|
|
|
|
text := fmt.Sprintf("🚨 %s is at {value}", data.SensorName)
|
|
if data.CustomMessage != "" {
|
|
text = data.CustomMessage
|
|
}
|
|
|
|
text = strings.Replace(text, "{value}", data.AlertValue, -1)
|
|
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, text)
|
|
msg.ParseMode = "Markdown"
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
}
|
|
|
|
/*
|
|
cameraUrl := ""
|
|
|
|
if event != nil {
|
|
cameraUrl = fmt.Sprintf("%s/#/cameras/%d", evt.AppConfig.PublicUrl, event.CameraId)
|
|
}
|
|
|
|
switch evt.Type {
|
|
case ChannelEventTest:
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, "👋 Test message")
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
|
|
case ChannelEventMotionStart:
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, fmt.Sprintf("👁 **Motion detected** by %s. [Go to stream](%s)", camera.Name, cameraUrl))
|
|
msg.ParseMode = "Markdown"
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
|
|
case ChannelEventCameraLost:
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, fmt.Sprintf("🔴 Connection to camera %s **lost**. [Go to camera](%s)", camera.Name, cameraUrl))
|
|
msg.ParseMode = "Markdown"
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
|
|
case ChannelEventCameraFound:
|
|
msg := tgbotapi.NewMessage(config.TargetChannel, fmt.Sprintf("🟢 Connection to camera %s **re-established**. [Go to camera](%s)", camera.Name, cameraUrl))
|
|
msg.ParseMode = "Markdown"
|
|
_, err = bot.Send(msg)
|
|
return err
|
|
|
|
case ChannelEventPreview:
|
|
if !event.Preview.Valid {
|
|
return nil
|
|
}
|
|
|
|
reader, err := os.Open(event.Preview.String)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open preview file - %w", err)
|
|
}
|
|
|
|
file := tgbotapi.FileReader{
|
|
Name: event.Preview.String,
|
|
Reader: reader,
|
|
}
|
|
|
|
previewMsg := tgbotapi.NewVideo(config.TargetChannel, file)
|
|
// previewMsg.Caption = fmt.Sprintf("Captured by %s", camera.Name)
|
|
_, err = bot.Send(previewMsg)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
case ChannelEventMovie:
|
|
if !event.Movie.Valid {
|
|
return nil
|
|
}
|
|
|
|
reader, err := os.Open(event.Movie.String)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
videoFile := tgbotapi.FileReader{
|
|
Name: event.Movie.String,
|
|
Reader: reader,
|
|
}
|
|
|
|
msg := tgbotapi.NewVideo(config.TargetChannel, videoFile)
|
|
// msg.Caption = fmt.Sprintf("Captured by %s", camera.Name)
|
|
_, err = bot.Send(msg)
|
|
|
|
return err
|
|
|
|
case ChannelEventPicture:
|
|
if !event.Picture.Valid {
|
|
return nil
|
|
}
|
|
|
|
reader, err := os.Open(event.Picture.String)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
videoFile := tgbotapi.FileReader{
|
|
Name: event.Movie.String,
|
|
Reader: reader,
|
|
}
|
|
|
|
msg := tgbotapi.NewPhoto(config.TargetChannel, videoFile)
|
|
msg.Caption = fmt.Sprintf(`Captured by %s`, camera.Name)
|
|
|
|
_, err = bot.Send(msg)
|
|
|
|
return err
|
|
}
|
|
*/
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s TelegramIntegration) ValidateConfig(rawConfig string) error {
|
|
config := TelegramNotificationChannelConfig{}
|
|
return json.Unmarshal([]byte(rawConfig), &config)
|
|
}
|