/
messagequery.go
96 lines (81 loc) · 2.92 KB
/
messagequery.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
// ADDED BY DROP - https://github.com/matryer/drop (v0.6)
// source: github.com/maciekmm/messenger-platform-go-sdk (ca9227b956ad50bc8b6225a464f6c0146887f7c5)
// update: drop -f github.com/maciekmm/messenger-platform-go-sdk
// license: The MIT License (MIT) (see repo for details)
package main
import (
"errors"
)
type SendMessage struct {
Text string `json:"text,omitempty"`
Attachment *Attachment `json:"attachment,omitempty"`
}
// Recipient describes the person who will receive the message
// Either ID or PhoneNumber has to be set
type Recipient struct {
ID string `json:"id,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
}
// NotificationType describes the behavior phone will execute after receiving the message
type NotificationType string
const (
// NotificationTypeRegular will emit a sound/vibration and a phone notification
NotificationTypeRegular NotificationType = "REGULAR"
// NotificationTypeSilentPush will just emit a phone notification
NotificationTypeSilentPush NotificationType = "SILENT_PUSH"
// NotificationTypeNoPush will not emit sound/vibration nor a phone notification
NotificationTypeNoPush NotificationType = "NO_PUSH"
)
type MessageQuery struct {
Recipient Recipient `json:"recipient"`
Message SendMessage `json:"message"`
NotificationType NotificationType `json:"notification_type,omitempty"`
}
func (mq *MessageQuery) RecipientID(recipientID string) error {
if mq.Recipient.PhoneNumber != "" {
return errors.New("Only one user identification (phone or id) can be specified.")
}
mq.Recipient.ID = recipientID
return nil
}
func (mq *MessageQuery) RecipientPhoneNumber(phoneNumber string) error {
if mq.Recipient.ID != "" {
return errors.New("Only one user identification (phone or id) can be specified.")
}
mq.Recipient.PhoneNumber = phoneNumber
return nil
}
func (mq *MessageQuery) Notification(notification NotificationType) *MessageQuery {
mq.NotificationType = notification
return mq
}
func (mq *MessageQuery) Text(text string) error {
if mq.Message.Attachment == nil {
mq.Message.Attachment = &Attachment{}
}
if mq.Message.Attachment != nil && mq.Message.Attachment.Type == AttachmentTypeTemplate {
return errors.New("Can't set both text and template.")
}
mq.Message.Text = text
return nil
}
func (mq *MessageQuery) resource(typ AttachmentType, url string) error {
if mq.Message.Attachment == nil {
mq.Message.Attachment = &Attachment{}
}
if mq.Message.Attachment.Payload != nil {
return errors.New("Attachment already specified.")
}
mq.Message.Attachment.Type = typ
mq.Message.Attachment.Payload = &Resource{URL: url}
return nil
}
func (mq *MessageQuery) Audio(url string) error {
return mq.resource(AttachmentTypeAudio, url)
}
func (mq *MessageQuery) Video(url string) error {
return mq.resource(AttachmentTypeVideo, url)
}
func (mq *MessageQuery) Image(url string) error {
return mq.resource(AttachmentTypeImage, url)
}