-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebhook.go
More file actions
298 lines (250 loc) · 8 KB
/
webhook.go
File metadata and controls
298 lines (250 loc) · 8 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package instabot
import (
"encoding/json"
)
// WebhookEventType defines webhook event type.
type WebhookEventType string
// all webhook event types.
const (
WebhookEventTypeTextMessage WebhookEventType = WebhookEventType("text")
WebhookEventTypeImageMessage WebhookEventType = WebhookEventType("image")
WebhookEventTypeAudioMessage WebhookEventType = WebhookEventType("audio")
WebhookEventTypeVideoMessage WebhookEventType = WebhookEventType("video")
WebhookEventTypeFileMessage WebhookEventType = WebhookEventType("file")
WebhookEventTypeShare WebhookEventType = WebhookEventType("share")
WebhookEventTypeMessageReply WebhookEventType = WebhookEventType("message_reply")
WebhookEventTypeStoryMention WebhookEventType = WebhookEventType("story_mention")
WebhookEventTypeStoryReply WebhookEventType = WebhookEventType("story_reply")
WebhookEventTypeQuickReply WebhookEventType = WebhookEventType("quick_reply")
WebhookEventTypeReaction WebhookEventType = WebhookEventType("reaction")
WebhookEventTypeMessageSeen WebhookEventType = WebhookEventType("message_seen")
WebhookEventTypePostBack WebhookEventType = WebhookEventType("postback")
WebhookEventTypeEcho WebhookEventType = WebhookEventType("echo")
WebhookEventTypeDeleted WebhookEventType = WebhookEventType("deleted")
WebhookEventTypeUnsupported WebhookEventType = WebhookEventType("unsupported")
)
// ReplyToStory defines story details.
type ReplyToStory struct {
URL string `json:"url"`
ID string `json:"id"`
}
// ReplyTo defines either reply to message or story.
type ReplyTo struct {
MID string `json:"mid"`
Story *ReplyToStory `json:"story"`
}
// AttachmentPayload defines different attachment payload.
type AttachmentPayload struct {
URL string `json:"url"`
}
// Attachment defines attachment for different type like audio, video, media share, etc.
type Attachment struct {
Type string `json:"type"`
Payload AttachmentPayload `json:"payload"`
}
// ReferralProduct defines fb/instagram shop product.
type ReferralProduct struct {
ID string `json:"id"`
}
// Referral holds product referral.
// TODO: integrate later
type Referral struct {
Product ReferralProduct `json:"product"`
}
// Reaction defines reaction.
type Reaction struct {
MID string `json:"mid"`
Action string `json:"action"`
Reaction string `json:"reaction"`
Emoji string `json:"emoji"`
}
// Read defines seen message details.
type Read struct {
MID string `json:"mid"`
}
// WebhookQuickReply defines webhook quickreply.
type WebhookQuickReply struct {
Payload string `json:"payload"`
}
// Postback defines postback.
type Postback struct {
MID string `json:"mid"`
Title string `json:"title"`
Payload string `json:"payload"`
}
// WebhookMessage defines different message event type details.
type WebhookMessage struct {
MID string `json:"mid"`
Text string `json:"text"`
QuickReply *WebhookQuickReply `json:"quick_reply"`
Attachments []*Attachment `json:"attachments"`
ReplyTo *ReplyTo `json:"reply_to"`
IsEcho bool `json:"is_echo"`
IsUnsupported bool `json:"is_unsupported"`
IsDeleted bool `json:"is_deleted"`
}
func (m *WebhookMessage) isQuickReply() bool {
return m.QuickReply != nil
}
func (m *WebhookMessage) isStoryReply() bool {
return m.ReplyTo != nil && m.ReplyTo.Story != nil
}
func (m *WebhookMessage) isMessageReply() bool {
return m.ReplyTo != nil && m.ReplyTo.MID != ""
}
func (m *WebhookMessage) isStoryMention() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeStoryMention) {
return true
}
return false
}
func (m *WebhookMessage) isShare() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeShare) {
return true
}
return false
}
func (m *WebhookMessage) isImageMessage() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeImageMessage) {
return true
}
return false
}
func (m *WebhookMessage) isAudioMessage() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeAudioMessage) {
return true
}
return false
}
func (m *WebhookMessage) isVideoMessage() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeVideoMessage) {
return true
}
return false
}
func (m *WebhookMessage) isFileMessage() bool {
if len(m.Attachments) > 0 &&
m.Attachments[0].Type == string(WebhookEventTypeFileMessage) {
return true
}
return false
}
// Messaging defines events.
type Messaging struct {
Type WebhookEventType
Sender *Sender `json:"sender"`
Recipient *Recipient `json:"recipient"`
Timestamp int64 `json:"timestamp"`
Message *WebhookMessage `json:"message"`
Read *Read `json:"read"`
Reaction *Reaction `json:"reaction"`
Referral *Referral `json:"referral"`
PostBack *Postback `json:"postback"`
}
func (m *Messaging) isMessageEvent() bool {
return m.Message != nil
}
func (m *Messaging) isMessageSeenEvent() bool {
return m.Read != nil
}
func (m *Messaging) isReactionEvent() bool {
return m.Reaction != nil
}
func (m *Messaging) isReferralEvent() bool {
return m.Referral != nil
}
func (m *Messaging) isPostBackEvent() bool {
return m.PostBack != nil
}
// Entry defines entry.
type Entry struct {
ID string `json:"id"`
Time int64 `json:"time"`
Messaging []*Messaging `json:"messaging"`
}
// WebhookEvent defines instagram webhook event payload.
type WebhookEvent struct {
Object string `json:"object"`
Entries []*Entry `json:"entry"`
}
func (e *WebhookEvent) setType() {
for _, entry := range e.Entries {
for _, event := range entry.Messaging {
switch {
case event.isMessageEvent():
switch {
case event.Message.IsEcho:
event.Type = WebhookEventTypeEcho
case event.Message.IsDeleted:
event.Type = WebhookEventTypeDeleted
case event.Message.IsUnsupported:
event.Type = WebhookEventTypeUnsupported
case event.Message.isAudioMessage():
event.Type = WebhookEventTypeAudioMessage
case event.Message.isFileMessage():
event.Type = WebhookEventTypeFileMessage
case event.Message.isImageMessage():
event.Type = WebhookEventTypeImageMessage
case event.Message.isVideoMessage():
event.Type = WebhookEventTypeVideoMessage
case event.Message.isMessageReply():
event.Type = WebhookEventTypeMessageReply
case event.Message.isQuickReply():
event.Type = WebhookEventTypeQuickReply
case event.Message.isShare():
event.Type = WebhookEventTypeShare
case event.Message.isStoryMention():
event.Type = WebhookEventTypeStoryMention
case event.Message.isStoryReply():
event.Type = WebhookEventTypeStoryReply
case event.Message.Text != "":
event.Type = WebhookEventTypeTextMessage
}
case event.isMessageSeenEvent():
event.Type = WebhookEventTypeMessageSeen
case event.isReactionEvent():
event.Type = WebhookEventTypeReaction
case event.isPostBackEvent():
event.Type = WebhookEventTypePostBack
}
}
}
}
// UnmarshalJSON unmarshal json webhook events.
func (e *WebhookEvent) UnmarshalJSON(buffer []byte) error {
type rawEntry struct {
ID string `json:"id"`
Time int64 `json:"time"`
Messaging []*Messaging `json:"messaging"`
Message []*Messaging `json:"message"`
}
// TODO: use this wrapper to parse referral & other
// inconsistent structure later
type rawWebhookEvent struct {
Object string `json:"object"`
Entries []*rawEntry `json:"entry"`
}
re := rawWebhookEvent{}
if err := json.Unmarshal(buffer, &re); err != nil {
return err
}
e.Object = re.Object
for _, rEntry := range re.Entries {
eEntry := Entry{
ID: rEntry.ID,
Time: rEntry.Time,
Messaging: rEntry.Messaging,
}
if len(rEntry.Message) > 0 && len(rEntry.Messaging) == 0 {
eEntry.Messaging = rEntry.Message
}
e.Entries = append(e.Entries, &eEntry)
}
e.setType()
return nil
}