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

This commit is contained in:
Владимир Фёдоров 2023-04-06 01:18:25 +07:00
parent 9e6681da30
commit b771745c47
5 changed files with 106 additions and 10 deletions

View File

@ -9,4 +9,5 @@ const (
Go = Command("/go") Go = Command("/go")
Stat = Command("/stat") Stat = Command("/stat")
Eat = Command("/eat") Eat = Command("/eat")
Pause = Command("/pause")
) )

View File

@ -4,6 +4,7 @@ const (
UserStateNone = UserState("") UserStateNone = UserState("")
UserStateGo = UserState("Go") UserStateGo = UserState("Go")
UserStateEat = UserState("Eat") UserStateEat = UserState("Eat")
UserStatePause = UserState("Pause")
) )
type UserState string type UserState string

View File

@ -2,12 +2,13 @@ package db
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log" "log"
"time" "time"
"valera/config" "valera/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
) )
type DB struct { type DB struct {
@ -112,6 +113,19 @@ func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
return err return err
} }
func (db *DB) SetPause(chatID int64, duration time.Duration) error {
ctx := context.Background()
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc).Add(+duration)
_, err := db.chatsColection.UpdateOne(
ctx,
bson.M{"chat_id": chatID},
bson.M{"$set": bson.M{"pause_until": t}},
)
return err
}
func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) { func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
ctx := context.Background() ctx := context.Background()
@ -178,7 +192,21 @@ func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
func (db *DB) GetAllChats() ([]int64, error) { func (db *DB) GetAllChats() ([]int64, error) {
ctx := context.Background() ctx := context.Background()
cursor, err := db.chatsColection.Find(ctx, bson.M{}) loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc)
cursor, err := db.chatsColection.Find(
ctx,
bson.M{
"$or": bson.A{
bson.M{
"pause_until": bson.M{"$lt": t},
},
bson.M{
"pause_until": bson.M{"$exists": false},
},
},
},
)
if err != nil { if err != nil {
return nil, err return nil, err
} }

51
main.go
View File

@ -12,6 +12,7 @@ import (
"valera/calories" "valera/calories"
"valera/commands" "valera/commands"
"valera/db" "valera/db"
"valera/pause"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags" "github.com/umputun/go-flags"
@ -27,7 +28,7 @@ var (
) )
const ( const (
version = "v1.5.0" version = "v1.6.0"
) )
type Opts struct { type Opts struct {
@ -105,7 +106,7 @@ func run() {
} }
go func() { go func() {
http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/go", func(w http.ResponseWriter, _ *http.Request) {
chats, err := dataBase.GetAllChats() chats, err := dataBase.GetAllChats()
if err != nil { if err != nil {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
@ -121,7 +122,7 @@ func run() {
_, _ = fmt.Fprintf(w, `{"result":"ok"}`) _, _ = fmt.Fprintf(w, `{"result":"ok"}`)
}) })
http.HandleFunc("/stat", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/stat", func(w http.ResponseWriter, _ *http.Request) {
chats, err := dataBase.GetAllChats() chats, err := dataBase.GetAllChats()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -161,6 +162,16 @@ func run() {
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)
@ -223,6 +234,22 @@ func run() {
log.Println(err) log.Println(err)
} }
continue continue
case db.UserStatePause:
duration, err := pause.GetDuration(text)
if err != nil {
log.Println(err)
continue
}
if err := dataBase.SetPause(chatID, duration); err != nil {
log.Println(err)
}
msg := tgbot.NewMessage(chatID, fmt.Sprintf("Поставил паузу %v, отдыхай", duration))
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = bot.Send(msg)
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
continue
} }
command := commands.Command(strings.Replace(text, opts.Name, "", 1)) command := commands.Command(strings.Replace(text, opts.Name, "", 1))
@ -249,6 +276,24 @@ func run() {
log.Println(err) log.Println(err)
} }
} }
case commands.Pause:
msg := tgbot.NewMessage(chatID, "Хочешь отдохнуть? Сколько времени тебе нужно?")
msg.ReplyMarkup = tgbot.NewReplyKeyboard([][]tgbot.KeyboardButton{
tgbot.NewKeyboardButtonRow(
tgbot.NewKeyboardButton("1ч"),
tgbot.NewKeyboardButton("4ч"),
),
tgbot.NewKeyboardButtonRow(
tgbot.NewKeyboardButton("1д"),
tgbot.NewKeyboardButton("2д"),
tgbot.NewKeyboardButton("7д"),
),
}...)
if _, err := bot.Send(msg); err == nil {
if err := dataBase.SetStatusToChat(chatID, db.UserStatePause); err != nil {
log.Println(err)
}
}
} }
} }
} }

21
pause/pause.go Normal file
View File

@ -0,0 +1,21 @@
package pause
import (
"strconv"
"strings"
"time"
)
func GetDuration(text string) (time.Duration, error) {
d := strings.TrimSuffix(text, "ч")
d = strings.TrimSuffix(d, "д")
count, err := strconv.Atoi(d)
if err != nil {
return 0, err
}
res := time.Duration(count) * time.Hour
if strings.HasSuffix(text, "д") {
res *= 24
}
return res, nil
}