Matterbridge sender
This commit is contained in:
parent
64f4930b4e
commit
94a47b2962
27
config.go
27
config.go
|
@ -25,16 +25,25 @@ type SatelChangeType struct {
|
||||||
changeType satel.ChangeType
|
changeType satel.ChangeType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MatterbridgeConfig struct {
|
||||||
|
URI string `yaml:"uri"`
|
||||||
|
Token string `yaml:"token"`
|
||||||
|
Gateway string `yaml:"gateway"`
|
||||||
|
/// Username from which the messages will appear
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
SatelAddr string `yaml:"satel-addr"`
|
SatelAddr string `yaml:"satel-addr"`
|
||||||
ChatIds []int64 `yaml:"tg-chat-ids"`
|
ChatIds []int64 `yaml:"tg-chat-ids"`
|
||||||
AllowedTypes []SatelChangeType `yaml:"allowed-types"`
|
AllowedTypes []SatelChangeType `yaml:"allowed-types"`
|
||||||
AllowedIndexes []int `yaml:"allowed-indexes"`
|
AllowedIndexes []int `yaml:"allowed-indexes"`
|
||||||
PoolInterval OwnDuration `yaml:"pool-interval"`
|
PoolInterval OwnDuration `yaml:"pool-interval"`
|
||||||
ArmCallbackUrls []string `yaml:"arm-callback-urls"`
|
ArmCallbackUrls []string `yaml:"arm-callback-urls"`
|
||||||
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"`
|
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"`
|
||||||
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
|
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
|
||||||
WriteMemoryProfile bool `yaml:"write-memory-profile"`
|
WriteMemoryProfile bool `yaml:"write-memory-profile"`
|
||||||
|
Matterbridge MatterbridgeConfig `yaml:"matterbridge"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
|
|
11
main.go
11
main.go
|
@ -69,14 +69,17 @@ func main() {
|
||||||
|
|
||||||
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
|
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
|
||||||
|
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
tgTpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
||||||
|
ircTpl := template.Must(template.New("IRCMessage").Parse(IRCMessageTemplate))
|
||||||
|
|
||||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||||
|
|
||||||
Consume(
|
Consume(
|
||||||
SendToTg(Throttle(NotifyViaHTTP(tgEvents, config, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),
|
SendToMatterbridge(
|
||||||
&wg, sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds)),
|
SendToTg(Throttle(NotifyViaHTTP(tgEvents, config, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),
|
||||||
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
|
&wg, sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds)),
|
||||||
|
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tgTpl),
|
||||||
|
s, config, &wg, log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds), ircTpl),
|
||||||
)
|
)
|
||||||
|
|
||||||
go CloseSatelOnCtrlC(s, &cleanShutdown)
|
go CloseSatelOnCtrlC(s, &cleanShutdown)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -108,3 +111,40 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait
|
||||||
|
|
||||||
return returnEvents
|
return returnEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MatterbridgeMessage struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Gateway string `json:"gateway"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config AppConfig, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage {
|
||||||
|
returnEvents := make(chan GenericMessage)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Add(1)
|
||||||
|
defer wg.Done()
|
||||||
|
for e := range events {
|
||||||
|
returnEvents <- e
|
||||||
|
body, err := json.Marshal(MatterbridgeMessage{
|
||||||
|
Text: e.Format(tpl, s, logger),
|
||||||
|
Username: config.Matterbridge.Username,
|
||||||
|
Gateway: config.Matterbridge.Gateway,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal("Could not marshal a JSON message: ", err)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, config.Matterbridge.URI, bytes.NewBuffer(body))
|
||||||
|
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", config.Matterbridge.Token)}
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
logger.Print("Could not POST ", config.Matterbridge.URI, ": ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Print("Notified via Matterbridge with result ", res.StatusCode)
|
||||||
|
}
|
||||||
|
close(returnEvents)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return returnEvents
|
||||||
|
}
|
||||||
|
|
|
@ -5,3 +5,7 @@ const TelegramMessageTemplate = `{{- range .Messages}}
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
Huh, no messages - this is a bug
|
Huh, no messages - this is a bug
|
||||||
{{- end}}`
|
{{- end}}`
|
||||||
|
|
||||||
|
const IRCMessageTemplate = `{{- range .Messages}} {{.GetName}}: {{.FormatEvent}}; {{- else -}}
|
||||||
|
Huh, no messages - this is a bug
|
||||||
|
{{- end}}`
|
||||||
|
|
|
@ -53,3 +53,17 @@ func TestTelegramTemplate(t *testing.T) {
|
||||||
|
|
||||||
// assert.Equal(t, "siemka", mockSender.message)
|
// assert.Equal(t, "siemka", mockSender.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIRCTemplate(t *testing.T) {
|
||||||
|
testEvents := make(chan GenericMessage)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
|
||||||
|
tpl, err := template.New("TestIRCTemplate").Parse(IRCMessageTemplate)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))
|
||||||
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// assert.Equal(t, "siemka", mockSender.message)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue