hswro-alarm-bot/main.go

92 wiersze
2.4 KiB
Go
Czysty Zwykły widok Historia

2024-02-08 18:23:46 +00:00
package main
import (
2024-02-09 22:32:32 +00:00
"flag"
2024-02-08 18:23:46 +00:00
"fmt"
"net"
2024-02-09 22:32:32 +00:00
"os"
"strconv"
2024-02-09 22:32:32 +00:00
"strings"
"sync"
"time"
2024-02-08 18:23:46 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
2024-02-09 22:32:32 +00:00
"github.com/probakowski/go-satel"
2024-02-08 18:23:46 +00:00
)
2024-02-09 22:32:32 +00:00
const (
2024-02-10 23:13:31 +00:00
MessageNotMoreOftenThanSeconds = 15
2024-02-09 22:32:32 +00:00
)
2024-02-10 23:13:31 +00:00
type TgSender struct {
bot *tgbotapi.BotAPI
2024-02-09 22:32:32 +00:00
}
2024-02-10 23:13:31 +00:00
func (self TgSender) Send(msg GenericMessage) error {
to_send := tgbotapi.NewMessage(msg.chat_id, msg.msg)
_, err := self.bot.Send(to_send)
return err
2024-02-10 06:51:36 +00:00
}
2024-02-10 23:13:31 +00:00
func send_tg_message(tg_events chan GenericMessage, msg string, chat_ids []int64) {
2024-02-10 06:51:36 +00:00
for _, chat_id := range chat_ids {
2024-02-10 23:13:31 +00:00
tg_events <- GenericMessage{chat_id, msg}
2024-02-10 06:51:36 +00:00
}
}
2024-02-11 10:51:41 +00:00
type RealSleeper struct {
duration time.Duration
}
func (self RealSleeper) Sleep(ch chan<- interface{}) {
time.Sleep(self.duration)
ch <- nil
}
2024-02-08 18:23:46 +00:00
func main() {
2024-02-10 06:51:36 +00:00
var (
2024-02-10 23:13:31 +00:00
wg sync.WaitGroup
tg_events = make(chan GenericMessage)
2024-02-11 10:51:41 +00:00
sleeper = RealSleeper{time.Second * 15}
2024-02-10 06:51:36 +00:00
)
2024-02-08 21:09:31 +00:00
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_raw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
2024-02-08 18:23:46 +00:00
flag.Parse()
if len(*satel_api_addr) == 0 || len(*satel_api_port) == 0 || len(*chat_id_raw) == 0 {
2024-02-08 21:09:31 +00:00
fmt.Println("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
2024-02-08 18:23:46 +00:00
os.Exit(1)
}
chat_ids_strings := strings.Split(*chat_id_raw, ",")
var chat_ids []int64
for _, chat_id_str := range chat_ids_strings {
chat_id, err := strconv.ParseInt(chat_id_str, 10, 64)
if err != nil {
fmt.Printf("Tried to use a non-int value for one of tg_chat_ids: %s. That's bad.", chat_id_str)
os.Exit(1)
}
chat_ids = append(chat_ids, chat_id)
}
2024-02-08 18:23:46 +00:00
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"))
2024-02-09 22:32:32 +00:00
if err != nil {
panic(err)
}
2024-02-10 23:13:31 +00:00
tgSender := TgSender{bot}
2024-02-11 10:51:41 +00:00
go tg_sender_worker(tg_events, tgSender, &wg, sleeper)
2024-02-08 18:23:46 +00:00
for e, ok := <-s.Events; ok; e, ok = <-s.Events {
2024-02-10 23:13:31 +00:00
send_tg_message(tg_events, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chat_ids)
2024-02-08 18:23:46 +00:00
}
2024-02-09 22:32:32 +00:00
close(tg_events)
wg.Wait()
2024-02-08 18:23:46 +00:00
}