@@ -0,0 +1,44 @@
|
||||
package clear_bot_state
|
||||
|
||||
import (
|
||||
"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{
|
||||
"/c", // en
|
||||
"/с", // ru
|
||||
}
|
||||
|
||||
type clearBotState struct {
|
||||
bot *tgbot.BotAPI
|
||||
dataBase *db.DB
|
||||
}
|
||||
|
||||
func NewClearBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
||||
return &clearBotState{
|
||||
bot: bot,
|
||||
dataBase: dataBase,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *clearBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := tgbot.NewMessage(chatInfo.ChatID, "Чистка")
|
||||
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
|
||||
_, _ = s.bot.Send(msg)
|
||||
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
||||
}
|
||||
|
||||
func (s *clearBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
||||
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) 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, 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) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package help_bot_state
|
||||
|
||||
import (
|
||||
"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{
|
||||
"/help",
|
||||
}
|
||||
|
||||
type helpBotState struct {
|
||||
bot *tgbot.BotAPI
|
||||
dataBase *db.DB
|
||||
}
|
||||
|
||||
func NewHelpBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
||||
return &helpBotState{
|
||||
bot: bot,
|
||||
dataBase: dataBase,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *helpBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := "Вот что я умею:\n\n1) Предлагать размяться\n - несколько видов упражнений 6 раз в день\n2) Показывать статистику за день\n3) Считать калории\n - введи одно число, я запишу калории\n - введи 2 числа через пробел одно количество грамм другое количество калорий в 100 граммах (порядок не важен), я посчитаю\n - введи название того что ты съел, я посчитаю (если знаю)"
|
||||
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, msg))
|
||||
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
||||
}
|
||||
|
||||
func (s *helpBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
||||
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package states
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"valera/internal/db"
|
||||
)
|
||||
|
||||
type BotState interface {
|
||||
Do(text string, chatInfo *db.ChatInfo, username string) error
|
||||
GetHandler() (string, func(http.ResponseWriter, *http.Request))
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package ping_bot_state
|
||||
|
||||
import (
|
||||
"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{
|
||||
"/ping",
|
||||
}
|
||||
|
||||
type pingBotState struct {
|
||||
bot *tgbot.BotAPI
|
||||
dataBase *db.DB
|
||||
}
|
||||
|
||||
func NewPingBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
||||
return &pingBotState{
|
||||
bot: bot,
|
||||
dataBase: dataBase,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := "pong"
|
||||
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, msg))
|
||||
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
||||
}
|
||||
|
||||
func (s *pingBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
||||
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package start_bot_state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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{
|
||||
"/start",
|
||||
}
|
||||
|
||||
type startBotState struct {
|
||||
bot *tgbot.BotAPI
|
||||
dataBase *db.DB
|
||||
version string
|
||||
}
|
||||
|
||||
func NewStartBotState(bot *tgbot.BotAPI, dataBase *db.DB, version string) states.BotState {
|
||||
return &startBotState{
|
||||
bot: bot,
|
||||
dataBase: dataBase,
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *startBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", s.version, chatInfo.ChatID)
|
||||
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, msg))
|
||||
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
||||
}
|
||||
|
||||
func (s *startBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
||||
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package stat_bot_state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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{
|
||||
"/stat",
|
||||
}
|
||||
|
||||
type statBotState struct {
|
||||
bot *tgbot.BotAPI
|
||||
dataBase *db.DB
|
||||
}
|
||||
|
||||
func NewStatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
||||
return &statBotState{
|
||||
bot: bot,
|
||||
dataBase: dataBase,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *statBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
||||
s.sendStatToChat(chatInfo.ChatID, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *statBotState) 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 {
|
||||
fmt.Println(err)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
|
||||
return
|
||||
}
|
||||
for _, chatID := range chats {
|
||||
if err := s.sendStatToChat(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\n"); err != nil {
|
||||
fmt.Println(err)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *statBotState) sendStatToChat(chatID int64, prefix string) error {
|
||||
stat, err := s.dataBase.GetStat(chatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msgText := prefix + "Результаты за сегодня:\n"
|
||||
for k, v := range stat {
|
||||
msgText += fmt.Sprintf("- %s: %d\n", k, v)
|
||||
}
|
||||
_, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user