-
Notifications
You must be signed in to change notification settings - Fork 10
/
receive.go
113 lines (96 loc) · 2.92 KB
/
receive.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
package mbotapi
// Payload received by the webhook
// The Object field is always set to `page`
// Contains bacthed entries
type Response struct {
Object string `json:"object"`
Entries []Entry `json:"entry"`
}
// This defines an Entry in the payload by webhook
type Entry struct {
PageID int64 `json:"id"`
Time int64 `json:"time"`
Messaging []Callback `json:"messaging"`
}
// This represents the content of the message sent by
// the user
// Various kinds of callbacks from user are -
// OptinCallback
// MessageCallback
// PostbackCallback
// DeliveryCallback
//
//TODO: Create a way to identify the type of callback
type Callback struct {
Sender User `json:"sender"`
Recipient Page `json:"recipient"`
Timestamp int64 `json:"timestamp"`
Optin InputOptin `json:"optin"`
Message InputMessage `json:"message,omitempty"`
Postback InputPostback `json:"postback,omitempty"`
Delivery InputDelivery `json:"delivery,omitempty"`
}
func (c Callback) IsMessage() bool {
return !(c.Message.Text == "" && len(c.Message.Attachments) == 0)
}
func (c Callback) IsOptin() bool {
return !(c.Optin == (InputOptin{}))
}
func (c Callback) IsPostback() bool {
return !(c.Postback == (InputPostback{}))
}
func (c Callback) IsDelivery() bool {
return !(len(c.Delivery.MIDs) == 0)
}
// This defines an user
// One of the fields will be set to identify the user
type User struct {
ID int64 `json:"id,omitempty,string"`
PhoneNumber string `json:"phone_number,omitempty"`
}
type Page struct {
ID int64 `json:"id,string"`
}
// Ref contains the `data-ref` set for message optin for the bot
type InputOptin struct {
Ref string `json:"ref"`
}
// This represents a Message from user
// If text message only Text field exists
// If media message Attachments fields contains an array of attachmensts sent
type InputMessage struct {
MID string `json:"mid"`
Seq int64 `json:"seq"`
Text string `json:"text"`
Attachments []InputAttachment `json:"attachments,omitempty"`
QuickReply InputQRPayload `json:"quick_reply,omitempty"`
}
//Represents a quick reply payload
type InputQRPayload struct {
Payload string `json:"payload"`
}
// Represents an attachement
// The types are image/audio/video/location
type InputAttachment struct {
Type string `json:"type"`
Payload InputAttachPayload `json:"payload"`
}
type InputAttachPayload struct {
URL string `json:"url,omitempty"`
Coords InputCoords `json:"coordinates,omitempty"`
}
type InputCoords struct {
Lat float64 `json:"lat"`
Long float64 `json:"long"`
}
// This contains delivery reports for batch
// of messages(mids)
type InputDelivery struct {
MIDs []string `json:"mids"`
Watermark int64 `json:"watermark"`
Seq int64 `json:"seq"`
}
// Represents a postback sent by clicking on Postback Button
type InputPostback struct {
Payload string `json:"payload"`
}