-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
120 lines (104 loc) · 4.14 KB
/
messages.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
package messenger
// Message represents any form of message structure to post to Facebook
// https://developers.facebook.com/docs/messenger-platform/reference/send-api/#message
type Message interface{}
// Recipient represents a user to send a message to
type recipient struct {
ID int `json:"id"` // the recipients ID
}
// TextMessage is the structure that represents a text only message on messenger
type TextMessage struct {
MessagingType string `json:"messaging_type"`
Recipient recipient `json:"recipient"`
Message textMessageContent `json:"message"`
PersonaID string `json:"persona_id,omitempty"`
}
type textMessageContent struct {
Text string `json:"text"`
}
// GenericMessage struct used for sending structural messages to messenger (messages with images, links, and buttons)
type GenericMessage struct {
Recipient recipient `json:"recipient"`
Message genericMessageContent `json:"message"`
PersonaID string `json:"persona_id,omitempty"`
}
type genericMessageContent struct {
Text string `json:"text"`
Attachment *attachment `json:"attachment,omitempty"`
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
}
// QuickReply is a suggested reply offered to the user that can carry a payload
type QuickReply struct {
ContentType string `json:"content_type"`
Title string `json:"title,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Payload []byte `json:"payload,omitempty"`
}
type attachment struct {
Type string `json:"type,omitempty"`
Payload payload `json:"payload,omitempty"`
}
type payload struct {
TemplateType string `json:"template_type,omitempty"`
Text string `json:"text,omitempty"`
Buttons []Button `json:"buttons,omitempty"`
Elements []Element `json:"elements,omitempty"`
}
// Element in Generic Message template
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic
type Element struct {
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
ItemURL string `json:"item_url,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Buttons []Button `json:"buttons,omitempty"`
}
// Button on Generic Message template element
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic
type Button struct {
Type string `json:"type"`
URL string `json:"url,omitempty"`
Title string `json:"title"`
Payload string `json:"payload,omitempty"`
}
// Add support for:
// Quick replies, Typing & Personas
// NewTextMessage returns a new text message structure
func (c Client) NewTextMessage(recipientID int, text string) TextMessage {
return TextMessage{
MessagingType: "UPDATE",
Recipient: recipient{ID: recipientID},
Message: textMessageContent{Text: text},
}
}
// NewGenericMessage creates new Generic Template message that's used for attaching other elements such as images, links, buttons etc
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic
func (c Client) NewGenericMessage(recipientID int) GenericMessage {
return GenericMessage{
Recipient: recipient{ID: recipientID},
Message: genericMessageContent{
Attachment: &attachment{
Type: "template",
Payload: payload{TemplateType: "generic"},
},
},
}
}
// AddElement adds a new element to the message object
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic
func (m *GenericMessage) AddElement(e Element) {
m.Message.Attachment.Payload.Elements = append(m.Message.Attachment.Payload.Elements, e)
}
// AddQuickReply adds a new quick reply to the message object, for info about content types, see here:
// https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
func (m *GenericMessage) AddQuickReply(q QuickReply) {
m.Message.QuickReplies = append(m.Message.QuickReplies, q)
}
// AddPersona adds a persona ID to a message
func (m *GenericMessage) AddPersona(personaID string) {
m.PersonaID = personaID
}
// AddPersona adds a persona ID to a message
func (m *TextMessage) AddPersona(personaID string) {
m.PersonaID = personaID
}