-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (113 loc) · 3.12 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 (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
const location = "Asia/Tokyo"
func Env_load() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
Env_load()
var (
signingSecret string
)
var spreadsheetId = os.Getenv("SHEET_ID")
const valueRange = "A:B"
var sheetName = os.Getenv("SHEET_NAME")
const valueInputOption = "USER_ENTERED"
const insertDataOption = "INSERT_ROWS"
flag.StringVar(&signingSecret, "secret", os.Getenv("SLACK_SIGNING_SECRET"), "Your Slack app's signing secret")
flag.Parse()
http.HandleFunc("/body_tp/", func(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
loc, err := time.LoadLocation(location)
if err != nil {
loc = time.FixedZone(location, 9*60*60)
}
ctx := context.Background()
srv, err := sheets.NewService(ctx, option.WithCredentialsFile("credentials.json"))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
switch s.Command {
case "/record":
params := &slack.Msg{Text: s.Text}
b, err := json.Marshal(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
t := time.Now().In(loc)
var now = fmt.Sprintln(t.Format("2006-01-02 15:04:05"))
rb := &sheets.ValueRange{
MajorDimension: "ROWS",
Values: [][]interface{}{
{now, s.Text},
},
}
_, err = srv.Spreadsheets.Values.Append(spreadsheetId, sheetName+"!"+valueRange, rb).
ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
log.Fatalf("%v", err)
}
case "/logs":
valueRenderOption := "FORMATTED_VALUE"
dateTimeRenderOption := "FORMATTED_STRING"
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, sheetName+"!"+valueRange).ValueRenderOption(valueRenderOption).
DateTimeRenderOption(dateTimeRenderOption).Context(ctx).Do()
if err != nil {
log.Fatal(err)
}
var slackMsg strings.Builder
for _, v := range resp.Values {
slackMsg.WriteString(fmt.Sprintf("%s %s[C]\n", v[0], v[1]))
}
b, err := json.Marshal(slack.Msg{Text: slackMsg.String()})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
})
fmt.Println("[INFO] Server listening")
http.ListenAndServe(":3000", nil)
}