1
0
Fork 0

Compare commits

..

No commits in common. "050dcbe72b2d58fb866415d19413e264175e0e2d" and "0c873ff77d861de772aed3db0312f63a5d3b6eec" have entirely different histories.

9 changed files with 55 additions and 113 deletions

View File

@ -4,8 +4,7 @@
Warning: this is a proof of concept, don't rely on it
It was very basically tested, and while it seems to work, approach it without any expectations.
In other words - treat it as a toy, not as a tool that will save your life or valuables, because it probably won't.
I didn't even test it yet so no idea if it works
## Usage
@ -67,41 +66,10 @@ Supported types are:
- `partition-with-violated-zones`
- `zone-isolate`
### Filtering events by index (which meaning depends on the change type)
### Filtering events by index (I guess "zone", but I'm not sure)
Use the `--allowed-indexes=1,2,3,...` command line parameter to set the list of allowed indexes (of course provide your own list instead of `1,2,3,...`). If that parameter is not provided, then all indexes are allowed; otherwise the notification is sent for all indexes.
## example systemd unit
```
[Unit]
Description=Satel Alarm Telegram Status Notifier
Requires=network.target
After=network.target
[Service]
Type=simple
ExecStart=/path/to/alarm_bot --satel-addr=192.168.13.37 --satel-port=7094 --tg-chat-id=1234,4567,9876
Environment=TELEGRAM_APITOKEN=YOUR_API_TOKEN
Environment=NOTIFY_URL_DISARM="http://localhost/disarmed"
Environment=NOTIFY_URL_ARM="http://localhost/armed"
DynamicUser=True
RuntimeDirectory=hswro-alarm-bot
StateDirectory=hswro-alarm-bot
RestartSec=30
Restart=on-failure
CPUAccounting=true
CPUQuota=5%
MemoryAccounting=true
MemoryHigh=25M
MemoryMax=50M
[Install]
WantedBy=multi-user.target
```
## Debugging
Set the `OMIT_TG` environment variable to, well, omit sending anything over to Telegram and just see the logs instead.

View File

@ -89,7 +89,7 @@ func FilterByLastSeen(ev <-chan satel.Event, wg *sync.WaitGroup, dataStore *Data
func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage {
if msg == nil {
msg = &GenericMessage{make([]satel.BasicEventElement, 0)}
msg = &GenericMessage{new.ChatIds, make([]satel.BasicEventElement, 0)}
}
throughNewMessages:

View File

@ -251,12 +251,12 @@ func TestThrottle(t *testing.T) {
wg.Done()
}()
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1}}
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest2}}
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest3}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest2}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest3}}
*mockSleeper.ch <- nil
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest4}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest4}}
close(testEvents)
wg.Wait()
@ -272,7 +272,7 @@ func TestThrottle(t *testing.T) {
}
func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage {
retval := GenericMessage{make([]satel.BasicEventElement, 0)}
retval := GenericMessage{TgChatId{123}, make([]satel.BasicEventElement, 0)}
for i := 0; i < numElements; i++ {
retval.Messages = append(retval.Messages, element)

33
main.go
View File

@ -21,6 +21,33 @@ const (
PersistenceFilename = "hs_wro_last_seen.bin"
)
type TgSender struct {
bot *tgbotapi.BotAPI
s SatelNameGetter
logger *log.Logger
}
func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
chatIds := msg.ChatIds.GetTgIds()
if chatIds == nil {
return nil
}
message := msg.Format(tpl, self.s, self.logger)
for _, chatId := range *chatIds {
toSend := tgbotapi.NewMessage(chatId, message)
toSend.ParseMode = "HTML"
_, err := self.bot.Send(toSend)
if err != nil {
return err
}
}
return nil
}
func sendTgMessage(tgEvents chan GenericMessage, msg []satel.BasicEventElement, chatId int64) {
tgEvents <- GenericMessage{TgChatId{chatId}, msg}
}
type RealSleeper struct {
duration time.Duration
}
@ -117,7 +144,7 @@ func main() {
}
logger.Print("Created Telegram Bot API client")
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), chatIds}
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds)}
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
@ -135,7 +162,9 @@ func main() {
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
&wg, allowedTypes, allowedIndexes) {
logger.Print("Received change from SATEL: ", e)
tgEvents <- GenericMessage{e.BasicEvents}
for _, chatId := range chatIds {
sendTgMessage(tgEvents, e.BasicEvents, chatId)
}
}
close(tgEvents)

View File

@ -32,10 +32,12 @@ type SatelMsgContent struct {
}
type GenericMessage struct {
ChatIds ChatId
Messages []satel.BasicEventElement
}
type SatelMessage struct {
ChatIds ChatId
Messages []SatelMsgContent
}
@ -64,6 +66,7 @@ func (self SatelMsgContent) GetName() string {
func (self GenericMessage) Format(template *template.Template, s SatelNameGetter, logger *log.Logger) string {
b := strings.Builder{}
template.Execute(&b, SatelMessage{
self.ChatIds,
getSatelMessageContent(self.Messages, s, logger),
})
return b.String()

View File

@ -22,6 +22,18 @@ type Sleeper interface {
Sleep(ch chan<- interface{})
}
type ChatId interface {
GetTgIds() *[]int64
}
type TgChatId struct {
tgChatId int64
}
func (self TgChatId) GetTgIds() *[]int64 {
return &[]int64{self.tgChatId}
}
func Consume(events <-chan GenericMessage) {
go func() {
for range events {

View File

@ -1,35 +0,0 @@
package main
import (
"html/template"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type TelegramBotSender interface {
Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
}
type TgSender struct {
bot TelegramBotSender
s SatelNameGetter
logger *log.Logger
chatIds []int64
}
func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
if len(self.chatIds) == 0 {
return nil
}
message := msg.Format(tpl, self.s, self.logger)
for _, chatId := range self.chatIds {
toSend := tgbotapi.NewMessage(chatId, message)
toSend.ParseMode = "HTML"
_, err := self.bot.Send(toSend)
if err != nil {
return err
}
}
return nil
}

View File

@ -1,35 +0,0 @@
package main
import (
"html/template"
"io"
"log"
"testing"
"git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/stretchr/testify/assert"
)
type MockTgBotAPI struct {
callCount int
}
func (self *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
self.callCount += 1
return tgbotapi.Message{}, nil
}
var (
tgSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
)
func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
a := assert.New(t)
tpl := template.Must(template.New("TelegramMessage").Parse(""))
mockBot := MockTgBotAPI{0}
tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}}
tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl)
a.Equal(0, mockBot.callCount)
}

View File

@ -47,7 +47,7 @@ func TestTelegramTemplate(t *testing.T) {
tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate)
assert.NoError(t, err)
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
close(testEvents)
wg.Wait()