forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smsglobal.go
179 lines (155 loc) · 4.61 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
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
// Package smsglobal allows bulk messaging to a desired recipient list
package smsglobal
import (
"errors"
"flag"
"net/url"
"strings"
"github.com/extrame/gocryptotrader/common"
"github.com/extrame/gocryptotrader/communications/base"
"github.com/extrame/gocryptotrader/config"
)
const (
smsGlobalAPIURL = "https://www.smsglobal.com/http-api.php"
)
var (
errSMSNotSent = errors.New("SMSGlobal message not sent")
errContactNotFound = errors.New("SMSGlobal error contact not found")
)
// SMSGlobal is the overarching type across this package
type SMSGlobal struct {
base.Base
Contacts []Contact
Username string
Password string
SendFrom string
}
// Setup takes in a SMSGlobal configuration, sets username, password and
// and recipient list
func (s *SMSGlobal) Setup(config config.CommunicationsConfig) {
s.Name = config.SMSGlobalConfig.Name
s.Enabled = config.SMSGlobalConfig.Enabled
s.Verbose = config.SMSGlobalConfig.Verbose
s.Username = config.SMSGlobalConfig.Username
s.Password = config.SMSGlobalConfig.Password
var contacts []Contact
for x := range config.SMSGlobalConfig.Contacts {
contacts = append(contacts,
Contact{
Name: config.SMSGlobalConfig.Contacts[x].Name,
Number: config.SMSGlobalConfig.Contacts[x].Number,
Enabled: config.SMSGlobalConfig.Contacts[x].Enabled,
},
)
}
s.Contacts = contacts
}
// Connect connects to the service
func (s *SMSGlobal) Connect() error {
s.Connected = true
return nil
}
// PushEvent pushes an event to a contact list via SMS
func (s *SMSGlobal) PushEvent(base.Event) error {
return errors.New("not yet implemented")
}
// GetEnabledContacts returns how many SMS contacts are enabled in the
// contact list
func (s *SMSGlobal) GetEnabledContacts() int {
counter := 0
for x := range s.Contacts {
if s.Contacts[x].Enabled {
counter++
}
}
return counter
}
// GetContactByNumber returns a contact with supplied number
func (s *SMSGlobal) GetContactByNumber(number string) (Contact, error) {
for x := range s.Contacts {
if s.Contacts[x].Number == number {
return s.Contacts[x], nil
}
}
return Contact{}, errContactNotFound
}
// GetContactByName returns a contact with supplied name
func (s *SMSGlobal) GetContactByName(name string) (Contact, error) {
for x := range s.Contacts {
if common.StringToLower(s.Contacts[x].Name) == common.StringToLower(name) {
return s.Contacts[x], nil
}
}
return Contact{}, errContactNotFound
}
// AddContact checks to see if a contact exists and adds them if it doesn't
func (s *SMSGlobal) AddContact(contact Contact) error {
if contact.Name == "" || contact.Number == "" {
return errors.New("SMSGlobal AddContact() error - nothing to add")
}
if s.ContactExists(contact) {
return errors.New("SMSGlobal AddContact() error - contact already exists")
}
s.Contacts = append(s.Contacts, contact)
return nil
}
// ContactExists checks to see if a contact exists
func (s *SMSGlobal) ContactExists(contact Contact) bool {
for x := range s.Contacts {
if s.Contacts[x].Number == contact.Number && common.StringToLower(s.Contacts[x].Name) == common.StringToLower(contact.Name) {
return true
}
}
return false
}
// RemoveContact removes a contact if it exists
func (s *SMSGlobal) RemoveContact(contact Contact) error {
if !s.ContactExists(contact) {
return errors.New("SMSGlobal RemoveContact() error - contact does not exist")
}
for x := range s.Contacts {
if s.Contacts[x].Name == contact.Name && s.Contacts[x].Number == contact.Number {
s.Contacts = append(s.Contacts[:x], s.Contacts[x+1:]...)
return nil
}
}
return errors.New("SMSGlobal RemoveContact() error - contact already removed")
}
// SendMessageToAll sends a message to all enabled contacts in cfg
func (s *SMSGlobal) SendMessageToAll(message string) error {
for x := range s.Contacts {
if s.Contacts[x].Enabled {
err := s.SendMessage(s.Contacts[x].Number, message)
if err != nil {
return err
}
}
}
return nil
}
// SendMessage sends a message to an individual contact
func (s *SMSGlobal) SendMessage(to, message string) error {
if flag.Lookup("test.v") != nil {
return nil
}
values := url.Values{}
values.Set("action", "sendsms")
values.Set("user", s.Username)
values.Set("password", s.Password)
values.Set("from", s.SendFrom)
values.Set("to", to)
values.Set("text", message)
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := common.SendHTTPRequest("POST",
smsGlobalAPIURL,
headers,
strings.NewReader(values.Encode()))
if err != nil {
return err
}
if !common.StringContains(resp, "OK: 0; Sent queued message") {
return errSMSNotSent
}
return nil
}