add framework
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-04-08 18:55:16 +07:00
parent 11d3adf8db
commit 4509aae6a8
10 changed files with 403 additions and 147 deletions

View File

@ -4,34 +4,27 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"valera/internal/calories" "valera/internal/calories"
"valera/internal/commands" "valera/internal/commands"
"valera/internal/config" "valera/internal/config"
"valera/internal/db" "valera/internal/db"
"valera/internal/pause" "valera/internal/pause"
"valera/internal/states"
"valera/internal/states/clear_bot_state"
"valera/internal/states/go_bot_state"
"valera/internal/states/help_bot_state"
"valera/internal/states/ping_bot_state"
"valera/internal/states/start_bot_state"
"valera/internal/states/stat_bot_state"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
) )
var (
workoutTypes = []string{
"Отжимания",
"Пресс",
"Подтягивания",
"Приседания",
}
)
const ( const (
version = "v1.6.3" version = "v1.7.0"
) )
func main() { func main() {
run()
}
func run() {
cfg := config.NewAppConfig() cfg := config.NewAppConfig()
bot, err := tgbot.NewBotAPI(cfg.TgConfig.Token) bot, err := tgbot.NewBotAPI(cfg.TgConfig.Token)
@ -52,46 +45,19 @@ func run() {
panic(err) panic(err)
} }
botStates := []states.BotState{
clear_bot_state.NewClearBotState(bot, dataBase),
go_bot_state.NewGoBotState(bot, dataBase),
stat_bot_state.NewStatBotState(bot, dataBase),
start_bot_state.NewStartBotState(bot, dataBase, version),
help_bot_state.NewHelpBotState(bot, dataBase),
ping_bot_state.NewPingBotState(bot, dataBase),
}
go func() { go func() {
http.HandleFunc("/go", func(w http.ResponseWriter, _ *http.Request) { for _, s := range botStates {
chats, err := dataBase.GetAllChats() http.HandleFunc(s.GetHandler())
if err != nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
} }
for _, chatID := range chats {
sendGoToChat(bot, dataBase, chatID)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
})
http.HandleFunc("/stat", func(w http.ResponseWriter, _ *http.Request) {
chats, err := 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 := sendStatToChat(bot, dataBase, 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"}`)
})
log.Println("Server is start up! port", cfg.Port) log.Println("Server is start up! port", cfg.Port)
log.Fatal(http.ListenAndServe(cfg.Port, nil)) log.Fatal(http.ListenAndServe(cfg.Port, nil))
}() }()
@ -105,60 +71,19 @@ func run() {
text := update.Message.Text text := update.Message.Text
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
username := update.Message.From.UserName username := update.Message.From.UserName
if text == "/c" {
msg := tgbot.NewMessage(chatID, "Чистка")
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = bot.Send(msg)
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
continue
}
userInfoDTO, err := dataBase.GetChatInfo(chatID) userInfoDTO, err := dataBase.GetChatInfo(chatID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
} }
isWorkout := false for _, s := range botStates {
for _, workoutType := range workoutTypes { if err := s.Do(text, userInfoDTO, username); err != nil {
if userInfoDTO.GetStatus() == db.UserState(workoutType) {
count, err := strconv.Atoi(text)
if err != nil {
log.Println(err)
continue
}
if err := dataBase.AddWorkout(chatID, db.NewWorkout(workoutType, count, username)); err != nil {
log.Println(err)
continue
}
msgText := fmt.Sprintf("Отлично, %s, записал.", text)
if count <= 0 {
msgText = "Плохо, хочешь быть толстым и не красивым?"
}
_, _ = bot.Send(tgbot.NewMessage(chatID, msgText))
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err) log.Println(err)
} }
isWorkout = true
break
}
}
if isWorkout {
continue
} }
switch userInfoDTO.GetStatus() { switch userInfoDTO.GetStatus() {
case db.UserStateGo:
if err := dataBase.SetStatusToChat(chatID, db.UserState(text)); err != nil {
log.Println(err)
}
msg := tgbot.NewMessage(chatID, fmt.Sprintf("%s, отпишись сколько раз ты выполнил упражнение", text))
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = bot.Send(msg)
continue
case db.UserStateEat: case db.UserStateEat:
count, err := calories.CalcCalories(text) count, err := calories.CalcCalories(text)
if err != nil { if err != nil {
@ -198,22 +123,6 @@ func run() {
command := commands.Command(text) command := commands.Command(text)
switch command { switch command {
case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
case commands.Help:
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n - несколько видов упражнений 6 раз в день\n2) Показывать статистику за день\n3) Считать калории\n - введи одно число, я запишу калории\n - введи 2 числа через пробел одно количество грамм другое количество калорий в 100 граммах (порядок не важен), я посчитаю\n - введи название того что ты съел, я посчитаю (если знаю)"))
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go:
sendGoToChat(bot, dataBase, chatID)
case commands.Stat:
if err := sendStatToChat(bot, dataBase, chatID, ""); err != nil {
fmt.Println(err)
continue
}
case commands.Eat: case commands.Eat:
if _, err := bot.Send(tgbot.NewMessage(chatID, "Вижу ты поел, отпишись сколько калорий было")); err == nil { if _, err := bot.Send(tgbot.NewMessage(chatID, "Вижу ты поел, отпишись сколько калорий было")); err == nil {
if err := dataBase.SetStatusToChat(chatID, db.UserStateEat); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateEat); err != nil {
@ -241,30 +150,3 @@ func run() {
} }
} }
} }
func sendGoToChat(bot *tgbot.BotAPI, dataBase *db.DB, 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 := bot.Send(msg); err == nil {
if err := dataBase.SetStatusToChat(chatID, db.UserStateGo); err != nil {
log.Println(err)
}
}
}
func sendStatToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64, prefix string) error {
stat, err := dataBase.GetStat(chatID)
if err != nil {
return err
}
msgText := prefix + "Результаты за сегодня:\n"
for k, v := range stat {
msgText += fmt.Sprintf("- %s: %d\n", k, v)
}
_, _ = bot.Send(tgbot.NewMessage(chatID, msgText))
return nil
}

4
go.mod
View File

@ -1,10 +1,9 @@
module valera module valera
go 1.17 go 1.18
require ( require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/umputun/go-flags v1.5.1
go.mongodb.org/mongo-driver v1.11.2 go.mongodb.org/mongo-driver v1.11.2
) )
@ -19,6 +18,7 @@ require (
github.com/xdg-go/stringprep v1.0.3 // indirect github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
) )

7
go.sum
View File

@ -5,8 +5,8 @@ github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaEL
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@ -27,8 +27,6 @@ github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQ
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/umputun/go-flags v1.5.1 h1:vRauoXV3Ultt1HrxivSxowbintgZLJE+EcBy5ta3/mY=
github.com/umputun/go-flags v1.5.1/go.mod h1:nTbvsO/hKqe7Utri/NoyN18GR3+EWf+9RrmsdwdhrEc=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
@ -41,6 +39,8 @@ go.mongodb.org/mongo-driver v1.11.2 h1:+1v2rDQUWNcGW7/7E0Jvdz51V38XXxJfhzbV17aNH
go.mongodb.org/mongo-driver v1.11.2/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= go.mongodb.org/mongo-driver v1.11.2/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -52,7 +52,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -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)
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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))
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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
}