-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathraw.go
167 lines (143 loc) · 5.11 KB
/
raw.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
package fbbot
import (
"github.com/sirupsen/logrus"
)
// rawCallbackMessage is data you will receive at your webhook
type rawCallbackMessage struct {
// Object indicates the object type that this payload applies to.
// It could be: user, page, permissions, payments.
// In this case, value will alway be "page".
RawObject string `json:"object"`
// rawEntries is a slice containing event data of the same object type
// that are batched together
RawEntries []struct {
// rawID is ID of the object that triggers this event.
// In this case, it will alway be Page ID
RawID string `json:"id"`
// rawEventTime is time the event data was sent
RawEventTime int64 `json:"time"`
// rawMessaging contains data related to messaging
RawMessaging []rawMessageData `json:"messaging"`
} `json:"entry"`
}
// rawMessageData contains data related to a message
type rawMessageData struct {
// rawSender is user that sends message
RawSender User `json:"sender"`
// rawRecipient is user that receives message
// In this case, RawRecipient is your Page
RawRecipient Page `json:"recipient"`
// rawTimestamp is the time messageData was sent
RawTimestamp int64 `json:"timestamp"`
RawMessage *rawMessage `json:"message"`
Postback *Postback `json:"postback"`
Delivery *Delivery `json:"delivery"`
Optin *Optin `json:"optin"`
Read *Read `json:"read"`
CheckoutUpdate *CheckoutUpdate `json:"checkout_update"`
Payment *Payment `json:"payment"`
}
// rawMessage is a Facebook message
type rawMessage struct {
// rawMid is message ID
RawMid string `json:"mid"`
// rawSeq is message sequence number
RawSeq int `json:"seq"`
// rawText is text of message
RawText string `json:"text"`
// for quick reply
RawQuickreply rawQuickreply `json:"quick_reply"`
RawIsEcho bool `json:"is_echo"`
RawAppID int64 `json:"app_id"`
// rawAttachments is a slice containing attachment data
RawAttachments []rawAttachment `json:"attachments"`
}
// rawAttachment is attached image, video, audio or location
type rawAttachment struct {
// rawType is type of the attachment
// It could be: image, video, audio or location
RawType string `json:"type"`
// Attachment file
RawPayload rawPayload `json:"payload"`
}
// Attachment file
type rawPayload struct {
// URL of the attachment file
RawURL string `json:"url"`
RawCoordinates rawCoordinates `json:"coordinates"`
StickerID int64 `json:"sticker_id"`
}
type rawCoordinates struct {
RawLat float64 `json:"lat"`
RawLong float64 `json:"long"`
}
type rawQuickreply struct {
Payload string `json:"payload"`
}
func (cbMsg *rawCallbackMessage) Unbox() []interface{} {
var messages []interface{}
for _, entry := range cbMsg.RawEntries {
for _, rawMessageData := range entry.RawMessaging {
if rawMessageData.RawMessage != nil {
messages = append(messages, buildMessage(rawMessageData))
} else if rawMessageData.Postback != nil {
rawMessageData.Postback.Sender = rawMessageData.RawSender
messages = append(messages, rawMessageData.Postback)
} else if rawMessageData.Delivery != nil {
messages = append(messages, rawMessageData.Delivery)
} else if rawMessageData.Optin != nil {
rawMessageData.Optin.Sender = rawMessageData.RawSender
messages = append(messages, rawMessageData.Optin)
} else if rawMessageData.Read != nil {
rawMessageData.Read.Sender = rawMessageData.RawSender
messages = append(messages, rawMessageData.Read)
} else if rawMessageData.CheckoutUpdate != nil {
rawMessageData.CheckoutUpdate.Sender = rawMessageData.RawSender
messages = append(messages, rawMessageData.CheckoutUpdate)
} else if rawMessageData.Payment != nil {
rawMessageData.Payment.Sender = rawMessageData.RawSender
messages = append(messages, rawMessageData.Payment)
} else {
logrus.WithFields(logrus.Fields{"rawMessageData": rawMessageData}).Error("Unknown message type")
}
}
}
return messages
}
func buildMessage(m rawMessageData) *Message {
var msg Message
msg.ID = m.RawMessage.RawMid
msg.Page = m.RawRecipient
msg.Sender = m.RawSender
msg.Text = m.RawMessage.RawText
msg.Seq = m.RawMessage.RawSeq
msg.Timestamp = m.RawTimestamp
msg.IsEcho = m.RawMessage.RawIsEcho
msg.AppID = m.RawMessage.RawAppID
msg.Quickreply = Quickreply{Payload: m.RawMessage.RawQuickreply.Payload}
for _, attachment := range m.RawMessage.RawAttachments {
switch attachment.RawType {
case "image":
image := Image{URL: attachment.RawPayload.RawURL, StickerID: attachment.RawPayload.StickerID}
msg.Images = append(msg.Images, image)
case "video":
video := Video{URL: attachment.RawPayload.RawURL}
msg.Videos = append(msg.Videos, video)
case "audio":
audio := Audio{URL: attachment.RawPayload.RawURL}
msg.Audios = append(msg.Audios, audio)
case "file":
file := File{URL: attachment.RawPayload.RawURL}
msg.Files = append(msg.Files, file)
case "location":
location := Location{
Coordinates: Coordinates{
Lat: attachment.RawPayload.RawCoordinates.RawLat,
Long: attachment.RawPayload.RawCoordinates.RawLong,
},
}
msg.Location = location
}
}
return &msg
}