This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hangouts.go
64 lines (58 loc) · 1.54 KB
/
hangouts.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
package hangouts
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/seibert-media/golibs/log"
"go.uber.org/zap"
googleAuth "golang.org/x/oauth2/google"
)
// Hangouts handler
type Hangouts struct {
*http.Client
URL string
}
// New Hangouts client
func New(ctx context.Context, serviceAccount string) (*Hangouts, error) {
ctx = log.WithFields(ctx, zap.String("component", "hangouts"))
httpClient, err := googleAuth.DefaultClient(ctx, "https://www.googleapis.com/auth/chat.bot")
if err != nil {
return nil, err
}
return &Hangouts{
Client: httpClient,
}, nil
}
// NewWebhookClient for Hangouts
func NewWebhookClient(url string) (*Hangouts, error) {
return &Hangouts{
Client: &http.Client{},
URL: url,
}, nil
}
// Send Message to Hangouts
// space can be left empty if using webhooks as it is used to identify the channel messages are being sent to
func (h *Hangouts) Send(ctx context.Context, space string, msg *Message) error {
url := h.URL
if url == "" {
url = fmt.Sprintf("https://chat.googleapis.com/v1/%s/messages", space)
}
ctx = log.WithFields(ctx, zap.String("url", url))
data, err := json.Marshal(msg)
if err != nil {
return err
}
resp, err := h.Client.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
if resp.StatusCode/100 != 2 {
body, _ := ioutil.ReadAll(resp.Body)
log.From(ctx).Error("post message error", zap.String("status", resp.Status), zap.ByteString("body", body))
return fmt.Errorf("post message error: %s", body)
}
return nil
}