-
Notifications
You must be signed in to change notification settings - Fork 4
/
telegram.go
346 lines (311 loc) · 9.65 KB
/
telegram.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/eleboucher/berlin-vaccine-alert/models/chat"
"github.com/eleboucher/berlin-vaccine-alert/vaccines"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)
const (
startButton = "Start"
stopButton = "Stop"
filterButton = "Add filters (multiple choices available)"
azButton = "Look for AstraZeneca"
jjButton = "Look for Johnson & Johnson"
vcButton = "Look for MRNA vaccine (clinics and vaccination centers)"
everythingButton = "Look for everything"
contributeButton = "Contribute and support"
infoFilterButton = "Info about filters"
backButton = "Back"
)
var keyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(startButton),
tgbotapi.NewKeyboardButton(stopButton),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(filterButton),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(contributeButton),
),
)
var filtersKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(azButton),
tgbotapi.NewKeyboardButton(jjButton),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(vcButton),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(everythingButton),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton(infoFilterButton),
tgbotapi.NewKeyboardButton(backButton),
),
)
// Telegram Holds the structure for the telegram bot
type Telegram struct {
bot *tgbotapi.BotAPI
limiter *rate.Limiter
chatModel *chat.Model
}
// NewBot return a new Telegram Bot
func NewBot(bot *tgbotapi.BotAPI, chatModel *chat.Model) *Telegram {
return &Telegram{
bot: bot,
chatModel: chatModel,
limiter: rate.NewLimiter(rate.Every(time.Second/30), 1),
}
}
// SendMessage send a message in string to a channel id
func (t *Telegram) SendMessage(message string, channel int64) error {
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: channel,
ReplyToMessageID: 0,
},
Text: message,
DisableWebPagePreview: true,
}
ctx := context.Background()
err := t.limiter.Wait(ctx)
if err != nil {
return err
}
_, err = t.bot.Send(msg)
if err != nil {
if strings.Contains(err.Error(), "Forbidden:") {
_, err := t.chatModel.Delete(channel)
if err != nil {
return err
}
}
return err
}
return nil
}
// SendMessageToAllUser send a message to all the enabled users
func (t *Telegram) SendMessageToAllUser(result *vaccines.Result) error {
chats, err := t.chatModel.List(&result.VaccineName)
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(len(chats))
log.Infof("sending message %s for %d users\n", result.Message, len(chats))
for _, chat := range chats {
chat := chat
go func() {
defer wg.Done()
chat := chat
err := t.SendMessage(result.Message, chat.ID)
if err != nil {
log.Error(err)
}
}()
}
wg.Wait()
return nil
}
// HandleNewUsers handle the commands from telegrams
func (t *Telegram) HandleNewUsers() error {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := t.bot.GetUpdatesChan(u)
for update := range updates {
update := update
go func() {
if update.Message == nil { // ignore any non-Message Updates
return
}
logrus.Infof("Receiving new message: %#v", update.Message)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
switch update.Message.Text {
case "open", backButton:
msg.ReplyMarkup = keyboard
_, err := t.bot.Send(msg)
if err != nil {
log.Error(err)
}
case "close":
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
_, err := t.bot.Send(msg)
if err != nil {
log.Error(err)
}
case contributeButton:
err := t.SendMessage("Hey you 🚀,\nThanks a lot for using the bot,\n\n\nFeel free to contribute on Github: https://github.com/eleboucher/berlin-vaccine-alert\n\n\nOr feel free to contribute on Paypal https://paypal.me/ELeboucher or Buy me a beer https://www.buymeacoffee.com/eleboucher", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case filterButton:
msg.ReplyMarkup = filtersKeyboard
_, err := t.bot.Send(msg)
if err != nil {
log.Error(err)
}
case stopButton:
err := t.stopChat(update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case startButton:
err := t.startChat(update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case azButton:
_, err := t.chatModel.UpdateFilters(update.Message.Chat.ID, vaccines.AstraZeneca)
if err != nil {
log.Error(err)
}
err = t.SendMessage("subscribed to AstraZeneca updates", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case jjButton:
_, err := t.chatModel.UpdateFilters(update.Message.Chat.ID, vaccines.JohnsonAndJohnson)
if err != nil {
log.Error(err)
}
err = t.SendMessage("subscribed to Johnson And Johnson updates", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case vcButton:
_, err := t.chatModel.UpdateFilters(update.Message.Chat.ID, vaccines.MRNA)
if err != nil {
log.Error(err)
}
err = t.SendMessage("subscribed to MRNA vaccines updates", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case everythingButton:
_, err := t.chatModel.UpdateFilters(update.Message.Chat.ID, "")
if err != nil {
log.Error(err)
}
err = t.SendMessage("subscribed to every updates", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case infoFilterButton:
chat, err := t.chatModel.Find(update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
var filters string
if len(chat.Filters) == 0 {
filters = "unfiltered"
} else {
filters = strings.Join(chat.Filters, "\n")
}
msg := fmt.Sprintf("your current filters are :\n%s\n\nSelect %s to reset them", filters, everythingButton)
err = t.SendMessage(msg, update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
}
switch update.Message.Command() {
case "start":
err := t.startChat(update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case "stop":
err := t.stopChat(update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
case "open":
msg.ReplyMarkup = filtersKeyboard
_, err := t.bot.Send(msg)
if err != nil {
log.Error(err)
}
case "contribute":
err := t.SendMessage("Hey you 🚀,\nThanks a lot for using the bot,\n\n\nFeel free to contribute on Github: https://github.com/eleboucher/berlin-vaccine-alert\n\n\nOr feel free to contribute on Paypal https://paypal.me/ELeboucher or Buy me a beer https://www.buymeacoffee.com/eleboucher", update.Message.Chat.ID)
if err != nil {
log.Error(err)
}
}
}()
}
log.Info("done with telegram handler")
return nil
}
func (t *Telegram) startChat(chatID int64) error {
log.Infof("adding chat %d\n", chatID)
_, err := t.chatModel.Create(chatID)
if err != nil {
if errors.Is(err, chat.ErrChatAlreadyExist) {
_, err := t.chatModel.Enable(chatID)
if err != nil {
return err
}
err = t.SendMessage(`
Hey Again!
You are already added to the subscription list, you will receive appointments shortly when they will be available!
I hope this bot helps you in your research to get the vaccine!
Provide feedback 📢 on Reddit: https://www.reddit.com/r/berlinvaccination/comments/np81h5/telegram_bot_to_get_a_vaccine_appointment/
Feel free to help me with the cost or with the code, via:
💸 Donate via PayPal: https://paypal.me/ELeboucher
🍻 Buy me a beer: https://www.buymeacoffee.com/eleboucher
🧑💻 Contribute to the code: https://github.com/eleboucher/berlin-vaccine-alert
I really hope it can help you to find your appointment!
Stay Safe, and thanks for your support! ❤️`, chatID)
if err != nil {
return err
}
return err
}
return err
}
err = t.SendMessage(`
Welcome 👋🏼!
You are now added to the subscription list, you will receive appointments shortly when they will be available
I hope this bot helps you in your research to get the vaccine!
Provide feedback 📢 on Reddit: https://www.reddit.com/r/berlinvaccination/comments/np81h5/telegram_bot_to_get_a_vaccine_appointment/
Feel free to help me with the cost of the bot or with the code, via:
💸 Donate via PayPal: https://paypal.me/ELeboucher
🍻 Buy me a beer: https://www.buymeacoffee.com/eleboucher
🧑💻 Contribute to the code: https://github.com/eleboucher/berlin-vaccine-alert
I really hope it can help you to find your appointment!
Stay Safe, and thanks for your support! ❤️`, chatID)
if err != nil {
return err
}
return nil
}
func (t *Telegram) stopChat(chatID int64) error {
log.Infof("removing chat %d\n", chatID)
_, err := t.chatModel.Delete(chatID)
if err != nil {
return err
}
err = t.SendMessage(`
Hey!
You are removed from the list. If you want to receive messages again type /start.
I hope you had book an appointment and you are getting vaccinated soon!
If you have any feedback feel free to post something on Reddit: https://www.reddit.com/r/berlinvaccination/comments/np81h5/telegram_bot_to_get_a_vaccine_appointment/
Feel free to help me with the cost of the bot or with the code, via:
💸 Donate via PayPal: https://paypal.me/ELeboucher
🍻 Buy me a beer: https://www.buymeacoffee.com/eleboucher
🧑💻 Contribute to the code: https://github.com/eleboucher/berlin-vaccine-alert
Stay Safe, and thanks for your support! ❤️`, chatID)
if err != nil {
return err
}
return nil
}