forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsglobal.go
68 lines (58 loc) · 1.44 KB
/
smsglobal.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
package main
import (
"errors"
"log"
"net/url"
"strings"
)
const (
SMSGLOBAL_API_URL = "http://www.smsglobal.com/http-api.php"
ErrSMSContactNotFound = "SMS Contact not found."
ErrSMSNotSent = "SMS message not sent."
)
func GetEnabledSMSContacts() int {
counter := 0
for _, contact := range bot.config.SMS.Contacts {
if contact.Enabled {
counter++
}
}
return counter
}
func SMSSendToAll(message string) {
for _, contact := range bot.config.SMS.Contacts {
if contact.Enabled {
err := SMSNotify(contact.Number, message)
if err != nil {
log.Printf("Unable to send SMS to %s.\n", contact.Name)
}
}
}
}
func SMSGetNumberByName(name string) string {
for _, contact := range bot.config.SMS.Contacts {
if contact.Name == name {
return contact.Number
}
}
return ErrSMSContactNotFound
}
func SMSNotify(to, message string) error {
values := url.Values{}
values.Set("action", "sendsms")
values.Set("user", bot.config.SMS.Username)
values.Set("password", bot.config.SMS.Password)
values.Set("from", bot.config.Name)
values.Set("to", to)
values.Set("text", message)
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := SendHTTPRequest("POST", SMSGLOBAL_API_URL, headers, strings.NewReader(values.Encode()))
if err != nil {
return err
}
if !StringContains(resp, "OK: 0; Sent queued message") {
return errors.New(ErrSMSNotSent)
}
return nil
}