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

46 lines
1.4 KiB
Go

package main
import (
"os"
"fmt"
"net"
"flag"
"github.com/probakowski/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
satel_api_addr := flag.String("satel_addr", "", "Address that should be used to connect to the SATEL device")
satel_api_port := flag.String("satel_port", "7094", "Port that should be used to connect to the SATEL device")
chat_id := flag.Int64("tg_chat_id", -1, "Telegram Chat ID where to send updates")
flag.Parse()
if len(*satel_api_addr) == 0 || len(*satel_api_port) == 0 || *chat_id == -1 {
fmt.Println("Use --satel_addr=ADDR, --satel_port=PORT and --tg_chat_id=CHAT_ID command line flags to continue.")
os.Exit(1)
}
satel_addr := fmt.Sprintf("%s:%s", *satel_api_addr, *satel_api_port)
satel_conn, err := net.Dial("tcp", satel_addr)
if err != nil {
panic(err)
}
s := satel.NewConfig(satel_conn, satel.Config{EventsQueueSize: 1000})
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil {
panic(err)
}
for e, ok := <-s.Events; ok; e, ok = <-s.Events {
fmt.Println("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value)
msg := tgbotapi.NewMessage(*chat_id, fmt.Sprintf("Change from SATEL: Zone: %d Type: %s Value: %t",
e.Index, e.Type, e.Value))
if _, err := bot.Send(msg); err != nil {
// TODO: retry sending later in case of problems
panic(err)
}
}
}