/
message.go
214 lines (178 loc) · 6.07 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
// Copyright (C) 2016 Great Beyond AB - All Rights Reserved
// Written by David Högborg <d@greatbeyond.se>, 2016
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cellsynt
import (
"net/url"
"strings"
)
// Message is a generic interface to all types of messages
type Message interface {
Type() string
Destinations() string
GetParameters() map[string]string
}
type Options struct {
// Optional
OriginatorType OriginatorType
Originator string
}
func (b *Options) GetParameters() map[string]string {
if b == nil {
return map[string]string{}
}
params := map[string]string{
"originatortype": string(b.OriginatorType),
"originator": b.Originator,
}
return clearEmpty(params)
}
// Destination contains default values that all message types share.
// Theese values can be omitted if you want to use the client default
type Destination struct {
// Required
Recipients []string
// Optional, can be included in destination
DefaultCountryCode string
}
// Destination is the Destination address(es) formatted for cellsynt
func (b *Destination) Destinations() string {
if b == nil {
return ""
}
phones := []string{}
for _, phone := range b.Recipients {
if strings.HasPrefix(phone, "+") {
phone = "00" + strings.TrimPrefix(phone, "+")
} else if !strings.HasPrefix(phone, "00") {
phone = "00" + b.DefaultCountryCode + strings.TrimLeft(phone, "0")
}
phones = append(phones, phone)
}
return strings.Join(phones, ",")
}
func (b *Destination) GetParameters() map[string]string {
if b == nil {
return map[string]string{}
}
params := map[string]string{
"destination": b.Destinations(),
}
return clearEmpty(params)
}
// TextMessage is used to send a normal text message. Maximum number of characters is 160. Any characters
// specified within character coding GSM 03.38 can be used (e.g. English, Swedish, Norwegian), for
// other languages / alphabets (e.g. Arabic, Japanese, Chinese) please use Unicode.
type TextMessage struct {
// Required
Text string
// Optional
Charset Charset
AllowConcat bool
*Destination
*Options
}
// Type returns the message type
func (m *TextMessage) Type() string { return "text" }
// GetParameters implements Message interface
func (m *TextMessage) GetParameters() map[string]string {
params := map[string]string{
"type": m.Type(),
"text": url.QueryEscape(m.Text),
"charset": string(m.Charset),
"allowconcat": ternaryStr(m.AllowConcat, "6", ""),
}
params = mergeParams(params, m.Destination.GetParameters())
params = mergeParams(params, m.Options.GetParameters())
return clearEmpty(params)
}
// BinaryMessage can be used to send settings, bookmarks, visiting cards etc. See relevant phone
// manufacturer's manual for further specifications on message formats available.
// Two parameters are needed to send the binary data: udh and data.
// Parameters can be set individually, it is not mandatory to use both, however, at least one of them
type BinaryMessage struct {
Binary []byte
UDH []byte
*Destination
*Options
}
// Type returns the message type
func (m *BinaryMessage) Type() string { return "binary" }
// GetParameters implements Message interface
func (m *BinaryMessage) GetParameters() map[string]string {
params := map[string]string{
"type": m.Type(),
"data": string(m.Binary),
"udh": string(m.UDH),
}
params = mergeParams(params, m.Destination.GetParameters())
params = mergeParams(params, m.Options.GetParameters())
return clearEmpty(params)
}
// FlashMessage is used to send a normal text message which is shown directly on screen instead of being saved in
// the inbox on the recipient's mobile phone ("flash message").
// Please note! Support for flash messages can vary depending on mobile phone model, operator
// network and other external factors. If it is not possible to send a mess
type FlashMessage struct {
// Required
Text string
// Optional
Charset Charset
AllowConcat bool
*Destination
*Options
}
// Type returns the message type
func (m *FlashMessage) Type() string { return "flash" }
// GetParameters implements Message interface
func (m *FlashMessage) GetParameters() map[string]string {
params := map[string]string{
"type": m.Type(),
"text": url.QueryEscape(m.Text),
"charset": string(m.Charset),
"allowconcat": ternaryStr(m.AllowConcat, "6", ""),
}
params = mergeParams(params, m.Destination.GetParameters())
params = mergeParams(params, m.Options.GetParameters())
return clearEmpty(params)
}
// UnicodeMessage can be used if you need to send characters not available within an ordinary
// text message (e.g. Arabic, Japanese). A Unicode SMS can contain maximum 70 characters per
// message (or 67 characters per part for a long SMS).
// Unicode messages are sent by setting parameter type to unicode. When sending Unicode
// messages, the parameter charset can be set to UTF-8 to send data in the UTF-8 character set
// instead which holds characters of most of the world's languages.
type UnicodeMessage struct {
// Required
Text string
// Optional
Charset Charset
AllowConcat bool
*Destination
*Options
}
// Type returns the message type
func (m *UnicodeMessage) Type() string { return "unicode" }
// GetParameters implements Message interface
func (m *UnicodeMessage) GetParameters() map[string]string {
params := map[string]string{
"type": m.Type(),
"text": url.QueryEscape(m.Text),
"charset": string(m.Charset),
"allowconcat": ternaryStr(m.AllowConcat, "6", ""),
}
params = mergeParams(params, m.Destination.GetParameters())
params = mergeParams(params, m.Options.GetParameters())
return clearEmpty(params)
}