diff --git a/cmd/valera/main.go b/cmd/valera/main.go index 6950fc0..6f1c321 100644 --- a/cmd/valera/main.go +++ b/cmd/valera/main.go @@ -8,11 +8,12 @@ import ( "valera/internal/commands" "valera/internal/config" "valera/internal/db" - "valera/internal/pause" "valera/internal/states" "valera/internal/states/clear_bot_state" + "valera/internal/states/eat_bot_state" "valera/internal/states/go_bot_state" "valera/internal/states/help_bot_state" + "valera/internal/states/pause_bot_state" "valera/internal/states/ping_bot_state" "valera/internal/states/start_bot_state" "valera/internal/states/stat_bot_state" @@ -52,6 +53,8 @@ func main() { start_bot_state.NewStartBotState(bot, dataBase, version), help_bot_state.NewHelpBotState(bot, dataBase), ping_bot_state.NewPingBotState(bot, dataBase), + pause_bot_state.NewPauseBotState(bot, dataBase), + eat_bot_state.NewEatBotState(bot, dataBase), } go func() { @@ -85,68 +88,7 @@ func main() { switch userInfoDTO.GetStatus() { case db.UserStateEat: - count, err := calories.CalcCalories(text) - if err != nil { - log.Println(err) - continue - } - if err := dataBase.AddCalories(chatID, db.NewCalories(count, username)); err != nil { - log.Println(err) - continue - } - if count <= 0 { - _, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой")) - continue - } - _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %d, записал.", count))) - if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil { - log.Println(err) - } - continue - case db.UserStatePause: - duration, err := pause.GetDuration(text) - if err != nil { - log.Println(err) - continue - } - if err := dataBase.SetPause(chatID, duration); err != nil { - log.Println(err) - } - msg := tgbot.NewMessage(chatID, fmt.Sprintf("Поставил паузу %v, отдыхай", duration)) - msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false) - _, _ = bot.Send(msg) - if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil { - log.Println(err) - } - continue - } - - command := commands.Command(text) - switch command { - case commands.Eat: - if _, err := bot.Send(tgbot.NewMessage(chatID, "Вижу ты поел, отпишись сколько калорий было")); err == nil { - if err := dataBase.SetStatusToChat(chatID, db.UserStateEat); err != nil { - log.Println(err) - } - } - case commands.Pause: - msg := tgbot.NewMessage(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д"), - ), - }...) - if _, err := bot.Send(msg); err == nil { - if err := dataBase.SetStatusToChat(chatID, db.UserStatePause); err != nil { - log.Println(err) - } - } + } } } diff --git a/internal/states/eat_bot_state/eat_bot_state.go b/internal/states/eat_bot_state/eat_bot_state.go new file mode 100644 index 0000000..86de266 --- /dev/null +++ b/internal/states/eat_bot_state/eat_bot_state.go @@ -0,0 +1,62 @@ +package eat_bot_state + +import ( + "fmt" + "log" + "net/http" + "valera/internal/calories" + "valera/internal/db" + "valera/internal/states" + + tgbot "github.com/go-telegram-bot-api/telegram-bot-api" + "golang.org/x/exp/slices" +) + +var names = []string{ + "/eat", +} + +type eatBotState struct { + bot *tgbot.BotAPI + dataBase *db.DB +} + +func NewEatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { + return &eatBotState{ + bot: bot, + dataBase: dataBase, + } +} + +func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { + if chatInfo.Status == string(db.UserStateEat) { + count, err := calories.CalcCalories(text) + if err != nil { + log.Println(err) + return nil + } + if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, username)); err != nil { + log.Println(err) + return nil + } + if count <= 0 { + _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, "Все фигня, давай по новой")) + return nil + } + _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("Калории, фу, %d, записал.", count))) + return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone) + } + + if !slices.Contains(names, text) { + return nil + } + + _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, "Вижу ты поел, отпишись сколько калорий было")) + return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateEat) +} + +func (s *eatBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) { + return names[0], func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + } +} diff --git a/internal/states/pause_bot_state/pause_bot_state.go b/internal/states/pause_bot_state/pause_bot_state.go new file mode 100644 index 0000000..ae381c3 --- /dev/null +++ b/internal/states/pause_bot_state/pause_bot_state.go @@ -0,0 +1,71 @@ +package pause_bot_state + +import ( + "fmt" + "log" + "net/http" + "valera/internal/db" + "valera/internal/pause" + "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, + } +} + +func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { + if chatInfo.Status == string(db.UserStatePause) { + duration, err := pause.GetDuration(text) + 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) + } +}