mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Improved SMS handling code.
This commit is contained in:
62
config.go
62
config.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -13,26 +14,32 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrExchangeNameEmpty = "Exchange #%d in config: Exchange name is empty."
|
||||
ErrExchangeAvailablePairsEmpty = "Exchange %s: Available pairs is empty."
|
||||
ErrExchangeEnabledPairsEmpty = "Exchange %s: Enabled pairs is empty."
|
||||
ErrExchangeBaseCurrenciesEmpty = "Exchange %s: Base currencies is empty."
|
||||
ErrExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
|
||||
ErrExchangeNotFound = "Exchange %s: Not found."
|
||||
ErrExchangeNameEmpty = "Exchange #%d in config: Exchange name is empty."
|
||||
ErrExchangeAvailablePairsEmpty = "Exchange %s: Available pairs is empty."
|
||||
ErrExchangeEnabledPairsEmpty = "Exchange %s: Enabled pairs is empty."
|
||||
ErrExchangeBaseCurrenciesEmpty = "Exchange %s: Base currencies is empty."
|
||||
ErrExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
|
||||
ErrExchangeNotFound = "Exchange %s: Not found."
|
||||
WarningSMSGlobalDefaultOrEmptyValues = "WARNING -- SMS Support disabled due to default or empty Username/Password values."
|
||||
WarningSSMSGlobalSMSContactDefaultOrEmptyValues = "WARNING -- SMS contact #%d Name/Number disabled due to default or empty values."
|
||||
WarningSSMSGlobalSMSNoContacts = "WARNING -- SMS Support disabled due to no enabled contacts."
|
||||
)
|
||||
|
||||
type SMSContacts struct {
|
||||
Name string
|
||||
Number string
|
||||
Enabled bool
|
||||
type SMSGlobal struct {
|
||||
Enabled bool
|
||||
Username string
|
||||
Password string
|
||||
Contacts []struct {
|
||||
Name string
|
||||
Number string
|
||||
Enabled bool
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Name string
|
||||
SMSGlobalUsername string
|
||||
SMSGlobalPassword string
|
||||
SMSContacts []SMSContacts
|
||||
Exchanges []Exchanges
|
||||
Name string
|
||||
SMS SMSGlobal `json:"SMSGlobal"`
|
||||
Exchanges []Exchanges
|
||||
}
|
||||
|
||||
type Exchanges struct {
|
||||
@@ -69,6 +76,31 @@ func UpdateExchangeConfig(e Exchanges) error {
|
||||
return fmt.Errorf(ErrExchangeNotFound, e.Name)
|
||||
}
|
||||
|
||||
func CheckSMSGlobalConfigValues() error {
|
||||
if bot.config.SMS.Enabled {
|
||||
if bot.config.SMS.Username == "" || bot.config.SMS.Username == "Username" || bot.config.SMS.Password == "" || bot.config.SMS.Password == "Password" {
|
||||
bot.config.SMS.Enabled = false
|
||||
return errors.New(WarningSMSGlobalDefaultOrEmptyValues)
|
||||
}
|
||||
contacts := 0
|
||||
for i := range bot.config.SMS.Contacts {
|
||||
if bot.config.SMS.Contacts[i].Enabled {
|
||||
if bot.config.SMS.Contacts[i].Name == "" || bot.config.SMS.Contacts[i].Number == "" || (bot.config.SMS.Contacts[i].Name == "Bob" && bot.config.SMS.Contacts[i].Number == "12345") {
|
||||
log.Printf(WarningSSMSGlobalSMSContactDefaultOrEmptyValues, i)
|
||||
bot.config.SMS.Contacts[i].Enabled = false
|
||||
continue
|
||||
}
|
||||
contacts++
|
||||
}
|
||||
}
|
||||
if contacts == 0 {
|
||||
bot.config.SMS.Enabled = false
|
||||
return errors.New(WarningSSMSGlobalSMSNoContacts)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckConfigValues() error {
|
||||
for i, exch := range bot.config.Exchanges {
|
||||
if exch.Enabled {
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
{
|
||||
"Name": "Skynet",
|
||||
"SMSGlobalUsername": "Username",
|
||||
"SMSGlobalPassword": "Password",
|
||||
"SMSContacts": [
|
||||
{
|
||||
"Name": "Bob",
|
||||
"Number": "12345",
|
||||
"Enabled": false
|
||||
}
|
||||
],
|
||||
"SMSGlobal": {
|
||||
"Enabled": false,
|
||||
"Username": "Username",
|
||||
"Password": "Password",
|
||||
"Contacts": [
|
||||
{
|
||||
"Name": "Bob",
|
||||
"Number": "12345",
|
||||
"Enabled": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Exchanges": [
|
||||
{
|
||||
"Name": "ANX",
|
||||
|
||||
@@ -221,7 +221,7 @@ func IsValidEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Actio
|
||||
return ErrInvalidAction
|
||||
}
|
||||
|
||||
if action[1] != "ALL" && SMSGetNumberByName(action[1]) == "" {
|
||||
if action[1] != "ALL" && SMSGetNumberByName(action[1]) == ErrSMSContactNotFound {
|
||||
return ErrInvalidAction
|
||||
}
|
||||
} else {
|
||||
|
||||
28
main.go
28
main.go
@@ -56,9 +56,21 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
err = CheckSMSGlobalConfigValues()
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
log.Println("Config file loaded.")
|
||||
log.Printf("Bot '%s' started.\n", bot.config.Name)
|
||||
|
||||
if bot.config.SMS.Enabled {
|
||||
log.Printf("SMS support enabled. Number of SMS contacts %d.\n", GetEnabledSMSContacts())
|
||||
} else {
|
||||
log.Println("SMS support disabled.")
|
||||
}
|
||||
|
||||
enabledExchanges := 0
|
||||
for _, exch := range bot.config.Exchanges {
|
||||
if exch.Enabled {
|
||||
@@ -73,22 +85,6 @@ func main() {
|
||||
|
||||
AdjustGoMaxProcs()
|
||||
|
||||
smsSupport := false
|
||||
smsContacts := 0
|
||||
|
||||
for _, sms := range bot.config.SMSContacts {
|
||||
if sms.Enabled {
|
||||
smsSupport = true
|
||||
smsContacts++
|
||||
}
|
||||
}
|
||||
|
||||
if smsSupport {
|
||||
log.Printf("SMS support enabled. Number of SMS contacts %d.\n", smsContacts)
|
||||
} else {
|
||||
log.Println("SMS support disabled.")
|
||||
}
|
||||
|
||||
log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), enabledExchanges)
|
||||
log.Println("Bot Exchange support:")
|
||||
|
||||
|
||||
40
smsglobal.go
40
smsglobal.go
@@ -1,41 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/url"
|
||||
"strings"
|
||||
"log"
|
||||
)
|
||||
|
||||
const (
|
||||
SMSGLOBAL_API_URL = "http://www.smsglobal.com/http-api.php"
|
||||
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.SMSContacts {
|
||||
for _, contact := range bot.config.SMS.Contacts {
|
||||
if contact.Enabled {
|
||||
err := SMSNotify(contact.Number, message)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Printf("Unable to send SMS to %s.\n", contact.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SMSGetNumberByName(name string) (string) {
|
||||
for _, contact := range bot.config.SMSContacts {
|
||||
func SMSGetNumberByName(name string) string {
|
||||
for _, contact := range bot.config.SMS.Contacts {
|
||||
if contact.Name == name {
|
||||
return contact.Number
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return ErrSMSContactNotFound
|
||||
}
|
||||
|
||||
func SMSNotify(to, message string) (error) {
|
||||
func SMSNotify(to, message string) error {
|
||||
values := url.Values{}
|
||||
values.Set("action", "sendsms")
|
||||
values.Set("user", bot.config.SMSGlobalUsername)
|
||||
values.Set("password", bot.config.SMSGlobalPassword)
|
||||
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)
|
||||
@@ -49,6 +61,8 @@ func SMSNotify(to, message string) (error) {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Recieved raw: %s\n", resp)
|
||||
if !StringContains(resp, "OK: 0; Sent queued message") {
|
||||
return errors.New(ErrSMSNotSent)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user