-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
170 lines (136 loc) · 3.01 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
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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rollbar/rollbar-go"
)
type AkashiStampParams struct {
Token string `json:"token"`
Type int `json:"type"`
Timezone string `json:"timezone"`
}
type AkashiResponse struct {
Success bool `json:"success"`
}
type ClickType int
const (
ClickTypeSingle ClickType = iota
ClickTypeDouble
ClickTypeLong
)
type Recorder interface {
Do(context.Context, ClickType) error
}
type AkashiRecorder struct {
BaseURL string
CorpID string
Token string
}
func (rec AkashiRecorder) Do(ctx context.Context, cType ClickType) error {
const (
// means "出勤" in Akashi API.
PunchIn = 11
// means "退勤" in Akashi API.
PunchOut = 12
)
var t int
switch cType {
case ClickTypeSingle, ClickTypeLong:
t = PunchIn
case ClickTypeDouble:
t = PunchOut
}
params := AkashiStampParams{
Token: rec.Token,
Type: t,
Timezone: "+09:00",
}
jsonParams, err := json.Marshal(params)
if err != nil {
return err
}
url := rec.buildURL()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonParams))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(body))
status := resp.StatusCode
if status != http.StatusOK {
return fmt.Errorf("status code was not 200: %d", status)
}
var ar AkashiResponse
err = json.Unmarshal(body, &ar)
if err != nil {
return err
}
if !ar.Success {
return errors.New("something error from Akashi")
}
return nil
}
func (rec AkashiRecorder) buildURL() string {
return fmt.Sprintf("%s/%s/stamps", rec.BaseURL, rec.CorpID)
}
type Akapun struct {
Recorder Recorder
}
func (akapun Akapun) HandleRequest(
ctx context.Context,
event events.IoTOneClickEvent,
) (string, error) {
var cType ClickType
switch t := event.DeviceEvent.ButtonClicked.ClickType; t {
case "SINGLE":
cType = ClickTypeSingle
case "DOUBLE":
cType = ClickTypeDouble
case "LONG":
cType = ClickTypeLong
default:
panic(fmt.Errorf("unknown click type was given: %s", t))
}
if err := akapun.Recorder.Do(ctx, cType); err != nil {
panic(err)
}
return "OK", nil
}
func setupRollbar(token string) {
rollbar.SetToken(token)
rollbar.SetEnvironment("production")
rollbar.SetServerHost("AWS Lambda")
rollbar.SetServerRoot("github.com/shirakiya/akapun")
}
func main() {
const AkashiURL = "https://atnd.ak4.jp/api/cooperation"
corpID := os.Getenv("AKASHI_CORP_ID")
akashiToken := os.Getenv("AKASHI_TOKEN")
rollbarToken := os.Getenv("ROLLBAR_TOKEN")
setupRollbar(rollbarToken)
akapun := Akapun{
Recorder: AkashiRecorder{
BaseURL: AkashiURL,
CorpID: corpID,
Token: akashiToken,
},
}
lambda.Start(rollbar.LambdaWrapper(akapun.HandleRequest))
}