-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrio.go
64 lines (54 loc) · 1.51 KB
/
trio.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
/*
Package TrioUtil is a client for TrioMobile (http://trio-mobile.com) that utilises TrioMobile's API.
*/
package TrioUtil
import (
"net/http"
"net/url"
"strings"
)
// Trio represents the Trio Instance
type Trio struct {
token string
params url.Values
}
// New - creates a new instance of Trio
func New(token string) *Trio {
params := url.Values{}
params.Set("api_key", token)
params.Set("action", "send")
params.Set("sender_id", "CLOUDSMS")
params.Set("content_type", "1")
params.Set("mode", "shortcode")
return &Trio{
token,
params,
}
}
// SendSms - send SMS to TRIO service provider
func (t *Trio) SendSms(phone string, message string) (*http.Response, error) {
// gets all the predefined url parameters
url_parameters := t.params
// set SendSms func specific url parameters
url_parameters.Set("to", phone)
url_parameters.Set("msg", message)
// parse the url parameters into an endpoint
endpoint := getEndpoint(url_parameters)
// sends the POST request to TrioMobile
resp, err := http.Post(endpoint, "", nil)
return resp, err
}
// returns the endpoint after parsing url parameters
func getEndpoint(url_parameters url.Values) string {
var Url *url.URL
Url, err := url.Parse("http://cloudsms.trio-mobile.com/index.php/api/bulk_mt?")
if err != nil {
return ""
}
parameters := url_parameters
Url.RawQuery = parameters.Encode()
// replaces all occurences of '+' to '%20' since the encoder
// encodes spaces to '+' instead of '%20'
str := strings.Replace(Url.String(), "+", "%20", -1)
return str
}