mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 15:09:57 +00:00
* Initial codes for a trade tracker * Moving everything in a broken fashion * Removes tradetracker. Removes some errors for subsystems * Cleans up some subsystems, renames stuttering types. Removes some global Bot usage * More basic subsystem renaming and file moving * Removes engine dependency from events,ntpserver,ordermanager,comms manager * Exports eventManager, fixes rpcserver. puts rpcserver back for now * Removes redundant error message, further removes engine dependencies * experimental end of day interface usage * adds ability to build the application * Withdraw and event manager handling * cleans up apiserver and communications manager * Cleans up some start/setup processes. Though should separate * More consistency with Setup Start Stop IsRunning funcs * Final consistency pass before testing phase * Fixes engine tests. Fixes stop nil issue * api server tests * Communications manager testing * Connection manager tests and nilsubsystem error * End of day currencypairsyncer tests * Adds databaseconnection/databaseconnection_test.go * Adds withdrawal manager tests * Deposit address testing. Moved orderbook sync first as its more important * Adds test for event manager * More full eventmanager testing * Adds testfile. Enables skipped test. * ntp manager tests * Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles * Adds websocket routine manager tests * Basic portfolio manager testing * Fixes issue with currency pair sync startup * Fixes issue with event manager startup * Starts the order manager before backtester starts * Fixes fee tests. Expands testing. Doesnt fix races * Fixes most test races * Resolves data races * Fixes subsystem test issues * currency pair syncer coverage tests * Refactors portfolio. Fixes tests. Withdraw validation Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager is in charge how the portfolio is handled and all portfolio functions are attached to the base instead of just exported at the package level Withdrawal validation occurred at the exchange level when it can just be run at the withdrawal manager level. All withdrawal requests go through that endpoint * lint -fix * golang lint fixes * lints and comments everything * Updates GCT logo, adds documentation for some subsystems * More documentation and more logo updates * Fixes backtesting and apiserver errors encountered * Fixes errors and typos from reviewing * More minor fixes * Changes %h verb to %w * reverbs to %s * Humbly begins reverting to more flat engine package The main reasoning for this is that the subsystem split doesn't make sense in a golang environment. The subsystems are only meant to be used with engine and so by placing them in a non-engine area, it does not work and is inconsistent with the rest of the application's package layout. This will begin salvaging the changes made by reverting to a flat engine package, but maintaining the consistent designs introduced. Further, I will look to remove any TestMains and decrease the scope of testing to be more local and decrease the issues that have been caused from our style of testing. * Manages to re-flatten things. Everything is within its own file * mini fixes * Fixes tests and data races and lints * Updates docs tool for engine to create filename readmes * os -> ioutil * remove err * Appveyor version increase test * Removes tCleanup as its unsupported on appveyor * Adds stuff that I thought was in previous merge master commit * Removes cancel from test * Fixes really fun test-exclusive data race * minor nit fixes * niterinos * docs gen * rm;rf test * Remove typoline. expands startstop helper. Splits apiserver * Removes accidental folder * Uses update instead of replace for order upsert * addresses nits. Renames files. Regenerates documentation. * lint and removal of comments * Add new test for default scenario * Fixes typo * regen docs
258 lines
6.3 KiB
Go
258 lines
6.3 KiB
Go
// Package telegram is used to connect to a cloud-based mobile and desktop
|
|
// messaging app using the bot API defined in
|
|
// https://core.telegram.org/bots/api#recent-changes
|
|
package telegram
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/communications/base"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
const (
|
|
apiURL = "https://api.telegram.org/bot%s/%s"
|
|
|
|
methodGetMe = "getMe"
|
|
methodGetUpdates = "getUpdates"
|
|
methodSendMessage = "sendMessage"
|
|
|
|
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`
|
|
|
|
talkRoot = "GoCryptoTrader bot"
|
|
)
|
|
|
|
var (
|
|
// ErrWaiter is the default timer to wait if an err occurs
|
|
// before retrying after successfully connecting
|
|
ErrWaiter = time.Second * 30
|
|
)
|
|
|
|
// Telegram is the overarching type across this package
|
|
type Telegram struct {
|
|
base.Base
|
|
initConnected bool
|
|
Token string
|
|
Offset int64
|
|
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 *base.CommunicationsConfig) {
|
|
t.Name = cfg.TelegramConfig.Name
|
|
t.Enabled = cfg.TelegramConfig.Enabled
|
|
t.Token = cfg.TelegramConfig.VerificationToken
|
|
t.Verbose = cfg.TelegramConfig.Verbose
|
|
}
|
|
|
|
// Connect starts an initial connection
|
|
func (t *Telegram) Connect() error {
|
|
if err := t.TestConnection(); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Debugln(log.CommunicationMgr, "Telegram: Connected successfully!")
|
|
t.Connected = true
|
|
go t.PollerStart()
|
|
return nil
|
|
}
|
|
|
|
// PushEvent sends an event to a supplied recipient list via telegram
|
|
func (t *Telegram) PushEvent(event base.Event) error {
|
|
msg := fmt.Sprintf("Type: %s Message: %s",
|
|
event.Type, event.Message)
|
|
for i := range t.AuthorisedClients {
|
|
err := t.SendMessage(msg, t.AuthorisedClients[i])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PollerStart starts the long polling sequence
|
|
func (t *Telegram) PollerStart() {
|
|
errWait := func(err error) {
|
|
log.Errorln(log.CommunicationMgr, err)
|
|
time.Sleep(ErrWaiter)
|
|
}
|
|
|
|
for {
|
|
if !t.initConnected {
|
|
err := t.InitialConnect()
|
|
if err != nil {
|
|
errWait(err)
|
|
continue
|
|
}
|
|
t.initConnected = true
|
|
}
|
|
|
|
resp, err := t.GetUpdates()
|
|
if err != nil {
|
|
errWait(err)
|
|
continue
|
|
}
|
|
|
|
for i := range resp.Result {
|
|
if resp.Result[i].UpdateID > t.Offset {
|
|
if string(resp.Result[i].Message.Text[0]) == "/" {
|
|
err = t.HandleMessages(resp.Result[i].Message.Text, resp.Result[i].Message.From.ID)
|
|
if err != nil {
|
|
log.Errorf(log.CommunicationMgr, "Telegram: Unable to HandleMessages. Error: %s\n", err)
|
|
continue
|
|
}
|
|
}
|
|
t.Offset = resp.Result[i].UpdateID
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// InitialConnect sets offset, and sends a welcome greeting to any associated
|
|
// IDs
|
|
func (t *Telegram) InitialConnect() error {
|
|
resp, err := t.GetUpdates()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !resp.Ok {
|
|
return errors.New(resp.Description)
|
|
}
|
|
|
|
warmWelcomeList := make(map[string]int64)
|
|
for i := range resp.Result {
|
|
if resp.Result[i].Message.From.ID != 0 {
|
|
warmWelcomeList[resp.Result[i].Message.From.UserName] = resp.Result[i].Message.From.ID
|
|
}
|
|
}
|
|
|
|
for userName, ID := range warmWelcomeList {
|
|
err = t.SendMessage(fmt.Sprintf("GoCryptoTrader bot has connected: Hello, %s!", userName), ID)
|
|
if err != nil {
|
|
log.Errorf(log.CommunicationMgr, "Telegram: Unable to send welcome message. Error: %s\n", err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
if len(resp.Result) == 0 {
|
|
return nil
|
|
}
|
|
|
|
t.Offset = resp.Result[len(resp.Result)-1].UpdateID
|
|
return nil
|
|
}
|
|
|
|
// HandleMessages handles incoming message from the long polling routine
|
|
func (t *Telegram) HandleMessages(text string, chatID int64) error {
|
|
if t.Verbose {
|
|
log.Debugf(log.CommunicationMgr, "Telegram: Received message: %s\n", text)
|
|
}
|
|
|
|
switch {
|
|
case strings.Contains(text, cmdHelp):
|
|
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, cmdHelpReply), chatID)
|
|
|
|
case strings.Contains(text, cmdStart):
|
|
return t.SendMessage(fmt.Sprintf("%s: START COMMANDS HERE", talkRoot), chatID)
|
|
|
|
case strings.Contains(text, cmdStatus):
|
|
return t.SendMessage(fmt.Sprintf("%s: %s", talkRoot, t.GetStatus()), chatID)
|
|
|
|
default:
|
|
return t.SendMessage(fmt.Sprintf("Command %s not recognized", text), chatID)
|
|
}
|
|
}
|
|
|
|
// GetUpdates gets new updates via a long poll connection
|
|
func (t *Telegram) GetUpdates() (GetUpdateResponse, error) {
|
|
var newUpdates GetUpdateResponse
|
|
path := fmt.Sprintf(apiURL, t.Token, methodGetUpdates)
|
|
return newUpdates, t.SendHTTPRequest(path, nil, &newUpdates)
|
|
}
|
|
|
|
// TestConnection tests bot's supplied authentication token
|
|
func (t *Telegram) TestConnection() error {
|
|
var isConnected User
|
|
path := fmt.Sprintf(apiURL, t.Token, methodGetMe)
|
|
|
|
err := t.SendHTTPRequest(path, nil, &isConnected)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !isConnected.Ok {
|
|
return errors.New(isConnected.Description)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SendMessage sends a message to a user by their chatID
|
|
func (t *Telegram) SendMessage(text string, chatID int64) error {
|
|
path := fmt.Sprintf(apiURL, t.Token, methodSendMessage)
|
|
|
|
messageToSend := struct {
|
|
ChatID int64 `json:"chat_id"`
|
|
Text string `json:"text"`
|
|
}{
|
|
chatID,
|
|
text,
|
|
}
|
|
|
|
json, err := json.Marshal(&messageToSend)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp := Message{}
|
|
err = t.SendHTTPRequest(path, json, &resp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !resp.Ok {
|
|
return errors.New(resp.Description)
|
|
}
|
|
|
|
if t.Verbose {
|
|
log.Debugf(log.CommunicationMgr, "Telegram: Sent '%s'\n", text)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SendHTTPRequest sends an authenticated HTTP request
|
|
func (t *Telegram) SendHTTPRequest(path string, data []byte, result interface{}) error {
|
|
headers := make(map[string]string)
|
|
headers["content-type"] = "application/json"
|
|
|
|
resp, err := common.SendHTTPRequest(http.MethodPost,
|
|
path,
|
|
headers,
|
|
bytes.NewBuffer(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return json.Unmarshal([]byte(resp), result)
|
|
}
|