-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_deletemodify.go
66 lines (54 loc) · 2.03 KB
/
handler_deletemodify.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
package telegrambot
import (
"context"
"fmt"
"github.com/looplab/fsm"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
"github.com/rs/zerolog/log"
)
func (bot nukiBot) fsmDeleteModifyCommand() *fsm.FSM {
return fsm.NewFSM(
"idle",
fsm.Events{
{Name: FSMEventDefault, Src: []string{"idle"}, Dst: "waiting_for_resa"},
{Name: "resa_received", Src: []string{"idle", "waiting_for_resa"}, Dst: "finished"},
{Name: "reset", Src: []string{"idle", "waiting_for_resa", "finished"}, Dst: "idle"},
},
fsm.Callbacks{
FSMEventDefault: bot.fsmEventDeleteModifyDefault,
"before_resa_received": bot.fsmEventDeleteModifyReceived,
"finished": fsmEventFinished,
},
)
}
func (bot nukiBot) fsmEventDeleteModifyDefault(ctx context.Context, e *fsm.Event) {
log.Debug().Str("callback", FSMEventDefault).Msg("Callback called")
msg := reinitMetadataMessage(e.FSM)
modifs := bot.reservationPendingModificationRoutine.GetAllPendingModifications()
if len(modifs) == 0 {
fsmRuntimeErr(e, "No pending modification available", "reset")
return
}
var keyboardButtons []telego.InlineKeyboardButton
for _, modif := range modifs {
keyboardButtons = append(keyboardButtons,
tu.InlineKeyboardButton(fmt.Sprintf("%s (%s - %s)", modif.ReservationRef, modif.FormatCheckIn(), modif.FormatCheckOut())).
WithCallbackData(NewCallbackData("resa_received", modif.ReservationRef)))
}
msg.ReplyMarkup = tu.InlineKeyboard(keyboardButtons)
msg.ParseMode = telego.ModeMarkdown
msg.Text = "What *pending modification* do you want to remove?"
msg.ProtectContent = true
}
func (bot nukiBot) fsmEventDeleteModifyReceived(ctx context.Context, e *fsm.Event) {
log.Debug().Str("callback", "resa_received").Msg("Callback called")
msg := reinitMetadataMessage(e.FSM)
data, err := checkFSMArg(e)
if err != nil {
msg.Text = fmt.Sprintf("An error occurred: %s", err)
return
}
bot.reservationPendingModificationRoutine.DeletePendingModification(data)
msg.Text = "Done\nClick on /listmodify to display the new list"
}