valera/internal/states/eat_bot_state/eat_bot_state.go

65 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package eat_bot_state
import (
"fmt"
"log"
"net/http"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
)
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) error {
if chatInfo.Status == string(db.UserStateEat) {
count, err := calcCalories(text)
if err != nil {
log.Println(err)
return nil
}
if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, chatInfo.Username)); err != nil {
log.Println(err)
return nil
}
stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDay(), times.GetNow())
if err != nil {
log.Println(err)
return nil
}
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("Калории, фу, %d, записал.\nКалорий сегодня: %v", count, stat["Калории"])))
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)
}
}