valera/internal/states/go_bot_state/go_bot_state.go

119 lines
3.0 KiB
Go
Raw Normal View History

2023-04-08 11:55:16 +00:00
package go_bot_state
import (
"fmt"
"log"
"net/http"
"strconv"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
)
var names = []string{
"/go",
"/го",
}
var (
workoutTypes = []string{
"Отжимания",
"Пресс",
"Подтягивания",
"Приседания",
}
)
type goBotState struct {
bot *tgbot.BotAPI
dataBase *db.DB
}
func NewGoBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
return &goBotState{
bot: bot,
dataBase: dataBase,
}
}
2023-04-09 07:47:38 +00:00
func (s *goBotState) Do(text string, chatInfo *db.ChatInfo) error {
2023-04-08 11:55:16 +00:00
if chatInfo.Status == string(db.UserStateGo) {
if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserState(text)); err != nil {
log.Println(err)
}
msg := tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%s, отпишись сколько раз ты выполнил упражнение", text))
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = s.bot.Send(msg)
return nil
}
isWorkout := false
for _, workoutType := range workoutTypes {
if chatInfo.GetStatus() == db.UserState(workoutType) {
count, err := strconv.Atoi(text)
if err != nil {
log.Println(err)
continue
}
2023-04-09 07:47:38 +00:00
if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, chatInfo.Username)); err != nil {
2023-04-08 11:55:16 +00:00
log.Println(err)
continue
}
msgText := fmt.Sprintf("Отлично, %s, записал.", text)
if count <= 0 {
msgText = "Плохо, хочешь быть толстым и не красивым?"
}
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, msgText))
if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone); err != nil {
log.Println(err)
}
isWorkout = true
break
}
}
if isWorkout {
return nil
}
2023-04-09 08:48:31 +00:00
if !slices.Contains(names, text) {
return nil
2023-04-08 11:55:16 +00:00
}
2023-04-09 08:48:31 +00:00
s.sendGoToChat(chatInfo.ChatID)
2023-04-08 11:55:16 +00:00
return nil
}
func (s *goBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
return names[0], func(w http.ResponseWriter, r *http.Request) {
chats, err := s.dataBase.GetAllChats()
if err != nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
}
for _, chatID := range chats {
s.sendGoToChat(chatID)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
}
}
func (s *goBotState) sendGoToChat(chatID int64) {
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, выбирай:")
rows := make([][]tgbot.KeyboardButton, 0, len(workoutTypes))
for _, workoutType := range workoutTypes {
rows = append(rows, tgbot.NewKeyboardButtonRow(tgbot.NewKeyboardButton(workoutType)))
}
msg.ReplyMarkup = tgbot.NewReplyKeyboard(rows...)
if _, err := s.bot.Send(msg); err == nil {
if err := s.dataBase.SetStatusToChat(chatID, db.UserStateGo); err != nil {
log.Println(err)
}
}
}