-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (70 loc) · 2.17 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
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
lark "github.com/larksuite/oapi-sdk-go/v3"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"log"
"main/larkAPI"
"main/lib"
"net/http"
"os"
"time"
)
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var appID = os.Getenv("APP_ID")
var appSecret = os.Getenv("APP_SECRET")
var approvalCode = os.Getenv("APPROVAL_CODE")
var client = lark.NewClient(appID, appSecret,
lark.WithLogLevel(larkcore.LogLevelDebug),
lark.WithReqTimeout(3*time.Second),
lark.WithEnableTokenCache(true),
lark.WithHttpClient(http.DefaultClient))
type FeishuEvent struct {
Type string `json:"type"`
Event json.RawMessage `json:"event"`
Token string `json:"token"`
Encrypt bool `json:"encrypt"`
}
var event FeishuEvent
if err := json.Unmarshal([]byte(request.Body), &event); err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
log.Println("event.Type: " + event.Type)
RES, FLAG, err := lib.IsChallenge(ctx, request)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
// validation request
if FLAG {
// subscribe approval by the way
err := larkAPI.Subscribe(appID, appSecret, approvalCode, client)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
return RES, nil
}
// if is not the validation request
instanceID, err := larkAPI.GetInstanceID(request.Body)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
instanceDetail, err := larkAPI.GetInstanceDetails(appID, appSecret, instanceID, client)
log.Println(instanceDetail)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
sta, err := lib.GetApprovalStatus(instanceDetail)
form, err := lib.GetApprovalForms(instanceDetail)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
lib.SendMsg(sta + ": " + form)
// return success
return events.APIGatewayProxyResponse{StatusCode: 200}, nil
}
func main() {
lambda.Start(Handler)
}