-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage.go
309 lines (263 loc) · 7.49 KB
/
message.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
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
299
300
301
302
303
304
305
306
307
308
309
package instabot
import (
"encoding/json"
)
// Message type interface.
type Message interface {
Type() MessageType
}
// MessageType defines instagram message type.
type MessageType string
// all message type.
// https://developers.facebook.com/docs/messenger-platform/instagram/features/send-message#supported-messages
const (
MessageTypeText MessageType = MessageType("text")
MessageTypeImage MessageType = MessageType("image")
MessageTypeSticker MessageType = MessageType("sticker")
MessageTypeMediaShare MessageType = MessageType("media_share")
MessageTypeReacton MessageType = MessageType("reaction")
MessageTypeTemplate MessageType = MessageType("template")
)
// TextMessage defines text message.
type TextMessage struct {
messageType MessageType
Text string
quickReplyItems []*QuickReply
}
// TextMessageOption defines optional argument for new text message construction.
type TextMessageOption func(*TextMessage)
// WithQuickReplies adds quick reply items to text message.
// Quick reply is only supported with text message.
func WithQuickReplies(quickReplyItems []*QuickReply) TextMessageOption {
return func(m *TextMessage) {
m.quickReplyItems = quickReplyItems
}
}
// NewTextMessage returns a new text message.
func NewTextMessage(text string, options ...TextMessageOption) *TextMessage {
m := &TextMessage{
messageType: MessageTypeText,
Text: text,
}
for _, option := range options {
option(m)
}
return m
}
// AttachQuickReplies attach quick reply items to text message
func (m *TextMessage) AttachQuickReplies(quickReplies []*QuickReply) {
m.quickReplyItems = quickReplies
}
// Type returns message type.
func (m *TextMessage) Type() MessageType {
return m.messageType
}
// MarshalJSON returns json of the message.
func (m *TextMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Text string `json:"text"`
QuickReplyItems []*QuickReply `json:"quick_replies,omitempty"`
}{
Text: m.Text,
QuickReplyItems: m.quickReplyItems,
})
}
// ImageMessage defines image message.
type ImageMessage struct {
messageType MessageType
ImageURL string
}
// NewImageMessage returns a new image message.
func NewImageMessage(imageURL string) *ImageMessage {
return &ImageMessage{
messageType: MessageTypeImage,
ImageURL: imageURL,
}
}
// Type returns message type.
func (m *ImageMessage) Type() MessageType {
return m.messageType
}
// MarshalJSON returns json of the message.
func (m *ImageMessage) MarshalJSON() ([]byte, error) {
type Payload struct {
ImageURL string `json:"url"`
}
type Attachment struct {
Type string `json:"type"`
Payload *Payload `json:"payload"`
}
return json.Marshal(&struct {
Attachment *Attachment `json:"attachment"`
}{
Attachment: &Attachment{
Type: string(m.messageType),
Payload: &Payload{
ImageURL: m.ImageURL,
},
},
})
}
// StickerType defines sticker type.
type StickerType string
// all sticker type.
const (
StickerTypeHeart StickerType = StickerType("like_heart")
)
// StickerMessage defines sticker message.
type StickerMessage struct {
messageType MessageType
Sticker StickerType
}
// NewStickerMessage returns sticker message.
func NewStickerMessage(sticker StickerType) *StickerMessage {
return &StickerMessage{
messageType: MessageTypeSticker,
Sticker: sticker,
}
}
// Type returns message type.
func (m *StickerMessage) Type() MessageType {
return m.messageType
}
// MarshalJSON returns json of the message.
func (m *StickerMessage) MarshalJSON() ([]byte, error) {
type Attachment struct {
Type string `json:"type"`
}
return json.Marshal(&struct {
Attachment *Attachment `json:"attachment"`
}{
Attachment: &Attachment{
Type: string(m.Sticker),
},
})
}
// MediaShareMessage defines media share message.
type MediaShareMessage struct {
messageType MessageType
MediaID string
}
// NewMediaShareMessage returns new media share message.
func NewMediaShareMessage(mediaID string) *MediaShareMessage {
return &MediaShareMessage{
messageType: MessageTypeMediaShare,
MediaID: mediaID,
}
}
// Type returns message type.
func (m *MediaShareMessage) Type() MessageType {
return m.messageType
}
// MarshalJSON returns json of the message.
func (m *MediaShareMessage) MarshalJSON() ([]byte, error) {
type Payload struct {
MediaID string `json:"id"`
}
type Attachment struct {
Type string `json:"type"`
Payload *Payload `json:"payload"`
}
return json.Marshal(&struct {
Attachment *Attachment `json:"attachment"`
}{
Attachment: &Attachment{
Type: string(m.messageType),
Payload: &Payload{
MediaID: m.MediaID,
},
},
})
}
// GenericTemplateMessage defines generic template message.
// A generic template message could have maximum of 10 elements.
type GenericTemplateMessage struct {
messageType MessageType
templateType TemplateType
Elements []*GenericTemplateElement
}
// NewGenericTemplateMessage returns generic template element.
// A generic template message could have maximum of 10 elements.
func NewGenericTemplateMessage(elements []*GenericTemplateElement) *GenericTemplateMessage {
return &GenericTemplateMessage{
messageType: MessageTypeTemplate,
templateType: TemplateTypeGeneric,
Elements: elements,
}
}
// Type returns message type.
func (m *GenericTemplateMessage) Type() MessageType {
return m.messageType
}
// TemplateType returns template type.
func (m *GenericTemplateMessage) TemplateType() TemplateType {
return m.templateType
}
// MarshalJSON returns json of the message.
func (m *GenericTemplateMessage) MarshalJSON() ([]byte, error) {
type Payload struct {
TemplateType string `json:"template_type"`
Elements []*GenericTemplateElement `json:"elements"`
}
type Attachment struct {
Type string `json:"type"`
Payload *Payload `json:"payload"`
}
return json.Marshal(&struct {
Attachment *Attachment `json:"attachment"`
}{
Attachment: &Attachment{
Type: string(m.messageType),
Payload: &Payload{
TemplateType: string(m.templateType),
Elements: m.Elements,
},
},
})
}
// ProductTemplateMessage defines product template message.
// A product template message could have maximum of 10 elements.
type ProductTemplateMessage struct {
messageType MessageType
templateType TemplateType
Elements []*ProductTemplateElement
}
// NewProductTemplateMessage returns product template element.
// A product template message could have maximum of 10 elements.
func NewProductTemplateMessage(elements []*ProductTemplateElement) *ProductTemplateMessage {
return &ProductTemplateMessage{
messageType: MessageTypeTemplate,
templateType: TemplateTypeProduct,
Elements: elements,
}
}
// Type returns message type.
func (m *ProductTemplateMessage) Type() MessageType {
return m.messageType
}
// TemplateType returns template type.
func (m *ProductTemplateMessage) TemplateType() TemplateType {
return m.templateType
}
// MarshalJSON returns json of the message.
func (m *ProductTemplateMessage) MarshalJSON() ([]byte, error) {
type Payload struct {
TemplateType string `json:"template_type"`
Elements []*ProductTemplateElement `json:"elements"`
}
type Attachment struct {
Type string `json:"type"`
Payload *Payload `json:"payload"`
}
return json.Marshal(&struct {
Attachment *Attachment `json:"attachment"`
}{
Attachment: &Attachment{
Type: string(m.messageType),
Payload: &Payload{
TemplateType: string(m.templateType),
Elements: m.Elements,
},
},
})
}