-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzenziva.go
More file actions
185 lines (149 loc) · 4.08 KB
/
Copy pathzenziva.go
File metadata and controls
185 lines (149 loc) · 4.08 KB
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
package zenziva
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/google/go-querystring/query"
)
// Zenziva model
type Zenziva struct {
Username string `json:"username"`
Password string `json:"password"`
Type string `json:"type"`
Subdomain string `json:"subdomain"`
Payload payload `json:"payload"`
}
type payload struct {
Username string `json:"username" url:"userkey"`
Password string `json:"password" url:"passkey"`
To string `json:"to" url:"nohp"`
Message string `json:"message" url:"pesan"`
OTP bool `json:"otp,omitempty" url:"-"`
}
type zenResponse map[string]interface{}
// Response API
type Response struct {
MessageID string `json:"message_id"`
To string `json:"to"`
Status bool `json:"status"`
Message string `json:"message"`
}
// static const
const (
SCHEME = "https"
DOMAIN = "zenziva.net"
)
var availableType []string = []string{"reguler", "masking", "sms_center", "whatsapp_reguler", "whatsapp_center"}
// To : set phone number
func (zen *Zenziva) To(phone string) *Zenziva {
zen.Payload.To = phone
return zen
}
// Message : set message
func (zen *Zenziva) Message(text string) *Zenziva {
zen.Payload.Message = text
return zen
}
// OTP : set message is otp or not
func (zen *Zenziva) OTP(status bool) *Zenziva {
zen.Payload.OTP = status
return zen
}
// Send : Used to send message
func (zen *Zenziva) Send() (res Response, err error) {
err = zen.initRequest()
if err != nil {
return
}
zen.assignConfig()
res, err = zen.requestAPI(zen.Payload)
if err != nil {
return
}
return
}
// SimpleSend : Used to send message with simpler method
func (zen *Zenziva) SimpleSend(to, message string) (res Response, err error) {
return zen.To(to).Message(message).Send()
}
// SimpleSendOTP : Used to send message with simpler method + otp
func (zen *Zenziva) SimpleSendOTP(to, message string, otp bool) (res Response, err error) {
return zen.To(to).Message(message).OTP(otp).Send()
}
func (zen *Zenziva) typeSMS(smsType string) {
zen.Type = smsType
}
func (zen *Zenziva) subdomain(subdomain string) {
zen.Subdomain = subdomain
}
func (zen *Zenziva) initRequest() error {
if zen.Username == "" {
return errors.New("Username is requried")
}
if zen.Password == "" {
return errors.New("Password is requried")
}
if zen.Type == "" {
zen.Type = "reguler"
}
if !inArray(zen.Type, availableType) {
return errors.New("Type/Package should one of (" + strings.Join(availableType, ", ") + ")")
}
if inArray(zen.Type, []string{"sms_center", "whatsapp_center"}) && zen.Subdomain == "" {
return errors.New("Subdomain is requried if type sms_center or whatsapp_center")
}
return nil
}
func (zen *Zenziva) assignConfig() *Zenziva {
zen.Payload.Username = zen.Username
zen.Payload.Password = zen.Password
return zen
}
func (zen *Zenziva) getSendSMSURL() string {
if inArray(zen.Type, []string{"reguler", "whatsapp_reguler"}) {
zen.Subdomain = "gsm"
} else if zen.Type == "masking" {
zen.Subdomain = "masking"
}
path := ""
if inArray(zen.Type, []string{"reguler", "masking", "sms_center"}) {
if zen.Payload.OTP {
path = "api/sendOTP/"
} else {
path = "api/sendsms/"
}
} else if zen.Type == "whatsapp_reguler" {
path = "api/sendWA/"
} else if zen.Type == "whatsapp_center" {
path = "api/WAsendMsg/"
}
url := SCHEME + "://" + zen.Subdomain + "." + DOMAIN + "/" + path
return url
}
func (zen *Zenziva) requestAPI(data payload) (resAPI Response, err error) {
url := zen.getSendSMSURL()
v, _ := query.Values(data)
payload := strings.NewReader(v.Encode())
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
zenRes := zenResponse{}
err = json.Unmarshal(body, &zenRes)
if err != nil {
return
}
resAPI = Response{
MessageID: zenRes["messageId"].(string),
To: zenRes["to"].(string),
Status: zenRes["status"].(string) == "1",
Message: zenRes["text"].(string),
}
return resAPI, nil
}