-
Notifications
You must be signed in to change notification settings - Fork 34
/
message.go
184 lines (158 loc) · 4.83 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
package viber
import "encoding/json"
/*
{
"receiver": "01234567890A=",
"min_api_version": 1,
"sender": {
"name": "John McClane",
"avatar": "http://avatar.example.com"
},
"tracking_data": "tracking data",
"type": "text",
"text": "a message from pa"
}
*/
type messageResponse struct {
Status int `json:"status"`
StatusMessage string `json:"status_message"`
MessageToken uint64 `json:"message_token"`
}
// Message interface for all types of viber messages
type Message interface {
SetReceiver(r string)
SetFrom(from string)
SetKeyboard(k *Keyboard)
}
// TextMessage for Viber
type TextMessage struct {
Receiver string `json:"receiver,omitempty"`
From string `json:"from,omitempty"`
MinAPIVersion uint `json:"min_api_version,omitempty"`
Sender Sender `json:"sender"`
Type MessageType `json:"type"`
TrackingData string `json:"tracking_data,omitempty"`
Text string `json:"text"`
Keyboars *Keyboard `json:"keyboard,omitempty"`
// "media": "http://www.images.com/img.jpg",
// "thumbnail": "http://www.images.com/thumb.jpg"
// "size": 10000,
// "duration": 10
}
// URLMessage structure
type URLMessage struct {
TextMessage
Media string `json:"media"`
}
// PictureMessage structure
type PictureMessage struct {
TextMessage
Media string `json:"media"`
Thumbnail string `json:"thumbnail,omitempty"`
}
// VideoMessage structure
type VideoMessage struct {
TextMessage
Media string `json:"media"`
Thumbnail string `json:"thumbnail,omitempty"`
Size uint `json:"size"`
Duration uint `json:"duration,omitempty"`
}
// MessageType for viber messaging
type MessageType string
// Message types
const (
TypeTextMessage = MessageType("text")
TypeURLMessage = MessageType("url")
TypePictureMessage = MessageType("picture")
TypeVideoMessage = MessageType("video")
TypeFileMessage = MessageType("file")
TypeLocationMessage = MessageType("location")
TypeContactMessage = MessageType("contact")
TypeStickerMessage = MessageType("sticker")
TypeRichMediaMessage = MessageType("rich_media")
)
//video, file, location, contact, sticker, carousel content
func parseMsgResponse(b []byte) (msgToken uint64, err error) {
var resp messageResponse
if err := json.Unmarshal(b, &resp); err != nil {
return 0, err
}
if resp.Status != 0 {
return resp.MessageToken, Error{Status: resp.Status, StatusMessage: resp.StatusMessage}
}
return resp.MessageToken, nil
}
func (v *Viber) sendMessage(url string, m interface{}) (msgToken uint64, err error) {
b, err := v.PostData(url, m)
if err != nil {
return 0, err
}
return parseMsgResponse(b)
}
// NewTextMessage viber
func (v *Viber) NewTextMessage(msg string) *TextMessage {
return &TextMessage{
Sender: v.Sender,
Type: TypeTextMessage,
Text: msg,
}
}
// NewURLMessage creates new message with global sender and common params set
func (v *Viber) NewURLMessage(msg string, url string) *URLMessage {
return &URLMessage{
TextMessage: TextMessage{
Sender: v.Sender,
Type: TypeURLMessage,
Text: msg,
},
Media: url,
}
}
// NewPictureMessage for viber
func (v *Viber) NewPictureMessage(msg string, url string, thumbURL string) *PictureMessage {
return &PictureMessage{
TextMessage: TextMessage{
Sender: v.Sender,
Type: TypePictureMessage,
Text: msg,
},
Media: url,
Thumbnail: thumbURL,
}
}
// SendTextMessage to reciever, returns message token
func (v *Viber) SendTextMessage(receiver string, msg string) (msgToken uint64, err error) {
return v.SendMessage(receiver, v.NewTextMessage(msg))
}
// SendURLMessage to easily send url messages as global sender
func (v *Viber) SendURLMessage(receiver string, msg string, url string) (msgToken uint64, err error) {
return v.SendMessage(receiver, v.NewURLMessage(msg, url))
}
// SendPictureMessage to receiver, returns message token
func (v *Viber) SendPictureMessage(receiver string, msg string, url string, thumbURL string) (token uint64, err error) {
return v.SendMessage(receiver, v.NewPictureMessage(msg, url, thumbURL))
}
// SendPublicMessage from public account
func (v *Viber) SendPublicMessage(from string, m Message) (msgToken uint64, err error) {
// text, picture, video, file, location, contact, sticker and url
m.SetFrom(from)
return v.sendMessage("https://chatapi.viber.com/pa/post", m)
}
// SendMessage to receiver
func (v *Viber) SendMessage(to string, m Message) (msgToken uint64, err error) {
m.SetReceiver(to)
return v.sendMessage("https://chatapi.viber.com/pa/send_message", m)
}
// SetReceiver for text message
func (m *TextMessage) SetReceiver(r string) {
m.Receiver = r
}
// SetFrom to text message for public account message
func (m *TextMessage) SetFrom(from string) {
m.From = from
}
// SetKeyboard for text message
func (m *TextMessage) SetKeyboard(k *Keyboard) {
m.Keyboars = k
}