mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 23:16:53 +00:00
Engine improvements
This commit is contained in:
@@ -1,56 +1,15 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/assets"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
// global vars contain staged update data that will be sent to the communication
|
||||
// mediums
|
||||
var (
|
||||
TickerStaged map[string]map[assets.AssetType]map[string]ticker.Price
|
||||
OrderbookStaged map[string]map[assets.AssetType]map[string]Orderbook
|
||||
PortfolioStaged Portfolio
|
||||
SettingsStaged Settings
|
||||
ServiceStarted time.Time
|
||||
m sync.Mutex
|
||||
ServiceStarted time.Time
|
||||
)
|
||||
|
||||
// Orderbook holds the minimal orderbook details to be sent to a communication
|
||||
// medium
|
||||
type Orderbook struct {
|
||||
CurrencyPair string
|
||||
AssetType string
|
||||
TotalAsks float64
|
||||
TotalBids float64
|
||||
LastUpdated string
|
||||
}
|
||||
|
||||
// Ticker holds the minimal orderbook details to be sent to a communication
|
||||
// medium
|
||||
type Ticker struct {
|
||||
CurrencyPair string
|
||||
LastUpdated string
|
||||
}
|
||||
|
||||
// Portfolio holds the minimal portfolio details to be sent to a communication
|
||||
// medium
|
||||
type Portfolio struct {
|
||||
ProfitLoss string
|
||||
}
|
||||
|
||||
// Settings holds the minimal setting details to be sent to a communication
|
||||
// medium
|
||||
type Settings struct {
|
||||
EnabledExchanges string
|
||||
EnabledCommunications string
|
||||
}
|
||||
|
||||
// Base enforces standard variables across communication packages
|
||||
type Base struct {
|
||||
Name string
|
||||
@@ -61,9 +20,8 @@ type Base struct {
|
||||
|
||||
// Event is a generalise event type
|
||||
type Event struct {
|
||||
Type string
|
||||
GainLoss string
|
||||
TradeDetails string
|
||||
Type string
|
||||
Message string
|
||||
}
|
||||
|
||||
// IsEnabled returns if the comms package has been enabled in the configuration
|
||||
@@ -82,83 +40,6 @@ func (b *Base) GetName() string {
|
||||
return b.Name
|
||||
}
|
||||
|
||||
// GetTicker returns staged ticker data
|
||||
func (b *Base) GetTicker(exchangeName string) string {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
tickerPrice, ok := TickerStaged[exchangeName]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
var tickerPrices []ticker.Price
|
||||
for x := range tickerPrice {
|
||||
for y := range tickerPrice[x] {
|
||||
tickerPrices = append(tickerPrices, tickerPrice[x][y])
|
||||
}
|
||||
}
|
||||
|
||||
var packagedTickers []string
|
||||
for i := range tickerPrices {
|
||||
packagedTickers = append(packagedTickers, fmt.Sprintf(
|
||||
"Currency Pair: %s Ask: %f, Bid: %f High: %f Last: %f Low: %f ATH: %f Volume: %f",
|
||||
tickerPrices[i].Pair,
|
||||
tickerPrices[i].Ask,
|
||||
tickerPrices[i].Bid,
|
||||
tickerPrices[i].High,
|
||||
tickerPrices[i].Last,
|
||||
tickerPrices[i].Low,
|
||||
tickerPrices[i].PriceATH,
|
||||
tickerPrices[i].Volume))
|
||||
}
|
||||
return strings.Join(packagedTickers, "\n")
|
||||
}
|
||||
|
||||
// GetOrderbook returns staged orderbook data
|
||||
func (b *Base) GetOrderbook(exchangeName string) string {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
orderbook, ok := OrderbookStaged[exchangeName]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
var orderbooks []Orderbook
|
||||
for _, x := range orderbook {
|
||||
for _, y := range x {
|
||||
orderbooks = append(orderbooks, y)
|
||||
}
|
||||
}
|
||||
|
||||
var packagedOrderbooks []string
|
||||
for i := range orderbooks {
|
||||
packagedOrderbooks = append(packagedOrderbooks, fmt.Sprintf(
|
||||
"Currency Pair: %s AssetType: %s, LastUpdated: %s TotalAsks: %f TotalBids: %f",
|
||||
orderbooks[i].CurrencyPair,
|
||||
orderbooks[i].AssetType,
|
||||
orderbooks[i].LastUpdated,
|
||||
orderbooks[i].TotalAsks,
|
||||
orderbooks[i].TotalBids))
|
||||
}
|
||||
return strings.Join(packagedOrderbooks, "\n")
|
||||
}
|
||||
|
||||
// GetPortfolio returns staged portfolio info
|
||||
func (b *Base) GetPortfolio() string {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
return fmt.Sprintf("%v", PortfolioStaged)
|
||||
}
|
||||
|
||||
// GetSettings returns stage setting info
|
||||
func (b *Base) GetSettings() string {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
return fmt.Sprintf("%v", SettingsStaged)
|
||||
}
|
||||
|
||||
// GetStatus returns status data
|
||||
func (b *Base) GetStatus() string {
|
||||
return `
|
||||
|
||||
@@ -4,9 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/assets"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
)
|
||||
|
||||
@@ -26,10 +23,7 @@ type ICommunicate interface {
|
||||
// Setup sets up communication variables and intiates a connection to the
|
||||
// communication mediums
|
||||
func (c IComm) Setup() {
|
||||
TickerStaged = make(map[string]map[assets.AssetType]map[string]ticker.Price)
|
||||
OrderbookStaged = make(map[string]map[assets.AssetType]map[string]Orderbook)
|
||||
ServiceStarted = time.Now()
|
||||
|
||||
for i := range c {
|
||||
if c[i].IsEnabled() && !c[i].IsConnected() {
|
||||
err := c[i].Connect()
|
||||
@@ -46,8 +40,8 @@ func (c IComm) PushEvent(event Event) {
|
||||
if c[i].IsEnabled() && c[i].IsConnected() {
|
||||
err := c[i].PushEvent(event)
|
||||
if err != nil {
|
||||
log.Errorf("Communications error - PushEvent() in package %s with %v",
|
||||
c[i].GetName(), event)
|
||||
log.Errorf("Communications error - PushEvent() in package %s with %v. Err %s",
|
||||
c[i].GetName(), event, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,42 +62,3 @@ func (c IComm) GetEnabledCommunicationMediums() {
|
||||
log.Warnf("Communications: No communication mediums are enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
// StageTickerData stages updated ticker data for the communications package
|
||||
func (c IComm) StageTickerData(exchangeName string, assetType assets.AssetType, tickerPrice *ticker.Price) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
if _, ok := TickerStaged[exchangeName]; !ok {
|
||||
TickerStaged[exchangeName] = make(map[assets.AssetType]map[string]ticker.Price)
|
||||
}
|
||||
|
||||
if _, ok := TickerStaged[exchangeName][assetType]; !ok {
|
||||
TickerStaged[exchangeName][assetType] = make(map[string]ticker.Price)
|
||||
}
|
||||
|
||||
TickerStaged[exchangeName][assetType][tickerPrice.Pair.String()] = *tickerPrice
|
||||
}
|
||||
|
||||
// StageOrderbookData stages updated orderbook data for the communications
|
||||
// package
|
||||
func (c IComm) StageOrderbookData(exchangeName string, assetType assets.AssetType, ob *orderbook.Base) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
if _, ok := OrderbookStaged[exchangeName]; !ok {
|
||||
OrderbookStaged[exchangeName] = make(map[assets.AssetType]map[string]Orderbook)
|
||||
}
|
||||
|
||||
if _, ok := OrderbookStaged[exchangeName][assetType]; !ok {
|
||||
OrderbookStaged[exchangeName][assetType] = make(map[string]Orderbook)
|
||||
}
|
||||
|
||||
_, totalAsks := ob.TotalAsksAmount()
|
||||
_, totalBids := ob.TotalBidsAmount()
|
||||
|
||||
OrderbookStaged[exchangeName][assetType][ob.Pair.String()] = Orderbook{
|
||||
CurrencyPair: ob.Pair.String(),
|
||||
TotalAsks: totalAsks,
|
||||
TotalBids: totalBids}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,10 @@ package base
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
var (
|
||||
b Base
|
||||
i IComm
|
||||
)
|
||||
|
||||
func TestStart(t *testing.T) {
|
||||
@@ -39,41 +35,6 @@ func TestGetName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTicker(t *testing.T) {
|
||||
v := b.GetTicker("ANX")
|
||||
if v != "" {
|
||||
t.Error("test failed - base GetTicker() error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrderbook(t *testing.T) {
|
||||
v := b.GetOrderbook("ANX")
|
||||
if v != "" {
|
||||
t.Error("test failed - base GetOrderbook() error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortfolio(t *testing.T) {
|
||||
v := b.GetPortfolio()
|
||||
if v != "{}" {
|
||||
t.Error("test failed - base GetPortfolio() error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSettings(t *testing.T) {
|
||||
v := b.GetSettings()
|
||||
if v != "{ }" {
|
||||
t.Error("test failed - base GetSettings() error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStatus(t *testing.T) {
|
||||
v := b.GetStatus()
|
||||
if v == "" {
|
||||
t.Error("test failed - base GetStatus() error")
|
||||
}
|
||||
}
|
||||
|
||||
type CommunicationProvider struct {
|
||||
ICommunicate
|
||||
|
||||
@@ -166,58 +127,3 @@ func TestPushEvent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStageTickerData(t *testing.T) {
|
||||
_, ok := TickerStaged["bitstamp"]["someAsset"]["BTCUSD"]
|
||||
if ok {
|
||||
t.Fatalf("key should not exists")
|
||||
}
|
||||
|
||||
price := ticker.Price{}
|
||||
var i IComm
|
||||
i.Setup()
|
||||
|
||||
i.StageTickerData("bitstamp", "someAsset", &price)
|
||||
|
||||
_, ok = TickerStaged["bitstamp"]["someAsset"][price.Pair.String()]
|
||||
if !ok {
|
||||
t.Fatalf("key should exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderbookData(t *testing.T) {
|
||||
_, ok := OrderbookStaged["bitstamp"]["someAsset"]["someOrderbook"]
|
||||
if ok {
|
||||
t.Fatal("key should not exists")
|
||||
}
|
||||
|
||||
ob := orderbook.Base{
|
||||
Asks: []orderbook.Item{
|
||||
{Amount: 1, Price: 2, ID: 3},
|
||||
{Amount: 4, Price: 5, ID: 6},
|
||||
},
|
||||
}
|
||||
var i IComm
|
||||
i.Setup()
|
||||
|
||||
i.StageOrderbookData("bitstamp", "someAsset", &ob)
|
||||
|
||||
orderbook, ok := OrderbookStaged["bitstamp"]["someAsset"][ob.Pair.String()]
|
||||
if !ok {
|
||||
t.Fatal("key should exists")
|
||||
}
|
||||
|
||||
if ob.Pair.String() != orderbook.CurrencyPair {
|
||||
t.Fatal("currency missmatched")
|
||||
}
|
||||
|
||||
_, totalAsks := ob.TotalAsksAmount()
|
||||
if totalAsks != orderbook.TotalAsks {
|
||||
t.Fatal("total asks missmatched")
|
||||
}
|
||||
|
||||
_, totalBids := ob.TotalBidsAmount()
|
||||
if totalBids != orderbook.TotalBids {
|
||||
t.Fatal("total bids missmatched")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,21 +24,13 @@ import (
|
||||
const (
|
||||
SlackURL = "https://slack.com/api/rtm.start"
|
||||
|
||||
cmdStatus = "!status"
|
||||
cmdHelp = "!help"
|
||||
cmdSettings = "!settings"
|
||||
cmdTicker = "!ticker"
|
||||
cmdPortfolio = "!portfolio"
|
||||
cmdOrderbook = "!orderbook"
|
||||
cmdStatus = "!status"
|
||||
cmdHelp = "!help"
|
||||
|
||||
getHelp = `GoCryptoTrader SlackBot, thank you for using this service!
|
||||
Current commands are:
|
||||
!status - Displays current working status of bot
|
||||
!help - Displays help text
|
||||
!settings - Displays current settings
|
||||
!ticker - Displays recent ANX ticker
|
||||
!portfolio - Displays portfolio data
|
||||
!orderbook - Displays current ANX orderbook`
|
||||
!help - Displays help text`
|
||||
)
|
||||
|
||||
// Slack starts a websocket connection and uses https://api.slack.com/rtm real
|
||||
@@ -58,6 +50,11 @@ type Slack struct {
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// IsConnected returns whether or not the connection is connected
|
||||
func (s *Slack) IsConnected() bool {
|
||||
return s.Connected
|
||||
}
|
||||
|
||||
// Setup takes in a slack configuration, sets bots target channel and
|
||||
// sets verification token to access workspace
|
||||
func (s *Slack) Setup(cfg *config.CommunicationsConfig) {
|
||||
@@ -70,12 +67,22 @@ func (s *Slack) Setup(cfg *config.CommunicationsConfig) {
|
||||
|
||||
// Connect connects to the service
|
||||
func (s *Slack) Connect() error {
|
||||
return s.NewConnection()
|
||||
err := s.NewConnection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Connected = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// PushEvent pushes an event to either a slack channel or specific client
|
||||
func (s *Slack) PushEvent(base.Event) error {
|
||||
return common.ErrNotYetImplemented
|
||||
func (s *Slack) PushEvent(event base.Event) error {
|
||||
if s.Connected {
|
||||
return s.WebsocketSend("message",
|
||||
fmt.Sprintf("event: %s %s", event.Type, event.Message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildURL returns an appended token string with the SlackURL
|
||||
@@ -155,19 +162,22 @@ func (s *Slack) NewConnection() error {
|
||||
}
|
||||
|
||||
if s.Verbose {
|
||||
log.Debugf("%s [%s] connected to %s [%s] \nWebsocket URL: %s.\n",
|
||||
log.Debugf("Slack: %s [%s] connected to %s [%s] \nWebsocket URL: %s.\n",
|
||||
s.Details.Self.Name,
|
||||
s.Details.Self.ID,
|
||||
s.Details.Team.Domain,
|
||||
s.Details.Team.ID,
|
||||
s.Details.URL)
|
||||
log.Debugf("Slack channels: %s", s.GetChannelsString())
|
||||
log.Debugf("Slack: Public channels: %s", s.GetChannelsString())
|
||||
}
|
||||
|
||||
s.TargetChannelID, err = s.GetIDByName(s.TargetChannel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("Slack: Target channel ID: %v [#%v]", s.TargetChannelID,
|
||||
s.TargetChannel)
|
||||
return s.WebsocketConnect()
|
||||
}
|
||||
return errors.New("slack.go NewConnection() Already Connected")
|
||||
@@ -202,7 +212,6 @@ func (s *Slack) WebsocketReader() {
|
||||
}
|
||||
|
||||
var data WebsocketResponse
|
||||
|
||||
err = common.JSONDecode(resp, &data)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -240,7 +249,7 @@ func (s *Slack) WebsocketReader() {
|
||||
|
||||
case "pong":
|
||||
if s.Verbose {
|
||||
log.Debugf("Pong received from server")
|
||||
log.Debugf("Slack: Pong received from server")
|
||||
}
|
||||
default:
|
||||
log.Debugf(string(resp))
|
||||
@@ -255,7 +264,7 @@ func (s *Slack) handlePresenceChange(resp []byte) error {
|
||||
return err
|
||||
}
|
||||
if s.Verbose {
|
||||
log.Debugf("Presence change. User %s [%s] changed status to %s\n",
|
||||
log.Debugf("Slack: Presence change. User %s [%s] changed status to %s\n",
|
||||
s.GetUsernameByID(pres.User),
|
||||
pres.User, pres.Presence)
|
||||
}
|
||||
@@ -272,7 +281,7 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
|
||||
return err
|
||||
}
|
||||
if s.Verbose {
|
||||
log.Debugf("Msg received by %s [%s] with text: %s\n",
|
||||
log.Debugf("Slack: Message received by %s [%s] with text: %s\n",
|
||||
s.GetUsernameByID(msg.User),
|
||||
msg.User, msg.Text)
|
||||
}
|
||||
@@ -304,7 +313,7 @@ func (s *Slack) handleErrorResponse(data WebsocketResponse) error {
|
||||
|
||||
func (s *Slack) handleHelloResponse() {
|
||||
if s.Verbose {
|
||||
log.Debugln("Websocket connected successfully.")
|
||||
log.Debugln("Slack: Websocket connected successfully.")
|
||||
}
|
||||
s.Connected = true
|
||||
go s.WebsocketKeepAlive()
|
||||
@@ -321,7 +330,7 @@ func (s *Slack) handleReconnectResponse(resp []byte) error {
|
||||
}
|
||||
s.ReconnectURL = recURL.URL
|
||||
if s.Verbose {
|
||||
log.Debugf("Reconnect URL set to %s\n", s.ReconnectURL)
|
||||
log.Debugf("Slack: Reconnect URL set to %s\n", s.ReconnectURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -334,7 +343,7 @@ func (s *Slack) WebsocketKeepAlive() {
|
||||
for {
|
||||
<-ticker.C
|
||||
if err := s.WebsocketSend("ping", ""); err != nil {
|
||||
log.Debugf("slack WebsocketKeepAlive() error %s", err)
|
||||
log.Debugf("Slack: WebsocketKeepAlive() error %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -353,6 +362,11 @@ func (s *Slack) WebsocketSend(eventType, text string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.Verbose {
|
||||
log.Debugf("Slack: Sending websocket message: %s", string(data))
|
||||
}
|
||||
|
||||
if s.WebsocketConn == nil {
|
||||
return errors.New("websocket not connected")
|
||||
}
|
||||
@@ -362,7 +376,7 @@ func (s *Slack) WebsocketSend(eventType, text string) error {
|
||||
// HandleMessage handles incoming messages and/or commands from slack
|
||||
func (s *Slack) HandleMessage(msg *Message) error {
|
||||
if msg == nil {
|
||||
return errors.New("msg is nil")
|
||||
return errors.New("slack msg is nil")
|
||||
}
|
||||
|
||||
msg.Text = strings.ToLower(msg.Text)
|
||||
@@ -373,18 +387,6 @@ func (s *Slack) HandleMessage(msg *Message) error {
|
||||
case strings.Contains(msg.Text, cmdHelp):
|
||||
return s.WebsocketSend("message", getHelp)
|
||||
|
||||
case strings.Contains(msg.Text, cmdTicker):
|
||||
return s.WebsocketSend("message", s.GetTicker("ANX"))
|
||||
|
||||
case strings.Contains(msg.Text, cmdOrderbook):
|
||||
return s.WebsocketSend("message", s.GetOrderbook("ANX"))
|
||||
|
||||
case strings.Contains(msg.Text, cmdSettings):
|
||||
return s.WebsocketSend("message", s.GetSettings())
|
||||
|
||||
case strings.Contains(msg.Text, cmdPortfolio):
|
||||
return s.WebsocketSend("message", s.GetPortfolio())
|
||||
|
||||
default:
|
||||
return s.WebsocketSend("message", "GoCryptoTrader SlackBot - Command Unknown!")
|
||||
}
|
||||
|
||||
@@ -343,7 +343,6 @@ func TestWebsocketSend(t *testing.T) {
|
||||
|
||||
func TestHandleMessage(t *testing.T) {
|
||||
msg := &Message{}
|
||||
|
||||
err := s.HandleMessage(msg)
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
@@ -358,24 +357,4 @@ func TestHandleMessage(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
}
|
||||
msg.Text = cmdTicker
|
||||
err = s.HandleMessage(msg)
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
}
|
||||
msg.Text = cmdOrderbook
|
||||
err = s.HandleMessage(msg)
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
}
|
||||
msg.Text = cmdSettings
|
||||
err = s.HandleMessage(msg)
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
}
|
||||
msg.Text = cmdPortfolio
|
||||
err = s.HandleMessage(msg)
|
||||
if err == nil {
|
||||
t.Error("test failed - slack HandleMessage(), Sent message through nil websocket")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/communications/base"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -39,6 +40,7 @@ func (s *SMSGlobal) Setup(cfg *config.CommunicationsConfig) {
|
||||
s.Verbose = cfg.SMSGlobalConfig.Verbose
|
||||
s.Username = cfg.SMSGlobalConfig.Username
|
||||
s.Password = cfg.SMSGlobalConfig.Password
|
||||
s.SendFrom = cfg.SMSGlobalConfig.From
|
||||
|
||||
var contacts []Contact
|
||||
for x := range cfg.SMSGlobalConfig.Contacts {
|
||||
@@ -49,10 +51,19 @@ func (s *SMSGlobal) Setup(cfg *config.CommunicationsConfig) {
|
||||
Enabled: cfg.SMSGlobalConfig.Contacts[x].Enabled,
|
||||
},
|
||||
)
|
||||
log.Debugf("SMSGlobal: SMS Contact: %s. Number: %s. Enabled: %v",
|
||||
cfg.SMSGlobalConfig.Contacts[x].Name,
|
||||
cfg.SMSGlobalConfig.Contacts[x].Number,
|
||||
cfg.SMSGlobalConfig.Contacts[x].Enabled)
|
||||
}
|
||||
s.Contacts = contacts
|
||||
}
|
||||
|
||||
// IsConnected returns whether or not the connection is connected
|
||||
func (s *SMSGlobal) IsConnected() bool {
|
||||
return s.Connected
|
||||
}
|
||||
|
||||
// Connect connects to the service
|
||||
func (s *SMSGlobal) Connect() error {
|
||||
s.Connected = true
|
||||
@@ -60,8 +71,8 @@ func (s *SMSGlobal) Connect() error {
|
||||
}
|
||||
|
||||
// PushEvent pushes an event to a contact list via SMS
|
||||
func (s *SMSGlobal) PushEvent(base.Event) error {
|
||||
return common.ErrNotYetImplemented
|
||||
func (s *SMSGlobal) PushEvent(event base.Event) error {
|
||||
return s.SendMessageToAll(event.Message)
|
||||
}
|
||||
|
||||
// GetEnabledContacts returns how many SMS contacts are enabled in the
|
||||
@@ -139,6 +150,10 @@ func (s *SMSGlobal) RemoveContact(contact Contact) error {
|
||||
func (s *SMSGlobal) SendMessageToAll(message string) error {
|
||||
for x := range s.Contacts {
|
||||
if s.Contacts[x].Enabled {
|
||||
if s.Verbose {
|
||||
log.Debugf("SMSGlobal: Sending SMS to %s. Number: %s. Message: %s [From: %s]",
|
||||
s.Contacts[x].Name, s.Contacts[x].Number, message, s.SendFrom)
|
||||
}
|
||||
err := s.SendMessage(s.Contacts[x].Number, message)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestConnect(t *testing.T) {
|
||||
|
||||
func TestPushEvent(t *testing.T) {
|
||||
err := s.PushEvent(base.Event{})
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
t.Error("test failed - SMSGlobal PushEvent() error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,11 @@ func (s *SMTPservice) Setup(cfg *config.CommunicationsConfig) {
|
||||
s.RecipientList = cfg.SMTPConfig.RecipientList
|
||||
}
|
||||
|
||||
// IsConnected returns whether or not the connection is connected
|
||||
func (s *SMTPservice) IsConnected() bool {
|
||||
return s.Connected
|
||||
}
|
||||
|
||||
// Connect connects to service
|
||||
func (s *SMTPservice) Connect() error {
|
||||
s.Connected = true
|
||||
|
||||
@@ -23,23 +23,17 @@ const (
|
||||
methodGetUpdates = "getUpdates"
|
||||
methodSendMessage = "sendMessage"
|
||||
|
||||
cmdStart = "/start"
|
||||
cmdStatus = "/status"
|
||||
cmdHelp = "/help"
|
||||
cmdSettings = "/settings"
|
||||
cmdTicker = "/ticker"
|
||||
cmdPortfolio = "/portfolio"
|
||||
cmdOrders = "/orderbooks"
|
||||
cmdStart = "/start"
|
||||
cmdStatus = "/status"
|
||||
cmdHelp = "/help"
|
||||
cmdSettings = "/settings"
|
||||
|
||||
cmdHelpReply = `GoCryptoTrader TelegramBot, thank you for using this service!
|
||||
Current commands are:
|
||||
/start - Will authenticate your ID
|
||||
/status - Displays the status of the bot
|
||||
/help - Displays current command list
|
||||
/settings - Displays current bot settings
|
||||
/ticker - Displays current ANX ticker data
|
||||
/portfolio - Displays your current portfolio
|
||||
/orderbooks - Displays current orderbooks for ANX`
|
||||
/settings - Displays current bot settings`
|
||||
|
||||
talkRoot = "GoCryptoTrader bot"
|
||||
)
|
||||
@@ -52,6 +46,9 @@ type Telegram struct {
|
||||
AuthorisedClients []int64
|
||||
}
|
||||
|
||||
// IsConnected returns whether or not the connection is connected
|
||||
func (t *Telegram) IsConnected() bool { return t.Connected }
|
||||
|
||||
// Setup takes in a Telegram configuration and sets verification token
|
||||
func (t *Telegram) Setup(cfg *config.CommunicationsConfig) {
|
||||
t.Name = cfg.TelegramConfig.Name
|
||||
@@ -73,8 +70,8 @@ func (t *Telegram) Connect() error {
|
||||
// PushEvent sends an event to a supplied recipient list via telegram
|
||||
func (t *Telegram) PushEvent(event base.Event) error {
|
||||
for i := range t.AuthorisedClients {
|
||||
err := t.SendMessage(fmt.Sprintf("Type: %s Details: %s GainOrLoss: %s",
|
||||
event.Type, event.TradeDetails, event.GainLoss), t.AuthorisedClients[i])
|
||||
err := t.SendMessage(fmt.Sprintf("Type: %s Message: %s",
|
||||
event.Type, event.Message), t.AuthorisedClients[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -146,21 +143,9 @@ func (t *Telegram) HandleMessages(text string, chatID int64) error {
|
||||
case strings.Contains(text, cmdStart):
|
||||
return t.SendMessage(fmt.Sprintf("%s: START COMMANDS HERE", talkRoot), chatID)
|
||||
|
||||
case strings.Contains(text, cmdOrders):
|
||||
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetOrderbook("ANX")), chatID)
|
||||
|
||||
case strings.Contains(text, cmdStatus):
|
||||
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetStatus()), chatID)
|
||||
|
||||
case strings.Contains(text, cmdTicker):
|
||||
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetTicker("ANX")), chatID)
|
||||
|
||||
case strings.Contains(text, cmdSettings):
|
||||
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetSettings()), chatID)
|
||||
|
||||
case strings.Contains(text, cmdPortfolio):
|
||||
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetPortfolio()), chatID)
|
||||
|
||||
default:
|
||||
return t.SendMessage(fmt.Sprintf("command %s not recognized", text), chatID)
|
||||
}
|
||||
|
||||
@@ -58,31 +58,16 @@ func TestHandleMessages(t *testing.T) {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages(cmdOrders, chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages(cmdStatus, chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages(cmdTicker, chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages(cmdSettings, chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages(cmdPortfolio, chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
err)
|
||||
}
|
||||
err = T.HandleMessages("Not a command", chatID)
|
||||
if err.Error() != testErrNotFound {
|
||||
t.Errorf("test failed - telegram HandleMessages() error, expected 'Not found' got '%s'",
|
||||
|
||||
Reference in New Issue
Block a user