forked from larksuite/oapi-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
189 lines (178 loc) · 8.06 KB
/
event.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
/*
* MIT License
*
* Copyright (c) 2022 Lark Technologies Pte. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"context"
"fmt"
"net/http"
larkhelpdesk "github.com/larksuite/oapi-sdk-go/v3/service/helpdesk/v1"
"github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/core/httpserverext"
"github.com/larksuite/oapi-sdk-go/v3/event"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher/callback"
"github.com/larksuite/oapi-sdk-go/v3/service/application/v6"
"github.com/larksuite/oapi-sdk-go/v3/service/approval/v4"
"github.com/larksuite/oapi-sdk-go/v3/service/contact/v3"
"github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"github.com/larksuite/oapi-sdk-go/v3/service/meeting_room/v1"
)
func main() {
//1212121212
handler := dispatcher.NewEventDispatcher("", "1212121212")
handler.OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP2MessageReadV1(func(ctx context.Context, event *larkim.P2MessageReadV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1MessageReadV1(func(ctx context.Context, event *larkim.P1MessageReadV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1MessageReceiveV1(func(ctx context.Context, event *larkim.P1MessageReceiveV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1UserStatusChangedV3(func(ctx context.Context, event *larkcontact.P1UserStatusChangedV3) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1UserChangedV3(func(ctx context.Context, event *larkcontact.P1UserChangedV3) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1DepartmentChangedV3(func(ctx context.Context, event *larkcontact.P1DepartmentChangedV3) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1ContactScopeChangedV3(func(ctx context.Context, event *larkcontact.P1ContactScopeChangedV3) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1AddBotV1(func(ctx context.Context, event *larkim.P1AddBotV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1RemoveAddBotV1(func(ctx context.Context, event *larkim.P1RemoveBotV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1UserInOutChatV1(func(ctx context.Context, event *larkim.P1UserInOutChatV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1ChatDisbandV1(func(ctx context.Context, event *larkim.P1ChatDisbandV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1GroupSettingUpdatedV1(func(ctx context.Context, event *larkim.P1GroupSettingUpdatedV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1AppOpenV6(func(ctx context.Context, event *larkapplication.P1AppOpenV6) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1P2PChatCreatedV1(func(ctx context.Context, event *larkim.P1P2PChatCreatedV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1ThirdPartyMeetingRoomChangedV1(func(ctx context.Context, event *larkmeeting_room.P1ThirdPartyMeetingRoomChangedV1) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1LeaveApprovalV4(func(ctx context.Context, event *larkapproval.P1LeaveApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1WorkApprovalV4(func(ctx context.Context, event *larkapproval.P1WorkApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1ShiftApprovalV4(func(ctx context.Context, event *larkapproval.P1ShiftApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1RemedyApprovalV4(func(ctx context.Context, event *larkapproval.P1RemedyApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1TripApprovalV4(func(ctx context.Context, event *larkapproval.P1TripApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1OutApprovalV4(func(ctx context.Context, event *larkapproval.P1OutApprovalV4) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1AppStatusChangedV6(func(ctx context.Context, event *larkapplication.P1AppStatusChangedV6) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1AppUninstalledV6(func(ctx context.Context, event *larkapplication.P1AppUninstalledV6) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnP1OrderPaidV6(func(ctx context.Context, event *larkapplication.P1OrderPaidV6) error {
fmt.Println(larkcore.Prettify(event))
fmt.Println(event.RequestId())
return nil
}).OnCustomizedEvent("这里填入你要自定义订阅的 event 的 key,例如 out_approval", func(ctx context.Context, event *larkevent.EventReq) error {
// 原生消息体
fmt.Println(string(event.Body))
fmt.Println(larkcore.Prettify(event.Header))
fmt.Println(larkcore.Prettify(event.RequestURI))
fmt.Println(event.RequestId())
// 处理消息
cipherEventJsonStr, err := handler.ParseReq(ctx, event)
if err != nil {
// 错误处理
return err
}
plainEventJsonStr, err := handler.DecryptEvent(ctx, cipherEventJsonStr)
if err != nil {
// 错误处理
return err
}
// 处理解密后的 消息体
fmt.Println(plainEventJsonStr)
return nil
}).OnP2TicketMessageCreatedV1(func(ctx context.Context, event *larkhelpdesk.P2TicketMessageCreatedV1) error {
fmt.Println(event)
return nil
}).OnP2TicketUpdatedV1(func(ctx context.Context, event *larkhelpdesk.P2TicketUpdatedV1) error {
fmt.Println(event)
return nil
}).OnP2CardActionTrigger(func(ctx context.Context, event *callback.CardActionTriggerEvent) (*callback.CardActionTriggerReponse, error) {
fmt.Println("receive card action")
fmt.Println(larkcore.Prettify(event))
return nil, nil
}).OnP2CardURLPreviewGet(func(ctx context.Context, event *callback.URLPreviewGetEvent) (*callback.URLPreviewGetResponse, error) {
fmt.Println(event)
return nil, nil
})
// 注册 http 路由
http.HandleFunc("/webhook/event", httpserverext.NewEventHandlerFunc(handler,
larkevent.WithLogLevel(larkcore.LogLevelDebug)))
// 启动服务
err := http.ListenAndServe(":7777", nil)
if err != nil {
panic(err)
}
}