-
Notifications
You must be signed in to change notification settings - Fork 8
/
event.go
160 lines (147 loc) · 4.83 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
package linebot
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"io"
"log"
"net/http"
)
// Webhook ...
type Webhook struct {
Events []Event `json:"events"`
}
// Event ...
type Event struct {
ReplyToken string `json:"replyToken"`
Type EventType `json:"type"`
Timestamp int64 `json:"timestamp"`
Source EventSource `json:"source"`
Message struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Text string `json:"text"`
Duration int `json:"duration"`
Title string `json:"title"`
Address string `json:"address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
PackageID string `json:"packageId"`
StickerID string `json:"stickerId"`
} `json:"message"`
Postback `json:"postback"`
Beacon `json:"beacon"`
}
// EventType ...
type EventType string
// EentTypes
const (
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
)
// EventSourceType ...
type EventSourceType string
// EventSourceType ....
const (
EventSourceTypeUser EventSourceType = "user"
EventSourceTypeGroup EventSourceType = "group"
EventSourceTypeRoom EventSourceType = "room"
)
// EventSource ...
type EventSource struct {
Type EventSourceType `json:"type"`
UserID string `json:"userId"`
GroupID string `json:"groupId"`
RoomID string `json:"roomId"`
}
// Postback ...
type Postback struct {
Data string `json:"data"`
}
// Beacon ...
type Beacon struct {
Hwid string `json:"hwid"`
Type string `json:"type"`
}
// Middleware ...
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
if !validateSignature(r.Header.Get("x-line-signature"), body) {
log.Fatal(errors.New("invalid signature"))
}
webhook := new(Webhook)
if err = json.Unmarshal(body, webhook); err != nil {
log.Fatal(err)
}
for _, event := range webhook.Events {
eventHandler.OnEvent(event)
switch event.Type {
case EventTypeMessage:
switch event.Message.Type {
case MessageTypeText:
eventHandler.OnTextMessage(event.Source, event.ReplyToken, event.Message.Text)
case MessageTypeImage:
eventHandler.OnImageMessage(event.Source, event.ReplyToken, event.Message.ID)
case MessageTypeVideo:
eventHandler.OnVideoMessage(event.Source, event.ReplyToken, event.Message.ID)
case MessageTypeAudio:
eventHandler.OnAudioMessage(event.Source, event.ReplyToken, event.Message.ID)
case MessageTypeLocation:
eventHandler.OnLocationMessage(event.Source, event.ReplyToken, event.Message.Title, event.Message.Address, event.Message.Latitude, event.Message.Longitude)
case MessageTypeSticker:
eventHandler.OnStickerMessage(event.Source, event.ReplyToken, event.Message.PackageID, event.Message.StickerID)
}
case EventTypeFollow:
eventHandler.OnFollowEvent(event.Source, event.ReplyToken)
case EventTypeUnfollow:
eventHandler.OnUnFollowEvent(event.Source)
case EventTypeJoin:
eventHandler.OnJoinEvent(event.Source, event.ReplyToken)
case EventTypeLeave:
eventHandler.OnLeaveEvent(event.Source)
case EventTypePostback:
eventHandler.OnPostbackEvent(event.Source, event.ReplyToken, event.Postback.Data)
case EventTypeBeacon:
eventHandler.OnBeaconEvent(event.Source, event.ReplyToken, event.Beacon.Hwid, event.Beacon.Type)
}
}
next.ServeHTTP(w, r)
})
}
func validateSignature(signature string, body []byte) bool {
decoded, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return false
}
hash := hmac.New(sha256.New, []byte(channelSecret))
hash.Write(body)
return hmac.Equal(decoded, hash.Sum(nil))
}
// EventHandler ...
type EventHandler interface {
OnEvent(event Event)
OnFollowEvent(source EventSource, replyToken string)
OnUnFollowEvent(source EventSource)
OnJoinEvent(source EventSource, replyToken string)
OnLeaveEvent(source EventSource)
OnPostbackEvent(source EventSource, replyToken, postbackData string)
OnBeaconEvent(source EventSource, replyToken, beaconHwid string, beaconType string)
OnTextMessage(source EventSource, replyToken, text string)
OnImageMessage(source EventSource, replyToken, id string)
OnVideoMessage(source EventSource, replyToken, id string)
OnAudioMessage(source EventSource, replyToken, id string)
OnLocationMessage(source EventSource, replyToken string, title, address string, latitude, longitude float64)
OnStickerMessage(source EventSource, replyToken, packageID, stickerID string)
}