valera/internal/states/go_bot_state/go_bot_state.go

121 lines
3.0 KiB
Go
Raw 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 go_bot_state
import (
"fmt"
"log"
"net/http"
"strconv"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
"valera/internal/db"
"valera/internal/states"
)
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,
}
}
func (s *goBotState) Do(text string, chatInfo *db.ChatInfo) error {
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
}
if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, chatInfo.Username)); err != nil {
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
}
if !slices.Contains(names, text) {
return nil
}
s.sendGoToChat(chatInfo.ChatID)
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)
}
}
}