forked from gomodules/notify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.go
96 lines (80 loc) · 1.9 KB
/
chat.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package stride
import (
"errors"
"net/http"
"bitbucket.org/atlassian/go-stride/pkg/stride"
"github.com/appscode/envconfig"
"github.com/appscode/go-notify"
)
const (
UID = "stride"
)
type Options struct {
CloudID string `envconfig:"CLOUD_ID" required:"true"`
RoomToken string `envconfig:"ROOM_TOKEN"`
ClientID string `envconfig:"CLIENT_ID"`
ClientSecret string `envconfig:"CLIENT_SECRET"`
To []string `envconfig:"TO"`
}
type client struct {
opt Options
body string
}
var _ notify.ByChat = &client{}
func New(opt Options) *client {
return &client{opt: opt}
}
func Default() (*client, error) {
var opt Options
err := envconfig.Process(UID, &opt)
if err != nil {
return nil, err
}
return New(opt), nil
}
func Load(loader envconfig.LoaderFunc) (*client, error) {
var opt Options
err := envconfig.Load(UID, &opt, loader)
if err != nil {
return nil, err
}
return New(opt), nil
}
func (c client) UID() string {
return UID
}
func (c client) WithBody(body string) notify.ByChat {
c.body = body
return &c
}
func (c client) To(to string, cc ...string) notify.ByChat {
c.opt.To = append([]string{to}, cc...)
return &c
}
func (c *client) Send() error {
if len(c.opt.To) == 0 {
return errors.New("missing to")
}
if c.opt.RoomToken == "" && (c.opt.ClientID == "" || c.opt.ClientSecret == "") {
return errors.New("missing auth")
}
var sc stride.Client
if c.opt.RoomToken != "" {
if len(c.opt.To) > 1 {
return errors.New(`multiple "to" with room_token is not supported`)
}
sc = stride.NewRoomClient(c.opt.RoomToken, http.DefaultClient)
} else {
sc = stride.New(c.opt.ClientID, c.opt.ClientSecret)
}
for _, to := range c.opt.To {
conversation, err := sc.GetConversationByName(c.opt.CloudID, to)
if err != nil {
return err
}
if err := stride.SendText(sc, conversation.CloudID, conversation.ID, c.body); err != nil {
return err
}
}
return nil
}