-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
187 lines (163 loc) · 4.11 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
package main
import (
"fmt"
"os"
"sync"
"time"
"github.com/eleboucher/berlin-vaccine-alert/models/chat"
"github.com/eleboucher/berlin-vaccine-alert/sources"
"github.com/eleboucher/berlin-vaccine-alert/vaccines"
"github.com/getsentry/sentry-go"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/makasim/sentryhook"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Fetcher is the type to allow fetching information for an appointment
type Fetcher interface {
Name() string
Fetch() ([]*vaccines.Result, error)
ShouldSendResult(result []*vaccines.Result) bool
ResultSentNow(result []*vaccines.Result)
}
var rootCmd = &cobra.Command{
Use: "berlin-vaccine-alert <command>",
}
func fetchAllAppointment(fetchers []Fetcher, bot *Telegram) {
done := make(chan bool)
errChan := make(chan error)
for _, fetcher := range fetchers {
fetcher := fetcher
go func() {
log.Infof("%s: Starting fetch", fetcher.Name())
res, err := fetcher.Fetch()
if err != nil {
errChan <- err
return
}
log.Infof("%s: Received %d result", fetcher.Name(), len(res))
if len(res) > 0 && fetcher.ShouldSendResult(res) {
fetcher.ResultSentNow(res)
for _, r := range res {
err = bot.SendMessageToAllUser(r)
if err != nil {
errChan <- err
return
}
}
log.Infof("%s: messages sent on telegram", fetcher.Name())
}
done <- true
}()
}
timeout := time.After(5 * time.Second)
for {
select {
case <-done:
continue
case <-timeout:
return
case err := <-errChan:
log.Errorf("%v\n", err)
}
}
}
func init() {
viper.SetConfigName(".config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
if err := sentry.Init(sentry.ClientOptions{Dsn: viper.GetString("SENTRY_DSN")}); err != nil {
log.Fatal(err)
}
log.AddHook(sentryhook.New([]logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}))
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.InfoLevel)
}
func main() {
db, err := NewDB()
if err != nil {
log.Error(err)
return
}
bot, err := tgbotapi.NewBotAPI(viper.GetString("TELEGRAM_TOKEN"))
if err != nil {
log.Error(err)
return
}
chatModel := chat.NewModel(db)
telegram := NewBot(bot, chatModel)
var s = []Fetcher{
&sources.PuntoMedico{},
&sources.MedicoLeopoldPlatz{},
&sources.ArkonoPlatz{},
&sources.ArkonoPlatzJJ{},
&sources.ArkonoPlatzPfizer{},
&sources.Helios{},
}
var runCMD = &cobra.Command{
Use: "run",
Short: "run the telegram bot",
Run: func(cmd *cobra.Command, args []string) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
err := telegram.HandleNewUsers()
if err != nil {
log.Error(err)
return
}
}()
go func() {
defer wg.Done()
for range time.Tick(30 * time.Second) {
go fetchAllAppointment(s, telegram)
}
}()
wg.Wait()
},
}
var sendCMD = &cobra.Command{
Use: "send",
Short: "send message to all active user",
RunE: func(cmd *cobra.Command, args []string) error {
chats, err := chatModel.List(nil)
if err != nil {
return err
}
for _, chat := range chats {
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: chat.ID,
ReplyToMessageID: 0,
},
Text: "Hey, Thanks again for using the bot!\n\n Sadly Doctolib banned my server IP due to too many request therefore the bot will only give result for appointment outside of doctolib until I can fix it",
DisableWebPagePreview: true,
}
_, err := bot.Send(msg)
if err != nil {
log.Error(err)
continue
}
}
return nil
},
}
rootCmd.AddCommand(runCMD)
rootCmd.AddCommand(sendCMD)
err = rootCmd.Execute()
if err != nil {
log.Error(err)
}
}