diff --git a/README.md b/README.md index 9d1be8e..147057e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,13 @@ I didn't even test it yet so no idea if it works $ TELEGRAM_APITOKEN=YOUR_API_TOKEN ./alarm_bot --satel_addr=127.0.0.1 --satel_port=31337 --tg_chat_id=YOUR_CHAT_ID_FROM_BOTFATHER ``` +### Notification via HTTP callbacks + +Set the following environment variables: + +- `NOTIFY_URL_ARM` - for an URL that will be POST when partition *0* is armed +- `NOTIFY_URL_DISARM` - for an URL that will be POST when partition *0* is unarmed + ### Filtering events by change type It's possible to filter events by change type. Use the `--allowed-types=TYPE1,TYPE2,...` command line parameter to do that. If that parameter is not provided, then all change types are allowed, otherwise only the provided ones will be used for notifications. diff --git a/main.go b/main.go index f9fe7e6..03687c3 100644 --- a/main.go +++ b/main.go @@ -150,7 +150,11 @@ func main() { dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) - Consume(SendToTg(tgEvents, tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl)) + Consume(NotifyViaHTTP( + SendToTg(tgEvents, tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), + &wg, + logger, + )) go CloseSatelOnCtrlC(s) diff --git a/message_contents.go b/message_contents.go index 243dd0e..79f7ca8 100644 --- a/message_contents.go +++ b/message_contents.go @@ -9,6 +9,11 @@ import ( "git.sr.ht/~michalr/go-satel" ) +const ( + ArmedPartition_Armed = true + ArmedPartition_Unrmed = false +) + type SatelNameGetter interface { GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) } @@ -81,7 +86,7 @@ func getEmojiWhenTrueIsBad(v bool) string { } func getArmedPartitionStatus(v bool) string { - if v { + if v == ArmedPartition_Armed { return "✅ - ARMED" } else { return "🔴 - DISARMED" diff --git a/sender_worker.go b/sender_worker.go index 801e795..ceeb643 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -3,7 +3,15 @@ package main import ( "html/template" "log" + "net/http" + "os" "sync" + + "git.sr.ht/~michalr/go-satel" +) + +const ( + NotificationPartitionIndex = 0 ) type Sender interface { @@ -52,3 +60,46 @@ func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger return returnEvents } + +func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) { + wg.Add(1) + defer wg.Done() + if len(url) == 0 { + return + } + req, err := http.NewRequest(http.MethodPost, url, nil) + + res, err := http.DefaultClient.Do(req) + if err != nil { + logger.Print("Could not POST ", url, ": ", err) + return + } + logger.Print("Notified via HTTP with result ", res.StatusCode) +} + +func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage { + returnEvents := make(chan GenericMessage) + + armCallbackUrl := os.Getenv("NOTIFY_URL_ARM") + disarmCallbackUrl := os.Getenv("NOTIFY_URL_DISARM") + go func() { + wg.Add(1) + defer wg.Done() + for e := range events { + inner: + for _, basicElement := range e.Messages { + if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { + if basicElement.Value == ArmedPartition_Armed { + go doHttpNotification(armCallbackUrl, logger, wg) + } else { + go doHttpNotification(disarmCallbackUrl, logger, wg) + } + break inner + } + } + } + close(returnEvents) + }() + + return returnEvents +}