/
matrix.go
58 lines (47 loc) · 1.42 KB
/
matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package matrix
import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
t "github.com/containrrr/shoutrrr/pkg/types"
"net/url"
)
// Scheme is the identifying part of this service's configuration URL
const Scheme = "matrix"
// Service providing Matrix as a notification service
type Service struct {
standard.Standard
config *Config
client *client
pkr format.PropKeyResolver
}
// Initialize loads ServiceConfig from configURL and sets logger for this Service
func (s *Service) Initialize(configURL *url.URL, logger t.StdLogger) error {
s.SetLogger(logger)
s.config = &Config{}
s.pkr = format.NewPropKeyResolver(s.config)
if err := s.config.setURL(&s.pkr, configURL); err != nil {
return err
}
s.client = newClient(s.config.Host, s.config.DisableTLS, logger)
if s.config.User != "" {
return s.client.login(s.config.User, s.config.Password)
}
s.client.useToken(s.config.Password)
return nil
}
// Send notification
func (s *Service) Send(message string, params *t.Params) error {
config := *s.config
if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil {
return err
}
errors := s.client.sendMessage(message, s.config.Rooms)
if len(errors) > 0 {
for _, err := range errors {
s.Logf("error sending message: %v", err)
}
return fmt.Errorf("%v error(s) sending message, with initial error: %v", len(errors), errors[0])
}
return nil
}