-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
152 lines (141 loc) · 3.87 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
package main
import (
"net/http"
"os"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/caarlos0/alelobot/internal/alelo"
"github.com/caarlos0/alelobot/internal/datastore"
"github.com/caarlos0/alelogo"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
ds := datastore.NewRedis(os.Getenv("REDIS_URL"))
defer ds.Close()
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
// without a port binded, heroku complains and eventually kills the process.
go serve()
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
for update := range updates {
update := update
if update.Message == nil {
continue
}
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
}).Info("New Message")
if update.Message.Command() == "login" {
login(ds, bot, update)
continue
}
if update.Message.Command() == "balance" {
go balance(ds, bot, update)
continue
}
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
}).Info("Unknown command", update.Message.Text)
bot.Send(tgbotapi.NewMessage(
update.Message.Chat.ID,
"Os únicos comandos suportados são /login e /balance",
))
}
}
func balance(ds datastore.Datastore, bot *tgbotapi.BotAPI, update tgbotapi.Update) {
cpf, pwd, err := ds.Retrieve(update.Message.From.ID)
if cpf == "" || pwd == "" || err != nil {
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
}).Info("Not logged in, telling user to do that")
bot.Send(tgbotapi.NewMessage(
update.Message.Chat.ID,
"Por favor, faça /login...",
))
return
}
details, err := alelo.AllDetails(cpf, pwd)
if err != nil {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, err.Error()))
return
}
for _, detail := range details {
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
"cpf": cpf,
"card_id": detail.Number,
}).Info("Got user card's details", details)
bot.Send(tgbotapi.NewMessage(
update.Message.Chat.ID,
"Saldo do cartão "+detail.Number+" é "+detail.Balance,
))
}
}
func login(ds datastore.Datastore, bot *tgbotapi.BotAPI, update tgbotapi.Update) {
parts := strings.Split(
strings.TrimSpace(update.Message.CommandArguments()), " ",
)
if len(parts) != 2 {
bot.Send(tgbotapi.NewMessage(
update.Message.Chat.ID,
"Para fazer login, diga\n\n/login CPF Senha",
))
return
}
cpf, pwd := parts[0], parts[1]
if _, err := alelogo.New(cpf, pwd); err != nil {
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
"cpf": cpf,
}).Error(err.Error())
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, err.Error()))
return
}
if err := ds.Save(update.Message.From.ID, cpf, pwd); err != nil {
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
"cpf": cpf,
}).Error(err.Error())
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, err.Error()))
return
}
log.WithFields(log.Fields{
"ChatID": update.Message.Chat.ID,
"From": update.Message.From.UserName,
"cpf": cpf,
}).Info("Login success")
bot.Send(tgbotapi.NewMessage(
update.Message.Chat.ID, "Sucesso, agora é só dizer /balance!",
))
}
func serve() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cpf := os.Getenv("TEST_CPF")
pwd := os.Getenv("TEST_PWD")
_, err := alelo.AllDetails(cpf, pwd)
if err != nil {
http.Error(
w,
"Can't connect to Alelo API",
http.StatusServiceUnavailable,
)
return
}
w.Write([]byte("OK"))
})
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}