-
Notifications
You must be signed in to change notification settings - Fork 24
/
facebook.go
70 lines (61 loc) · 2.26 KB
/
facebook.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
package messenger
import "fmt"
// FacebookRequest received from Facebook server on webhook, contains messages, delivery reports and/or postbacks
type FacebookRequest struct {
Entry []struct {
ID int64 `json:"id"`
Messaging []struct {
Recipient struct {
ID int64 `json:"id,string"`
} `json:"recipient"`
Sender struct {
ID int64 `json:"id,string"`
} `json:"sender"`
Timestamp int `json:"timestamp"`
Message *FacebookMessage `json:"message,omitempty"`
Delivery *FacebookDelivery `json:"delivery"`
Postback *FacebookPostback `json:"postback"`
} `json:"messaging"`
Time int `json:"time"`
} `json:"entry"`
Object string `json:"object"`
}
// FacebookMessage struct for text messaged received from facebook server as part of FacebookRequest struct
type FacebookMessage struct {
Mid string `json:"mid"`
Seq int `json:"seq"`
Text string `json:"text"`
}
// FacebookDelivery struct for delivery reports received from Facebook server as part of FacebookRequest struct
type FacebookDelivery struct {
Mids []string `json:"mids"`
Seq int `json:"seq"`
Watermark int `json:"watermark"`
}
// FacebookPostback struct for postbacks received from Facebook server as part of FacebookRequest struct
type FacebookPostback struct {
Payload string `json:"payload"`
}
// rawFBResponse received from Facebook server after sending the message
// if Error is null we copy this into FacebookResponse object
type rawFBResponse struct {
MessageID string `json:"message_id"`
RecipientID int64 `json:"recipient_id,string"`
Error *FacebookError `json:"error"`
}
// FacebookResponse received from Facebook server after sending the message
type FacebookResponse struct {
MessageID string `json:"message_id"`
RecipientID int64 `json:"recipient_id,string"`
}
// FacebookError received form Facebook server if sending messages failed
type FacebookError struct {
Code int `json:"code"`
FbtraceID string `json:"fbtrace_id"`
Message string `json:"message"`
Type string `json:"type"`
}
// Error returns Go error object constructed from FacebookError data
func (err *FacebookError) Error() error {
return fmt.Errorf("FB Error: Type %s: %s; FB trace ID: %s", err.Type, err.Message, err.FbtraceID)
}