-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (182 loc) · 4.82 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"quote-telegram-bot/pkg/helpers"
"quote-telegram-bot/pkg/yfapi"
)
var (
botToken string
debug bool
version bool
Name string
Version string
Date string
)
func init() {
debugEnv, _ := strconv.ParseBool(os.Getenv("DEBUG"))
flag.StringVar(&botToken, "botToken", os.Getenv("BOT_TOKEN"), "Telegram bot token")
flag.BoolVar(&debug, "debug", debugEnv, "Enable debug")
flag.BoolVar(&version, "version", false, "Print version")
flag.Parse()
}
func main() {
if version {
fmt.Printf("%s %s %s", Name, Version, Date)
return
}
bot, err := tgbot.NewBotAPI(botToken)
if err != nil {
panic(err)
}
bot.Debug = debug
updateConfig := tgbot.NewUpdate(0)
updateConfig.Timeout = 30
updates := bot.GetUpdatesChan(updateConfig)
yfc := yfapi.NewYFClient()
for update := range updates {
// skip edited messages events
if update.EditedMessage != nil {
continue
}
msg := CreateMessage(&update)
// update.CallbackQuery used to process button presses
if update.CallbackQuery != nil {
// process search result button press
if len(strings.Split(update.CallbackQuery.Data, "|")) == 1 {
QueryQuote(yfc, update.CallbackQuery.Data, msg)
err := helpers.Retry(3, func() error {
if _, err := bot.Send(msg); err != nil {
return err
}
return nil
})
if err != nil {
log.Println(err)
}
continue
}
// generate chart button press metadata
params, err := yfapi.NewChartParams(update.CallbackQuery.Data)
if err != nil {
log.Println(err)
continue
}
// price and earnings/revenue charts have different sources and formats, but same interface
var data yfapi.Chartable
switch params.Measurement {
case "price":
data, err = yfc.GetPriceChart(params.Symbol, params.Interval)
case "earnings", "revenue":
data, err = yfc.GetQuote(params.Symbol)
default:
continue
}
if err != nil {
log.Println(err)
continue
}
switch params.Cmd {
// initial received only once when user press button "Charts" under quote info message.
// Price chart sent on this event.
case "initial":
chart, err := data.ChartBytes(params)
if err != nil {
log.Println(err)
continue
}
graph := tgbot.NewPhotoUpload(update.CallbackQuery.Message.Chat.ID, chart)
graph.ReplyMarkup = yfapi.ChartKeyboard(params, data.Intervals())
err = helpers.Retry(3, func() error {
if _, err := bot.Send(graph); err != nil {
return err
}
return nil
})
if err != nil {
log.Println(err)
}
default:
// any chart updates are processing here
chart, err := data.ChartBytes(params)
if err != nil {
log.Println(err)
continue
}
p := yfapi.NewMediaUpdateParams(update.CallbackQuery.Message, params, data.Intervals())
err = helpers.Retry(3, func() error {
if _, err = bot.UploadFile("editMessageMedia", p, "charts.png", chart); err != nil {
return err
}
return nil
})
if err != nil {
log.Println(err)
}
}
continue
}
// text messages and commands from user are processing here
switch s := update.Message.Command(); s {
case "start", "help":
msg.Text = yfapi.HelpMessage(update.Message.From.LanguageCode)
// any text messages are processing here
case "":
QueryQuote(yfc, update.Message.Text, msg)
default:
if update.Message.Text != "" && s != update.Message.Text {
s = update.Message.Text
}
result, err := yfc.Search(s)
if err != nil {
msg.Text = fmt.Sprintf("Unable to find: %s", s)
} else {
msg.Text = result.SearchMessage()
msg.ReplyMarkup = result.SearchMessageInlineKeyboard()
}
}
err := helpers.Retry(3, func() error {
if _, err := bot.Send(msg); err != nil {
return err
}
return nil
})
if err != nil {
log.Println(err)
}
}
}
func CreateMessage(update *tgbot.Update) *tgbot.MessageConfig {
var msg tgbot.MessageConfig
if update.CallbackQuery != nil {
fmt.Printf("%+v", update.CallbackQuery)
msg = tgbot.NewMessage(update.CallbackQuery.Message.Chat.ID, "")
} else if update.Message != nil {
fmt.Printf("%+v", update.Message)
msg = tgbot.NewMessage(update.Message.Chat.ID, "")
}
msg.ParseMode = tgbot.ModeMarkdown
msg.DisableWebPagePreview = true
return &msg
}
func QueryQuote(yfc *yfapi.YFClient, symbol string, msg *tgbot.MessageConfig) {
quote, err := yfc.GetQuote(symbol)
if err != nil {
msg.Text = fmt.Sprintf("Unable to get data for symbol: %s", symbol)
log.Println(err)
if qerr, ok := err.(*yfapi.QueryError); ok {
msg.Text = qerr.Error()
}
} else {
msg.Text = quote.StandardMessage()
msg.ReplyMarkup = quote.StandardMessageInlineKeyboard()
}
if msg.Text == "" {
msg.Text = fmt.Sprintf("No data found for symbol: %s", symbol)
}
}