2023-04-08 12:23:42 +00:00
|
|
|
package pause_bot_state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"valera/internal/db"
|
|
|
|
"valera/internal/states"
|
|
|
|
|
|
|
|
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
|
|
|
|
|
|
|
var names = []string{
|
|
|
|
"/pause",
|
|
|
|
}
|
|
|
|
|
|
|
|
type pauseBotState struct {
|
|
|
|
bot *tgbot.BotAPI
|
|
|
|
dataBase *db.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPauseBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|
|
|
return &pauseBotState{
|
|
|
|
bot: bot,
|
|
|
|
dataBase: dataBase,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 07:47:38 +00:00
|
|
|
func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
2023-04-08 12:23:42 +00:00
|
|
|
if chatInfo.Status == string(db.UserStatePause) {
|
2023-04-09 06:31:01 +00:00
|
|
|
duration, err := getDuration(text)
|
2023-04-08 12:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := s.dataBase.SetPause(chatInfo.ChatID, duration); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
msg := tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("Поставил паузу %v, отдыхай", duration))
|
|
|
|
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
|
|
|
|
_, _ = s.bot.Send(msg)
|
|
|
|
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !slices.Contains(names, text) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbot.NewMessage(chatInfo.ChatID, "Хочешь отдохнуть? Сколько времени тебе нужно?")
|
|
|
|
msg.ReplyMarkup = tgbot.NewReplyKeyboard([][]tgbot.KeyboardButton{
|
|
|
|
tgbot.NewKeyboardButtonRow(
|
|
|
|
tgbot.NewKeyboardButton("1ч"),
|
|
|
|
tgbot.NewKeyboardButton("4ч"),
|
|
|
|
),
|
|
|
|
tgbot.NewKeyboardButtonRow(
|
|
|
|
tgbot.NewKeyboardButton("1д"),
|
|
|
|
tgbot.NewKeyboardButton("2д"),
|
|
|
|
tgbot.NewKeyboardButton("7д"),
|
|
|
|
),
|
|
|
|
}...)
|
|
|
|
_, _ = s.bot.Send(msg)
|
|
|
|
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStatePause)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *pauseBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
|
|
|
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|