Compare commits

..

No commits in common. "e959812f9edab4446641858f9b56e2ba16c2803e" and "57755d28d236ce2b548c9630bfa29c738f2e8d40" have entirely different histories.

4 changed files with 27 additions and 91 deletions

View File

@ -6,6 +6,5 @@ const (
Start = Command("/start")
Help = Command("/help")
Ping = Command("/ping")
Go = Command("/go")
Stat = Command("/stat")
Go = Command("/go")
)

View File

@ -1,17 +1,15 @@
package config
type Config struct {
MongoURL string
DBName string
ChatsCollectionName string
WorkoutsCollectionName string
MongoURL string
DBName string
ChatsCollectionName string
}
func NewConfig() *Config {
return &Config{
MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027",
DBName: "valera",
ChatsCollectionName: "users",
WorkoutsCollectionName: "workouts",
MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027",
DBName: "valera",
ChatsCollectionName: "users",
}
}

View File

@ -5,7 +5,6 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
"valera/config"
)
@ -22,8 +21,9 @@ type chatDTO struct {
}
type ChatInfoDTO struct {
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
Workouts []Workout `bson:"workouts"`
}
func (c *ChatInfoDTO) GetStatus() UserState {
@ -34,22 +34,17 @@ func (c *ChatInfoDTO) GetStatus() UserState {
}
type Workout struct {
ChatID int64 `bson:"chat_id"`
Name string `bson:"name"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
Name string `bson:"name"`
Count int `bson:"count"`
}
func NewWorkout(
name string,
count int,
username string,
) *Workout {
return &Workout{
Name: name,
Count: count,
Username: username,
Name: name,
Count: count,
}
}
@ -80,13 +75,10 @@ func AddChat(chatID int64) error {
return result.Err()
}
func AddWorkout(chatID int64, workout *Workout) error {
func AddWorkoutInUser(chatID int64, workout *Workout) error {
if err := AddChat(chatID); err != nil {
return err
}
workout.ChatID = chatID
loc, _ := time.LoadLocation("Asia/Novosibirsk")
workout.CreatedAt = time.Now().In(loc)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
@ -96,18 +88,16 @@ func AddWorkout(chatID int64, workout *Workout) error {
return err
}
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
_, err = collection.InsertOne(
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
_, err = collection.UpdateOne(
ctx,
workout,
bson.M{"chat_id": chatID},
bson.M{"$push": bson.M{"workouts": workout}},
)
return err
}
func GetUserInfo(chatID int64) (*ChatInfoDTO, error) {
if err := AddChat(chatID); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
@ -141,34 +131,3 @@ func SetStatusInUser(chatID int64, status UserState) error {
)
return err
}
func GetStat(chatID int64) (map[string]int, error) {
if err := AddChat(chatID); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
cursor, err := collection.Find(ctx, bson.M{"chat_id": chatID})
if err != nil {
return nil, err
}
res := map[string]int{}
for cursor.Next(context.Background()) {
var result Workout
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
res[result.Name] += result.Count
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
return res, err
}

36
main.go
View File

@ -15,9 +15,10 @@ import (
)
const (
version = "v1.1.0"
version = "v1.0.0"
)
type Opts struct {
Token string `short:"t" long:"token" description:"Telegram api token"`
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@body_weight_loss_bot"`
@ -80,7 +81,8 @@ func run() {
text := update.Message.Text
chatID := update.Message.Chat.ID
username := update.Message.From.UserName
// username := update.Message.From.UserName
userInfoDTO, err := db.GetUserInfo(chatID)
if err != nil {
@ -90,19 +92,15 @@ func run() {
if userInfoDTO.GetStatus() == db.UserStateGo {
count, err := strconv.Atoi(text)
if err != nil {
log.Println(err)
continue
}
if err := db.AddWorkout(chatID, db.NewWorkout("Отжимания", count, username)); err != nil {
log.Println(err)
if err := db.AddWorkoutInUser(chatID, db.NewWorkout("Отжимания", count)); err != nil {
continue
}
msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text))
msg.ReplyMarkup = tgbot.NewHideKeyboard(false)
_, _ = bot.Send(msg)
if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
db.SetStatusInUser(chatID, db.UserStateNone)
continue
}
@ -110,11 +108,8 @@ func run() {
switch command {
case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
case commands.Help:
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n2) Показывать статистику"))
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться"))
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go:
@ -124,26 +119,11 @@ func run() {
tgbot.NewKeyboardButton("1"),
tgbot.NewKeyboardButton("2"),
tgbot.NewKeyboardButton("3"),
tgbot.NewKeyboardButton("5"),
tgbot.NewKeyboardButton("8"),
),
)
if _, err = bot.Send(msg); err == nil {
if err := db.SetStatusInUser(chatID, db.UserStateGo); err != nil {
log.Println(err)
}
db.SetStatusInUser(chatID, db.UserStateGo)
}
case commands.Stat:
stat, err := db.GetStat(chatID)
if err != nil {
log.Println(err)
continue
}
msgText := "Результаты за сегодня:\n"
for k, v := range stat {
msgText += fmt.Sprintf("- %s: %d\n", k, v)
}
_, _ = bot.Send(tgbot.NewMessage(chatID, msgText))
}
}
}