This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gopher.go
209 lines (171 loc) · 5.43 KB
/
gopher.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2016 Florin Pățan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Command gopher
//
// This is a Slack bot for the Gophers Slack.
//
// You can get an invite from https://invite.slack.golangbridge.org/
//
// To run this you need to set the ` GOPHERS_SLACK_BOT_TOKEN ` environment
// variable with the Slack bot token and that's it.
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/gopheracademy/gopher/bot"
"cloud.google.com/go/datastore"
"cloud.google.com/go/trace"
"github.com/ChimeraCoder/anaconda"
"github.com/gorilla/mux"
"github.com/nlopes/slack"
"golang.org/x/net/context"
"google.golang.org/api/option"
)
const gerritLink = "https://go-review.googlesource.com/changes/?q=status:merged&O=12&n=100"
var (
botVersion = "HEAD"
info = `{ "version": "` + botVersion + `" }`
)
func main() {
log.SetFlags(log.Lshortfile)
botName := os.Getenv("GOPHERS_SLACK_BOT_NAME")
slackBotToken := os.Getenv("GOPHERS_SLACK_BOT_TOKEN")
twitterConsumerKey := os.Getenv("GOPHER_SLACK_BOT_TWITTER_CONSUMER_KEY")
twitterConsumerSecret := os.Getenv("GOPHER_SLACK_BOT_TWITTER_CONSUMER_SECRET")
twitterAccessToken := os.Getenv("GOPHER_SLACK_BOT_TWITTER_ACCESS_TOKEN")
twitterAccessTokenSecret := os.Getenv("GOPHER_SLACK_BOT_TWITTER_ACCESS_TOKEN_SECRET")
devMode := os.Getenv("GOPHERS_SLACK_BOT_DEV_MODE") == "true"
if slackBotToken == "" {
log.Fatalln("slack bot token must be set in GOPHERS_SLACK_BOT_TOKEN")
}
if botName == "" {
if devMode {
log.Fatalln("bot name must be set in GOPHERS_SLACK_BOT_NAME")
}
botName = "tempbot"
}
if twitterConsumerKey == "" {
log.Fatalln("missing GOPHER_SLACK_BOT_TWITTER_CONSUMER_KEY")
}
if twitterConsumerSecret == "" {
log.Fatalln("missing GOPHER_SLACK_BOT_TWITTER_CONSUMER_SECRET")
}
if twitterAccessToken == "" {
log.Fatalln("missing GOPHER_SLACK_BOT_TWITTER_ACCESS_TOKEN")
}
if twitterAccessTokenSecret == "" {
log.Fatalln("missing GOPHER_SLACK_BOT_TWITTER_ACCESS_TOKEN_SECRET")
}
httpClient := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
ctx := context.Background()
projectID := "gophers-slack-bot"
traceClient, err := trace.NewClient(ctx, projectID, option.WithServiceAccountFile("/tmp/trace/trace.json"))
startupSpan := traceClient.NewSpan("b.main")
ctx = trace.NewContext(ctx, startupSpan)
traceHttpClient := traceClient.NewHTTPClient(httpClient)
slack.SetHTTPClient(traceHttpClient)
slackBotAPI := slack.New(slackBotToken)
botName = strings.TrimPrefix(botName, "@")
anaconda.SetConsumerKey(twitterConsumerKey)
anaconda.SetConsumerSecret(twitterConsumerSecret)
twitterAPI := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
rtmOptions := &slack.RTMOptions{}
slackBotRTM := slackBotAPI.NewRTMWithOptions(rtmOptions)
go slackBotRTM.ManageConnection()
runtime.Gosched()
dsClient, err := datastore.NewClient(ctx, projectID, option.WithServiceAccountFile("/tmp/datastore/datastore.json"))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer dsClient.Close()
b := bot.NewBot(slackBotAPI, dsClient, traceClient, twitterAPI, traceHttpClient, gerritLink, botName, slackBotToken, botVersion, devMode, log.Printf)
if err := b.Init(ctx, slackBotRTM, startupSpan); err != nil {
panic(err)
}
_, err = b.GetLastSeenCL(ctx)
if err != nil {
log.Printf("got error: %v\n", err)
panic(err)
}
go func() {
<-time.After(1 * time.Second)
for i := 0; i < 7; i++ {
b.MonitorGerrit(30 * time.Minute)
log.Printf("monitoring Gerrit failed %d times\n", i+1)
if i == 6 {
break
}
time.Sleep(time.Duration(i*10) * time.Second)
}
panic("monitoring Gerrit was terminated")
}()
go func() {
for msg := range slackBotRTM.IncomingEvents {
switch message := msg.Data.(type) {
case *slack.MessageEvent:
go b.HandleMessage(message)
case *slack.TeamJoinEvent:
go b.TeamJoined(message)
}
}
}()
go func(traceClient *trace.Client) {
healthz := func(traceClient *trace.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
span := traceClient.SpanFromRequest(r)
defer span.Finish()
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, info)
}
}
r := mux.NewRouter()
r.HandleFunc("/healthz", healthz(traceClient)).
Name("info").
Methods("GET")
s := http.Server{
Addr: ":8081",
Handler: r,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Fatal(s.ListenAndServe())
}(traceClient)
go func(){
gotimefm := time.NewTicker(1 * time.Minute)
defer gotimefm.Stop()
for range gotimefm.C {
b.GoTimeFM()
}
}()
log.Println("Gopher is now running")
startupSpan.Finish()
select {}
}