-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (159 loc) · 5.08 KB
/
main.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 main
import (
"github.com/Syfaro/telegram-bot-api"
"log"
"fmt"
"time"
"asset_tracker/types"
"asset_tracker/common"
"os"
)
// # TODO list
// 2. goroutine for each asset check in AssetWatcher
// 4. Implement panic?
func ValidateEnvs() []string {
return []string{"TG_TOKEN", "DB_PATH", "TIINGO_API_TOKEN"}
}
func AssetWatcher (bot *tgbotapi.BotAPI) {
var currentPrice float64
var savedAssets []types.SavedAsset
var err error
for {
savedAssets = common.DBRead()
fmt.Println(savedAssets)
for _, elem := range savedAssets {
if elem.AssetType == "stock" {
//#TODO some error handling maybe?
currentPrice, err = common.FetchStockPrice(elem.AssetName)
} else {
currentPrice, err = common.FetchCryptoPrice(elem.AssetName)
}
if err != nil {
log.Printf("Filed to fetch price for "+elem.AssetName)
continue
}
if (elem.InitPrice < elem.TargetPrice && elem.TargetPrice < currentPrice) ||
(elem.InitPrice > elem.TargetPrice && elem.TargetPrice > currentPrice) {
common.DeleteDBRecord(elem.UserID, elem.AssetName)
msg := tgbotapi.NewMessage(elem.UserID, "Reached target price ("+fmt.Sprint(elem.TargetPrice)+") for "+elem.AssetName)
bot.Send(msg)
}
}
time.Sleep(1 * time.Hour)
}
}
func main() {
var err error
for _, env := range ValidateEnvs() {
_, status := os.LookupEnv(env)
if status == false {
log.Printf("%s env is missing.", env)
os.Exit(1)
}
}
bot, err := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN"))
if err != nil {
log.Panic(err)
}
var userChats []types.SavedChat
var savedAssets []types.SavedAsset
var ChatPath string
var ChatStage int8
var replyText string
var validationBool bool
var usedKeyboard tgbotapi.ReplyKeyboardMarkup
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// Create chan for telegram updates
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
ucfg.Timeout = 60
updates, err := bot.GetUpdatesChan(ucfg)
go AssetWatcher(bot)
for update := range updates {
if update.Message != nil {
UserName := update.Message.From.UserName
ChatID := update.Message.Chat.ID
Text := update.Message.Text
log.Printf("[%s] %d %s", UserName, ChatID, Text)
usedKeyboard = common.CompileDefaultKeyboard()
if Text == "info" {
common.EndChat(&userChats, ChatID)
}
ChatPath, ChatStage = common.FetchUser(&userChats, ChatID)
fmt.Println(ChatPath, ChatStage) // debug
// Straight up ignore any input longer than 30 symbols since atm there is so case when it might be needed
if ChatPath != "" && len(Text) < 30 {
common.EndChat(&userChats, ChatID)
switch {
case ChatPath == "/crypto" || ChatPath == "/stock":
replyText = common.ConvertPrice(ChatPath[1:], Text)
case ChatPath == "/delete":
if common.ValidateUniqAsset(ChatID, Text) == false {
common.DeleteDBRecord(ChatID, Text)
replyText = "Asset successfully deleted"
} else {
replyText = "You are not tracking " + Text
}
// Watch conversation path
case ChatPath == "/watch":
switch(ChatStage) {
case 0:
if Text == "crypto" || Text == "stock" {
userChats = append(userChats, types.SavedChat{ChatID, ChatPath, 1})
savedAssets = append(savedAssets, types.SavedAsset{UserID: ChatID, AssetType: Text})
replyText = "Provide asset name"
} else {
replyText = "Unknown asset type, please write crypto/stock or use buttons"
}
case 1:
replyText, validationBool = common.UpdateAssetNPrice(&savedAssets, ChatID, Text)
if validationBool == true {
userChats = append(userChats, types.SavedChat{ChatID, ChatPath, 2})
}
case 2:
// Avoid really big numbers
if len(Text) > 20 {
replyText = "The target price provided is too long, try again"
} else {
replyText = common.AssetTrargetPrice(&savedAssets, ChatID, Text)
}
}
}
} else {
switch (Text) {
case "/start":
replyText = "Welcome :)"
case "/crypto":
userChats = append(userChats, types.SavedChat{ChatID, Text, 0})
replyText = "Provide coin name"
case "/stock":
userChats = append(userChats, types.SavedChat{ChatID, Text, 0})
replyText = "Provide stock name"
case "/watch":
userChats = append(userChats, types.SavedChat{ChatID, Text, 0})
replyText = "Provide asset type (stock/crypto)"
usedKeyboard = common.CompileTypeKeyboard()
case "/delete":
userChats = append(userChats, types.SavedChat{ChatID, Text, 0})
replyText = "Provide asset name"
case "/wipedb":
if ChatID == 88770025 {
common.WipeDB()
replyText = "DB wiped"
}
case "/assets":
replyText = common.CompileUserAssets(common.GetAssets(ChatID))
case "info":
replyText = "Info :)"
default:
replyText = "Command not recognized or the input was too long"
}
}
fmt.Println(userChats) // debug
fmt.Println(savedAssets) // debug
msg := tgbotapi.NewMessage(ChatID, replyText)
msg.ReplyMarkup = usedKeyboard
bot.Send(msg)
}
}
}