-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot_handler.go
More file actions
59 lines (47 loc) · 1.34 KB
/
bot_handler.go
File metadata and controls
59 lines (47 loc) · 1.34 KB
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
package main
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/mightymatth/earthquake-tools/tgbot/action"
"github.com/mightymatth/earthquake-tools/tgbot/storage"
"log"
)
func botHandler(update tgbotapi.Update, bot *tgbotapi.BotAPI, s storage.Service) {
if update.ChannelPost != nil {
update.Message = update.ChannelPost
}
if update.CallbackQuery != nil {
a, err := action.New(update.CallbackQuery.Data)
if err != nil {
log.Printf("cannot perform action: %s", err)
return
}
a.Perform(bot, update.CallbackQuery.Message, s)
_, _ = bot.AnswerCallbackQuery(tgbotapi.CallbackConfig{CallbackQueryID: update.CallbackQuery.ID})
return
}
if update.Message == nil { // ignore any non-Message Updates
return
}
switch update.Message.Text {
case "/start":
action.ShowHome(bot, update.Message.Chat.ID)
return
case "/list":
action.ShowSubscriptions(update.Message.Chat.ID, bot, s)
return
}
chatState := s.GetChatState(update.Message.Chat.ID)
if !update.Message.Chat.IsPrivate() && chatState.DisableInput {
return
}
if chatState.AwaitInput == "" {
action.ShowUnknownCommand(bot, update.Message.Chat.ID)
return
}
actionable, err := action.New(chatState.AwaitInput)
if err != nil {
log.Printf("cannot perform action: %s", err)
return
}
actionable.ProcessUserInput(bot, &update, s)
}