-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
87 lines (76 loc) · 1.9 KB
/
handler.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
package main
import (
"fmt"
"log"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
GenerateMessage = "generate random"
ForTodayMessage = "for today"
ForTomorrowMessage = "for tomorrow"
)
type UpdateHandler interface {
Handle(update tgbotapi.Update)
}
func NewMessageHandler(kgc *KudaGoClient, b *tgbotapi.BotAPI) UpdateHandler {
return &handler{
kgc: *kgc,
bot: b,
}
}
type handler struct {
kgc KudaGoClient
bot *tgbotapi.BotAPI
}
func (h *handler) Handle(update tgbotapi.Update) {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
var m string
switch update.Message.Text {
case "/start":
m = "Please generate new event"
case GenerateMessage:
event, err := h.kgc.GetRandomEvent("msk", update.Message.From.UserName)
if err != nil {
log.Print(err)
return
}
m = fmt.Sprintf("%s \n %s", event.Title, event.Site)
case ForTodayMessage:
event, err := h.kgc.GetEventForToday("msk", update.Message.From.UserName)
if err != nil {
log.Print(err)
return
}
m = fmt.Sprintf("Event for today \n %s \n %s", event.Title, event.Site)
case ForTomorrowMessage:
event, err := h.kgc.GetEventForTomorrow("msk", update.Message.From.UserName)
if err != nil {
log.Print(err)
return
}
m = fmt.Sprintf("Event for tomorrow \n %s \n %s", event.Title, event.Site)
case "/help":
m = "Generate random event"
default:
m = "Fuck off -_-"
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, m)
msg.ReplyToMessageID = update.Message.MessageID
attachKeybord(&msg)
_, err := h.bot.Send(msg)
if err != nil {
log.Print(err)
return
}
}
func attachKeybord(msg *tgbotapi.MessageConfig) {
msg.ReplyMarkup = tgbotapi.NewReplyKeyboard(
[]tgbotapi.KeyboardButton{
tgbotapi.NewKeyboardButton(GenerateMessage),
},
[]tgbotapi.KeyboardButton{
tgbotapi.NewKeyboardButton(ForTodayMessage),
tgbotapi.NewKeyboardButton(ForTomorrowMessage),
},
)
}