1
0
Fork 0

Sync senders

This commit is contained in:
Michał Rudowicz 2025-01-06 21:38:43 +01:00
parent f7bb03721c
commit 5f93bc084e
5 changed files with 132 additions and 27 deletions

63
sender_sync.go Normal file
View File

@ -0,0 +1,63 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
)
type SendToTelegramSync struct {
SyncFilterImpl
sender Sender
logger *log.Logger
tpl *template.Template
}
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 {
SyncFilterImpl
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)
}

40
sender_sync_test.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"html/template"
"io"
"log"
"os"
"testing"
"git.sr.ht/~michalr/go-satel"
"github.com/stretchr/testify/assert"
)
var (
tgSyncSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
)
func TestSyncTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
a := assert.New(t)
tpl := template.Must(template.New("TelegramMessage").Parse(""))
mockBot := MockTgBotAPI{}
s := MockSatelNameGetter{"test_name"}
sender := TgSender{&mockBot, s, log.New(io.Discard, "", 0), []int64{}}
tested := SendToTelegramSync{SyncFilterImpl{}, &sender, log.New(io.Discard, "", 0), tpl}
tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
a.Equal(0, len(mockBot.messages))
}
func TestSyncTelegramSender_SendsMessageToAllChatIds(t *testing.T) {
a := assert.New(t)
tpl := template.Must(template.New("TelegramMessage").Parse(""))
mockBot := MockTgBotAPI{}
s := MockSatelNameGetter{"test_name"}
sender := TgSender{&mockBot, s, log.New(os.Stderr, "", 0), []int64{1, 2, 3}}
tested := SendToTelegramSync{SyncFilterImpl{}, &sender, log.New(os.Stderr, "", 0), tpl}
tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
a.Equal(3, len(mockBot.messages))
}

View File

@ -7,19 +7,9 @@ import (
"testing" "testing"
"git.sr.ht/~michalr/go-satel" "git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/stretchr/testify/assert" "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 ( var (
tgSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true} tgSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
) )
@ -27,9 +17,9 @@ var (
func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) { func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
a := assert.New(t) a := assert.New(t)
tpl := template.Must(template.New("TelegramMessage").Parse("")) tpl := template.Must(template.New("TelegramMessage").Parse(""))
mockBot := MockTgBotAPI{0} mockBot := MockTgBotAPI{}
tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}} tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}}
tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl) tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl)
a.Equal(0, mockBot.callCount) a.Equal(0, len(mockBot.messages))
} }

View File

@ -21,20 +21,6 @@ func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template)
return nil return nil
} }
type MockSatelNameGetter struct {
name string
}
func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) {
retval := satel.NameEvent{
DevType: devType,
DevNumber: index,
DevTypeFunction: 0,
DevName: self.name,
}
return &retval, nil
}
var ( var (
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true} tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true} tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}

View File

@ -1,6 +1,9 @@
package main package main
import "git.sr.ht/~michalr/go-satel" import (
"git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func makeTestSatelEvent(changeType satel.ChangeType, index int, val bool) satel.Event { func makeTestSatelEvent(changeType satel.ChangeType, index int, val bool) satel.Event {
return satel.Event{ return satel.Event{
@ -11,3 +14,26 @@ func makeTestSatelEvent(changeType satel.ChangeType, index int, val bool) satel.
func makeGenericMessage(changeType satel.ChangeType, index int, val bool) GenericMessage { func makeGenericMessage(changeType satel.ChangeType, index int, val bool) GenericMessage {
return GenericMessage{[]satel.BasicEventElement{{Type: changeType, Index: index, Value: val}}} return GenericMessage{[]satel.BasicEventElement{{Type: changeType, Index: index, Value: val}}}
} }
type MockTgBotAPI struct {
messages []tgbotapi.Chattable
}
func (self *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
self.messages = append(self.messages, c)
return tgbotapi.Message{}, nil
}
type MockSatelNameGetter struct {
name string
}
func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) {
retval := satel.NameEvent{
DevType: devType,
DevNumber: index,
DevTypeFunction: 0,
DevName: self.name,
}
return &retval, nil
}