-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (105 loc) · 2.9 KB
/
main.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
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"encoding/json"
"github.com/kkweon/timebot"
)
var clientId string
var clientSecret string
func init() {
var ok bool
clientId, ok = os.LookupEnv("SLACK_CLIENT_ID")
if !ok {
log.Fatalln("$SLACK_CLIENT_ID is not available")
}
clientSecret, ok = os.LookupEnv("SLACK_CLIENT_SECRET")
if !ok {
log.Fatalln("$SLACK_CLIENT_SECRET is not available")
}
}
func main() {
token := flag.String("token", "", "Slack Token")
debug := flag.Bool("debug", false, "Turn on debug")
flag.Parse()
if *token == "" {
tok, ok := os.LookupEnv("SLACK_BOT_TOKEN")
if !ok {
log.Fatalln("--token or env SLACK_BOT_TOKEN variable is required")
}
*token = tok
}
port, ok := os.LookupEnv("PORT")
if !ok {
log.Println("Unalbe to retrieve $PORT environment variable")
port = "8080"
}
go timebot.Main(*token, *debug)
http.HandleFunc("/healthcheck", healthCheck)
http.HandleFunc("/oauth/slack", slackHandler)
log.Println("The server is running at :" + port)
http.ListenAndServe(":"+port, nil)
}
func healthCheck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("true"))
}
type OAuthGenerated struct {
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
TeamName string `json:"team_name"`
TeamID string `json:"team_id"`
IncomingWebhook struct {
URL string `json:"url"`
Channel string `json:"channel"`
ConfigurationURL string `json:"configuration_url"`
} `json:"incoming_webhook"`
Bot struct {
BotUserID string `json:"bot_user_id"`
BotAccessToken string `json:"bot_access_token"`
} `json:"bot"`
}
func slackHandler(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
if code == "" {
http.Error(w, "missing ?code", http.StatusBadRequest)
return
}
data := getClientSecret()
data["code"] = []string{code}
resp, err := http.PostForm("https://slack.com/api/oauth.access", data)
if err != nil {
http.Error(w, "Fail while sending a request https://slack.com/api/oauth.access", http.StatusInternalServerError)
return
}
var oauth OAuthGenerated
allbyte, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
log.Println(string(allbyte))
err = json.Unmarshal(allbyte, &oauth)
// err = json.NewDecoder(resp.Body).Decode(&oauth)
if err != nil {
http.Error(w, "Fail while decoding JSON", http.StatusInternalServerError)
return
}
token := oauth.Bot.BotAccessToken
if token != "" {
go timebot.Main(token, false)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("true"))
return
}
http.Error(w, "Fail while decoding JSON", http.StatusInternalServerError)
return
}
func getClientSecret() map[string][]string {
args := make(map[string][]string)
args["client_id"] = []string{clientId}
args["client_secret"] = []string{clientSecret}
return args
}