-
Notifications
You must be signed in to change notification settings - Fork 61
/
mattermost.go
52 lines (44 loc) · 1.43 KB
/
mattermost.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
package mattermost
import (
"bytes"
"fmt"
"net/http"
"net/url"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
)
// Service sends notifications to a pre-configured channel or user
type Service struct {
standard.Standard
config *Config
pkr format.PropKeyResolver
}
// Initialize loads ServiceConfig from configURL and sets logger for this Service
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
service.Logger.SetLogger(logger)
service.config = &Config{}
service.pkr = format.NewPropKeyResolver(service.config)
return service.config.setURL(&service.pkr, configURL)
}
// Send a notification message to Mattermost
func (service *Service) Send(message string, params *types.Params) error {
config := service.config
apiURL := buildURL(config)
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
return err
}
json, _ := CreateJSONPayload(config, message, params)
res, err := http.Post(apiURL, "application/json", bytes.NewReader(json))
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send notification to service, response status code %s", res.Status)
}
return err
}
// Builds the actual URL the request should go to
func buildURL(config *Config) string {
return fmt.Sprintf("https://%s/hooks/%s", config.Host, config.Token)
}