forked from keybase/managed-bots
/
handler.go
148 lines (131 loc) · 3.96 KB
/
handler.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package meetbot
import (
"context"
"fmt"
"strings"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/malware-unicorn/managed-bots/base"
"golang.org/x/oauth2"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
type Handler struct {
*base.DebugOutput
stats *base.StatsRegistry
kbc *kbchat.API
db *base.GoogleOAuthDB
config *oauth2.Config
}
var _ base.Handler = (*Handler)(nil)
func NewHandler(stats *base.StatsRegistry, kbc *kbchat.API, debugConfig *base.ChatDebugOutputConfig,
db *base.GoogleOAuthDB, config *oauth2.Config) *Handler {
return &Handler{
DebugOutput: base.NewDebugOutput("Handler", debugConfig),
stats: stats.SetPrefix("Handler"),
kbc: kbc,
db: db,
config: config,
}
}
func (h *Handler) HandleNewConv(conv chat1.ConvSummary) error {
welcomeMsg := "Hello! I can get you set up with a Google Meet video call anytime, just send me `!meet`."
return base.HandleNewTeam(h.stats, h.DebugOutput, h.kbc, conv, welcomeMsg)
}
func (h *Handler) HandleAuth(msg chat1.MsgSummary, _ string) error {
return h.HandleCommand(msg)
}
func (h *Handler) HandleCommand(msg chat1.MsgSummary) error {
if msg.Content.Text == nil {
return nil
}
cmd := strings.TrimSpace(msg.Content.Text.Body)
switch {
case strings.HasPrefix(cmd, "!meet"):
h.stats.Count("meet")
return h.meetHandler(msg)
}
return nil
}
func (h *Handler) meetHandler(msg chat1.MsgSummary) error {
retry := func() error {
// retry auth after nuking stored credentials
if err := h.db.DeleteToken(base.IdentifierFromMsg(msg)); err != nil {
return err
}
return h.meetHandlerInner(msg)
}
err := h.meetHandlerInner(msg)
switch err.(type) {
case nil, base.OAuthRequiredError:
return nil
case *googleapi.Error:
h.Errorf("unable to get service %v, deleting credentials and retrying", err)
return retry()
default:
if strings.Contains(err.Error(), "oauth2: cannot fetch token") {
h.Errorf("unable to get service %v, deleting credentials and retrying", err)
return retry()
}
return err
}
}
func (h *Handler) meetHandlerInner(msg chat1.MsgSummary) error {
identifier := base.IdentifierFromMsg(msg)
client, err := base.GetOAuthClient(identifier, msg, h.kbc, h.config, h.db,
base.GetOAuthOpts{
AuthMessageTemplate: "Visit %s\n to authorize me to create events.",
OAuthOfflineAccessType: true,
})
if err != nil || client == nil {
return err
}
srv, err := calendar.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return err
}
// Create a bogus event on the primary calendar to host the meeting, it is
// instantly deleted once the meeting is created.
requestID, err := base.MakeRequestID()
if err != nil {
return err
}
event := &calendar.Event{
Start: &calendar.EventDateTime{
DateTime: "2015-01-01T00:00:00-00:00",
},
End: &calendar.EventDateTime{
DateTime: "2015-01-01T00:00:00-00:00",
},
ConferenceData: &calendar.ConferenceData{
CreateRequest: &calendar.CreateConferenceRequest{
RequestId: requestID,
},
},
}
calendarId := "primary"
event, err = srv.Events.Insert(calendarId, event).ConferenceDataVersion(1).Do()
if err != nil {
return fmt.Errorf("meetHandler: unable to create event %s", err)
}
if err := srv.Events.Delete(calendarId, event.Id).Do(); err != nil {
return fmt.Errorf("meetHandler: unable to delete event %s", err)
}
if confData := event.ConferenceData; confData != nil {
for _, ep := range confData.EntryPoints {
if ep.EntryPointType == "video" {
// strip protocol to skip unfurl prompt
link := strings.TrimPrefix(ep.Uri, "https://")
if link == "" {
continue
}
h.ChatEcho(msg.ConvID, link)
return nil
}
}
}
h.Debug("meetHandler: no event found, conferenceData: %+v", event.ConferenceData)
h.ChatEcho(msg.ConvID, "I wasn't able to create a meeting, please try again.")
return nil
}