-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosms.go
153 lines (126 loc) · 3.89 KB
/
gosms.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
/*
A package for sending out SMS. Currently works only with bulksms.com
but may support more services if the need should arise.
Author: Leon Szpilewski / http://github.com/jsz/gosms
*/
package gosms
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
type SMSSender interface {
Send(receivers []string, message string) error
}
//implements the bulksms.com http API
type BulkSMSSMSSender struct {
Username string // bulksms.com username
Password string // bulksms.com password
Testmode int // 0 - no testmode, 1 always succeed, -1 alswys fail
RoutingGroup int // 1 - economy, 2 - standard (default), 3 - premium. see bulksms.com for pricing
SenderId string // the alphanumeric sender id - get it in your bulksms.com control panel, max 11 chars. this is CaseSensitive! MyId != Myid, won't work with econmoy (usually)
}
func NewBulkSMSSMSSender(username, password string) *BulkSMSSMSSender {
return &BulkSMSSMSSender{
Username: username,
Password: password,
Testmode: 0,
RoutingGroup: 2,
SenderId: "",
}
}
//get the total price (in CREDITS! NOT MONEYS!) for sending out a given message to a list of receivers.
//for credit prices see bulksms.com
func (sms *BulkSMSSMSSender) GetQuote(receivers []string, message string) (err error, quote float64) {
if sms.RoutingGroup < 1 || sms.RoutingGroup > 3 {
err = errors.New("Routing Group must be 1, 2 or 3!")
return
}
rtgrp := strconv.Itoa(sms.RoutingGroup)
endpoint_url := "http://bulksms.vsms.net:5567/eapi/submission/quote_sms/2/2.0"
rcvrs := strings.Join(receivers, ",")
data := url.Values{
"username": []string{sms.Username},
"password": []string{sms.Password},
"message": []string{message},
"msisdn": []string{rcvrs},
"routing_group": []string{rtgrp},
}
if len(sms.SenderId) > 0 {
data.Set("sender", sms.SenderId)
}
c := &http.Client{}
resp, err := c.PostForm(endpoint_url, data)
if err != nil {
return err, 0.0
}
respbytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err, 0.0
}
resp.Body.Close()
respstr := strings.Trim(string(respbytes[0:]), "\r\n")
respitems := strings.Split(respstr, "|")
respcode, err := strconv.ParseInt(respitems[0], 10, 64)
if err != nil {
return err, 0.0
}
if respcode != 0 {
return errors.New(fmt.Sprintf("err code %d: %s", respcode, respitems[1])), 0.0
}
quote, err = strconv.ParseFloat(respitems[2], 64)
return
}
//sends a message to a list of receivers.
//a receiver is a string containing an international telephone number
//without a leading + or 0. to send a sms to a german (+49) number
//you'd use "49172xxxxxx".
//the message strings max len is 160 chars
func (sms *BulkSMSSMSSender) Send(receivers []string, message string) error {
if sms.RoutingGroup < 1 || sms.RoutingGroup > 3 {
return errors.New("Routing Group must be 1, 2 or 3!")
}
rtgrp := strconv.Itoa(sms.RoutingGroup)
endpoint_url := "http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0"
rcvrs := strings.Join(receivers, ",")
data := url.Values{
"username": []string{sms.Username},
"password": []string{sms.Password},
"message": []string{message},
"msisdn": []string{rcvrs},
"routing_group": []string{rtgrp},
}
if len(sms.SenderId) > 0 {
data.Set("sender", sms.SenderId)
}
if sms.Testmode == -1 {
data["test_always_fail"] = []string{"1"}
}
if sms.Testmode == 1 {
data["test_always_succeed"] = []string{"1"}
}
c := &http.Client{}
resp, err := c.PostForm(endpoint_url, data)
if err != nil {
return err
}
respbytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
resp.Body.Close()
respstr := string(respbytes[0:])
respitems := strings.Split(respstr, "|")
respcode, err := strconv.ParseInt(respitems[0], 10, 64)
if err != nil {
return err
}
if respcode != 0 {
return errors.New(fmt.Sprintf("err code %d: %s", respcode, respitems[1]))
}
return nil
}