diff --git a/config/config.go b/config/config.go index 1967315..2d339d1 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,7 @@ func NewConfig() *Config { return &Config{ MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027", DBName: "valera", - ChatsCollectionName: "users", + ChatsCollectionName: "chats", WorkoutsCollectionName: "workouts", } } diff --git a/db/db.go b/db/db.go index fa3a435..26d35f4 100644 --- a/db/db.go +++ b/db/db.go @@ -81,6 +81,9 @@ func AddChat(chatID int64) error { } func AddWorkout(chatID int64, workout *Workout) error { + if workout.Count <= 0 { + return nil + } if err := AddChat(chatID); err != nil { return err } @@ -104,7 +107,7 @@ func AddWorkout(chatID int64, workout *Workout) error { return err } -func GetUserInfo(chatID int64) (*ChatInfoDTO, error) { +func GetChatInfo(chatID int64) (*ChatInfoDTO, error) { if err := AddChat(chatID); err != nil { return nil, err } @@ -124,7 +127,7 @@ func GetUserInfo(chatID int64) (*ChatInfoDTO, error) { return chatInfoDTO, err } -func SetStatusInUser(chatID int64, status UserState) error { +func SetStatusToChat(chatID int64, status UserState) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() @@ -181,3 +184,31 @@ func GetStat(chatID int64) (map[string]int, error) { } return res, err } + +func GetAllChats() ([]int64, error) { + 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.ChatsCollectionName) + cursor, err := collection.Find(ctx,bson.M{}) + if err != nil { + return nil, err + } + var res []int64 + for cursor.Next(context.Background()) { + var result ChatInfoDTO + if err := cursor.Decode(&result); err != nil { + log.Fatal(err) + } + res = append(res, result.ChatID) + } + if err := cursor.Err(); err != nil { + log.Fatal(err) + } + return res, err +} diff --git a/main.go b/main.go index ed7cf42..b4a11e7 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github.com/umputun/go-flags" "io/ioutil" "log" + "net/http" "os" "strconv" "strings" @@ -70,6 +71,28 @@ func run() { panic(err) } + go func() { + http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) { + chats, err := db.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 { + sendGoToChat(bot, chatID) + } + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, `{"result":"ok"}`) + }) + + port := ":10002" + log.Println("Server is start up! port", port) + log.Fatal(http.ListenAndServe(port, nil)) + }() + log.Println("Run", opts.Name) for update := range updates { @@ -82,7 +105,7 @@ func run() { chatID := update.Message.Chat.ID username := update.Message.From.UserName - userInfoDTO, err := db.GetUserInfo(chatID) + userInfoDTO, err := db.GetChatInfo(chatID) if err != nil { log.Println(err) continue @@ -97,10 +120,14 @@ func run() { log.Println(err) continue } - msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text)) + msgText := fmt.Sprintf("Отлично, %s, записал.", text) + if count <= 0 { + msgText = "Плохо, хочешь быть толстым и не красивым?" + } + msg := tgbot.NewMessage(chatID, msgText) msg.ReplyMarkup = tgbot.NewHideKeyboard(false) _, _ = bot.Send(msg) - if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil { + if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil { log.Println(err) } continue @@ -110,7 +137,7 @@ 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 { + if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil { log.Println(err) } case commands.Help: @@ -118,21 +145,7 @@ func run() { case commands.Ping: _, _ = bot.Send(tgbot.NewMessage(chatID, "pong")) case commands.Go: - msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение") - msg.ReplyMarkup = tgbot.NewReplyKeyboard( - tgbot.NewKeyboardButtonRow( - 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) - } - } + sendGoToChat(bot, chatID) case commands.Stat: stat, err := db.GetStat(chatID) if err != nil { @@ -147,3 +160,21 @@ func run() { } } } + +func sendGoToChat(bot *tgbot.BotAPI, chatID int64) { + msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение") + msg.ReplyMarkup = tgbot.NewReplyKeyboard( + tgbot.NewKeyboardButtonRow( + 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.SetStatusToChat(chatID, db.UserStateGo); err != nil { + log.Println(err) + } + } +}