/
base.go
88 lines (68 loc) · 2.04 KB
/
base.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
package cmd
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
//Command interface
type BaseCmd interface {
Run(bot *tgbotapi.BotAPI, chatMessage *tgbotapi.Message) error
}
//Base abstract command
type Base struct {
}
func (base *Base) send(bot *tgbotapi.BotAPI, text string, chatId int64, customKeyboard bool) error {
msg := base.getNewMessage(text, chatId, customKeyboard)
_, err := bot.Send(msg)
return err
}
func (base *Base) getNewMessage(text string, chatId int64, customKeyboard bool) tgbotapi.MessageConfig {
msg := tgbotapi.NewMessage(chatId, text)
if customKeyboard {
var buttonsRow1 []tgbotapi.KeyboardButton
if remindStep.parent.GetStep() == 2 {
buttonsRow1 = []tgbotapi.KeyboardButton{
{Text: "Send My Location", RequestLocation: true},
}
} else {
buttonsRow1 = []tgbotapi.KeyboardButton{
{Text: "/remind"},
{Text: "/help"},
}
}
msg.ReplyMarkup = tgbotapi.NewReplyKeyboard(buttonsRow1)
} else {
msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{HideKeyboard: true}
}
return msg
}
// 'unknown' command
type UnknownCmd struct {
base *Base
}
func (unknown *UnknownCmd) Run(bot *tgbotapi.BotAPI, chatMessage *tgbotapi.Message) error {
text := "Unknown command."
return unknown.base.send(bot, text, chatMessage.Chat.ID, true)
}
// not a command message
type NotCmd struct {
base *Base
}
func (notCmd *NotCmd) Run(bot *tgbotapi.BotAPI, chatMessage *tgbotapi.Message) error {
text := "Sorry I don't understand your messages. Please send me a command from list."
return notCmd.base.send(bot, text, chatMessage.Chat.ID, true)
}
// '/start' command
type StartCmd struct {
base *Base
}
func (start *StartCmd) Run(bot *tgbotapi.BotAPI, chatMessage *tgbotapi.Message) error {
help := new(HelpCmd)
return help.Run(bot, chatMessage)
}
// '/help' command
type HelpCmd struct {
base *Base
}
func (help *HelpCmd) Run(bot *tgbotapi.BotAPI, chatMessage *tgbotapi.Message) error {
text := "You can subscribe to notification using this /remind command."
return help.base.send(bot, text, chatMessage.Chat.ID, true)
}