2023-04-08 12:23:42 +00:00
|
|
|
|
package eat_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{
|
|
|
|
|
"/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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 07:47:38 +00:00
|
|
|
|
func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
2023-04-08 12:23:42 +00:00
|
|
|
|
if chatInfo.Status == string(db.UserStateEat) {
|
2023-04-09 06:31:01 +00:00
|
|
|
|
count, err := calcCalories(text)
|
2023-04-08 12:23:42 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 07:47:38 +00:00
|
|
|
|
if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, chatInfo.Username)); err != nil {
|
2023-04-08 12:23:42 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|