/
setname.go
107 lines (88 loc) · 2.82 KB
/
setname.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
package action
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/mightymatth/earthquake-tools/tgbot/entity"
"github.com/mightymatth/earthquake-tools/tgbot/storage"
"log"
)
type SetNameAction struct {
Action
}
const SetName Cmd = "SET_NAME"
func NewSetNameAction(subID string) SetNameAction {
return SetNameAction{Action{Cmd: SetName, Params: Params{P1: subID}}}
}
func (a SetNameAction) Perform(bot *tgbotapi.BotAPI, msg *tgbotapi.Message, s storage.Service) {
err := s.SetAwaitUserInput(msg.Chat.ID, a.Encode())
if err != nil {
log.Printf("cannot set chat state: %v", err)
return
}
sub, err := s.GetSubscription(a.Params.P1)
if err != nil {
log.Printf("cannot get subscription: %v", err)
return
}
message := editedMessageConfig(msg.Chat.ID, msg.MessageID, a.text(), a.inlineButtons(sub))
_, _ = bot.Send(message)
}
func (a SetNameAction) text() string {
return `
<b>Subscription ᐅ Name</b>
Enter short subscription name (e.g. city, region or state you want to observe).
`
}
func (a SetNameAction) inlineButtons(sub *entity.Subscription) *tgbotapi.InlineKeyboardMarkup {
cancel := tgbotapi.NewInlineKeyboardButtonData("❌ Cancel",
NewSubscriptionAction(sub.SubID, ResetInput).Encode())
kb := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(cancel),
)
return &kb
}
func ShowSetName(chatID int64, subID string, bot *tgbotapi.BotAPI, s storage.Service) {
sub, err := s.GetSubscription(subID)
if err != nil {
log.Printf("cannot get subscription: %v", err)
return
}
setNameAction := NewSetNameAction(subID)
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: chatID,
ReplyMarkup: setNameAction.inlineButtons(sub),
},
Text: setNameAction.text(),
ParseMode: tgbotapi.ModeHTML,
DisableWebPagePreview: true,
}
_, _ = bot.Send(msg)
}
func (a SetNameAction) ProcessUserInput(bot *tgbotapi.BotAPI, update *tgbotapi.Update, s storage.Service) {
if err := a.processInputValue(update.Message.Text); err != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, a.WrongInput())
_, _ = bot.Send(msg)
ShowSetName(update.Message.Chat.ID, a.Params.P1, bot, s)
return
}
nameUpdate := entity.SubscriptionUpdate{Name: update.Message.Text}
_, err := storage.Service.UpdateSubscription(s, a.Params.P1, &nameUpdate)
if err != nil {
log.Printf("cannot set name to subscription: %v", err)
return
}
_ = ResetAwaitInput(ResetInput, update.Message.Chat.ID, s)
ShowSubscription(update.Message.Chat.ID, a.Params.P1, bot, s)
}
func (a SetNameAction) processInputValue(text string) error {
switch {
case text == "", len(text) > 64:
return fmt.Errorf("invalid")
default:
return nil
}
}
func (a SetNameAction) WrongInput() string {
return "Wrong input. A simple, text value expected; no more than 64 characters."
}