-
Notifications
You must be signed in to change notification settings - Fork 220
/
googlechat.go
110 lines (97 loc) · 3.27 KB
/
googlechat.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Package googlechat provides message notification integration sent to multiple
// spaces within a Google Chat Application.
package googlechat
import (
"context"
"fmt"
"google.golang.org/api/chat/v1"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
type spacesMessageCreator interface {
Create(string, *chat.Message) callCreator
}
type callCreator interface {
Do(...googleapi.CallOption) (*chat.Message, error)
}
var (
// Compile-time check to ensure that client implements the spaces message service
// interface.
_ spacesMessageCreator = new(messageCreator)
// Compile-time check to ensure that client implements the create call interface.
_ callCreator = new(chat.SpacesMessagesCreateCall)
)
// messageCreator is wrapper struct for the native chat.SpacesMessagesService struct.
// This exists so that we can mock the chat.SpacesMessagesCreateCall with the common
// interface "createCall".
type messageCreator struct {
*chat.SpacesMessagesService
}
func newMessageCreator(ctx context.Context, options ...option.ClientOption) (spacesMessageCreator, error) {
svc, err := chat.NewService(ctx, options...)
if err != nil {
return nil, err
}
return &messageCreator{svc.Spaces.Messages}, nil
}
// Create creates a createCall struct for google chat. In order to execute sending
// the message utilize the `.Do` method found on the createCall.
func (m *messageCreator) Create(parent string, message *chat.Message) callCreator {
return m.SpacesMessagesService.Create(parent, message)
}
// Service encapsulates the google chat client along with internal state for storing
// chat spaces.
type Service struct {
messageCreator spacesMessageCreator
spaces []string
}
// New returns an instance of the google chat notification service.
func New(options ...option.ClientOption) (*Service, error) {
ctx := context.Background()
svc, err := newMessageCreator(ctx, options...)
if err != nil {
return nil, err
}
s := &Service{
messageCreator: svc,
spaces: []string{},
}
return s, nil
}
// NewWithContext returns an instance of the google chat notification service with the
// specified context. Utilize this constructor if the message requires the context to
// be set.
func NewWithContext(ctx context.Context, options ...option.ClientOption) (*Service, error) {
svc, err := newMessageCreator(ctx, options...)
if err != nil {
return nil, err
}
s := &Service{
messageCreator: svc,
spaces: []string{},
}
return s, nil
}
// AddReceivers takes a name of authorized spaces and appends them to the internal
// spaces slice. The Send method will send a given message to all those spaces.
func (s *Service) AddReceivers(spaces ...string) {
s.spaces = append(s.spaces, spaces...)
}
// Send takes a message subject and a message body and sends them to all the spaces
// previously set.
func (s *Service) Send(ctx context.Context, subject, message string) error {
// Treating subject as message title
msg := &chat.Message{Text: subject + "\n" + message}
for _, space := range s.spaces {
parent := fmt.Sprintf("spaces/%s", space)
select {
case <-ctx.Done():
return ctx.Err()
default:
if _, err := s.messageCreator.Create(parent, msg).Do(); err != nil {
return fmt.Errorf("send message to the google chat space %q: %w", space, err)
}
}
}
return nil
}