1
0
Fork 0
hswro-alarm-bot/sender_sync.go

70 lines
1.7 KiB
Go
Raw Normal View History

2025-01-06 20:38:43 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
)
type SendToTelegramSync struct {
2025-01-06 22:29:54 +00:00
SyncFilterImpl[GenericMessage]
2025-01-06 20:38:43 +00:00
sender Sender
logger *log.Logger
tpl *template.Template
}
2025-01-06 22:29:54 +00:00
func MakeSendToTelegramSync(sender Sender,
logger *log.Logger,
tpl *template.Template) SendToTelegramSync {
return SendToTelegramSync{SyncFilterImpl[GenericMessage]{}, sender, logger, tpl}
}
2025-01-06 20:38:43 +00:00
func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
err := sendToTg.sender.Send(msg, sendToTg.tpl)
if err != nil {
// TODO: handle it better
panic(err)
}
sendToTg.CallNext(msg)
}
type SendToMatterbridgeSync struct {
2025-01-06 22:29:54 +00:00
SyncFilterImpl[GenericMessage]
2025-01-06 20:38:43 +00:00
s SatelNameGetter
config AppConfig
logger *log.Logger
tpl *template.Template
}
func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) {
for _, matterbridgeConfig := range mbridge.config.Matterbridge {
body, err := json.Marshal(MatterbridgeMessage{
Text: msg.Format(mbridge.tpl, mbridge.s, mbridge.logger),
Username: matterbridgeConfig.Username,
Gateway: matterbridgeConfig.Gateway,
})
if err != nil {
mbridge.logger.Fatal("Could not marshal a JSON message: ", err)
}
req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body))
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)}
client := http.Client{
Timeout: httpTimeout,
}
res, err := client.Do(req)
if err != nil {
mbridge.logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
return
}
mbridge.logger.Print("Notified via Matterbridge with result ", res.StatusCode)
}
mbridge.CallNext(msg)
}