diff --git a/sender_sync.go b/sender_sync.go new file mode 100644 index 0000000..a6d9984 --- /dev/null +++ b/sender_sync.go @@ -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) +} diff --git a/sender_sync_test.go b/sender_sync_test.go new file mode 100644 index 0000000..20e841c --- /dev/null +++ b/sender_sync_test.go @@ -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)) +} diff --git a/telegram_utils_test.go b/telegram_utils_test.go index aa5cfd9..9df001d 100644 --- a/telegram_utils_test.go +++ b/telegram_utils_test.go @@ -7,19 +7,9 @@ import ( "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} ) @@ -27,9 +17,9 @@ var ( func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) { a := assert.New(t) tpl := template.Must(template.New("TelegramMessage").Parse("")) - mockBot := MockTgBotAPI{0} + mockBot := MockTgBotAPI{} tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}} tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl) - a.Equal(0, mockBot.callCount) + a.Equal(0, len(mockBot.messages)) } diff --git a/templates_test.go b/templates_test.go index 032f959..ae7b790 100644 --- a/templates_test.go +++ b/templates_test.go @@ -21,20 +21,6 @@ func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template) 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 ( tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true} tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true} diff --git a/test_utils.go b/test_utils.go index b2c4572..0dddc7c 100644 --- a/test_utils.go +++ b/test_utils.go @@ -1,6 +1,9 @@ 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 { 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 { 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 +}