mv mongo config to secrets
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-03-13 02:38:48 +07:00
parent af0b834bf3
commit f066e01d21
8 changed files with 159 additions and 118 deletions

View File

@ -30,6 +30,10 @@ steps:
environment: environment:
TOKEN: TOKEN:
from_secret: bot_token from_secret: bot_token
MONGO_URL:
from_secret: mongo_url
DB_NAME:
from_secret: db_name
PASSWORD: PASSWORD:
from_secret: ssh_pass from_secret: ssh_pass
settings: settings:
@ -41,11 +45,13 @@ steps:
from_secret: ssh_key from_secret: ssh_key
port: port:
from_secret: ssh_port from_secret: ssh_port
envs: [ TOKEN,PASSWORD ] envs: [ TOKEN,MONGO_URL,DB_NAME,PASSWORD ]
command_timeout: 10s command_timeout: 10s
script: script:
- cd deploys/valera_tg_bot - cd deploys/valera_tg_bot
- echo $${TOKEN} > token.txt - echo $${TOKEN} > token.txt
- echo $${MONGO_URL} > mongo_url.txt
- echo $${DB_NAME} > "db_name.txt"
- echo $${PASSWORD} | sudo -S systemctl restart valera - echo $${PASSWORD} | sudo -S systemctl restart valera
trigger: trigger:

4
.gitignore vendored
View File

@ -26,4 +26,6 @@ go.work
bin bin
# bot # bot
token.txt token.txt
mongo_url.txt
db_name.txt

View File

@ -8,10 +8,10 @@ type Config struct {
CaloriesCollectionName string CaloriesCollectionName string
} }
func NewConfig() *Config { func NewConfig(mongoURl string, dbName string) *Config {
return &Config{ return &Config{
MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027", MongoURL: mongoURl,
DBName: "valera", DBName: dbName,
ChatsCollectionName: "chats", ChatsCollectionName: "chats",
WorkoutsCollectionName: "workouts", WorkoutsCollectionName: "workouts",
CaloriesCollectionName: "calories", CaloriesCollectionName: "calories",

22
db/calories.go Normal file
View File

@ -0,0 +1,22 @@
package db
import "time"
type Calories struct {
ChatID int64 `bson:"chat_id"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewCalories(
count int,
username string,
) *Calories {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Calories{
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
}
}

22
db/chat.go Normal file
View File

@ -0,0 +1,22 @@
package db
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
)
type UserState string
type Chat struct {
ChatID int64 `bson:"chat_id"`
}
type ChatInfo struct {
ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
}
func (c *ChatInfo) GetStatus() UserState {
return UserState(c.Status)
}

129
db/db.go
View File

@ -10,88 +10,33 @@ import (
"valera/config" "valera/config"
) )
const ( type DB struct {
UserStateNone = UserState("") cfg *config.Config
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
)
type UserState string
type chatDTO struct {
ChatID int64 `bson:"chat_id"`
} }
type ChatInfoDTO struct { func NewDB(
ChatID int64 `bson:"chat_id"` mongoURL string,
Status string `bson:"status"` dbName string,
} ) *DB {
return &DB{
func (c *ChatInfoDTO) GetStatus() UserState { cfg: config.NewConfig(mongoURL, dbName),
return UserState(c.Status)
}
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"`
}
func NewWorkout(
name string,
count int,
username string,
) *Workout {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Workout{
Name: name,
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
} }
} }
type Calories struct { func (db *DB) AddChat(chatID int64) error {
ChatID int64 `bson:"chat_id"`
Count int `bson:"count"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewCalories(
count int,
username string,
) *Calories {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Calories{
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
}
}
var cfg *config.Config
func init() {
cfg = config.NewConfig()
}
func AddChat(chatID int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return err return err
} }
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
result := collection.FindOne(ctx, bson.M{"chat_id": chatID}) result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
if result.Err() == mongo.ErrNoDocuments { if result.Err() == mongo.ErrNoDocuments {
if _, err := collection.InsertOne(ctx, &chatDTO{ChatID: chatID}); err != nil { if _, err := collection.InsertOne(ctx, &Chat{ChatID: chatID}); err != nil {
return err return err
} }
return nil return nil
@ -99,11 +44,11 @@ func AddChat(chatID int64) error {
return result.Err() return result.Err()
} }
func AddWorkout(chatID int64, workout *Workout) error { func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
if workout.Count <= 0 { if workout.Count <= 0 {
return nil return nil
} }
if err := AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
return err return err
} }
workout.ChatID = chatID workout.ChatID = chatID
@ -111,12 +56,12 @@ func AddWorkout(chatID int64, workout *Workout) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return err return err
} }
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
_, err = collection.InsertOne( _, err = collection.InsertOne(
ctx, ctx,
workout, workout,
@ -124,11 +69,11 @@ func AddWorkout(chatID int64, workout *Workout) error {
return err return err
} }
func AddCalories(chatID int64, calories *Calories) error { func (db *DB) AddCalories(chatID int64, calories *Calories) error {
if calories.Count <= 0 { if calories.Count <= 0 {
return nil return nil
} }
if err := AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
return err return err
} }
calories.ChatID = chatID calories.ChatID = chatID
@ -136,12 +81,12 @@ func AddCalories(chatID int64, calories *Calories) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return err return err
} }
collection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
_, err = collection.InsertOne( _, err = collection.InsertOne(
ctx, ctx,
calories, calories,
@ -149,36 +94,36 @@ func AddCalories(chatID int64, calories *Calories) error {
return err return err
} }
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) { func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) {
if err := AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
return nil, err return nil, err
} }
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return nil, err return nil, err
} }
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
chatInfoDTO := &ChatInfoDTO{} chatInfoDTO := &ChatInfo{}
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
return nil, err return nil, err
} }
return chatInfoDTO, err return chatInfoDTO, err
} }
func SetStatusToChat(chatID int64, status UserState) error { func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return err return err
} }
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
_, err = collection.UpdateOne( _, err = collection.UpdateOne(
ctx, ctx,
bson.M{"chat_id": chatID}, bson.M{"chat_id": chatID},
@ -187,14 +132,14 @@ func SetStatusToChat(chatID int64, status UserState) error {
return err return err
} }
func GetStat(chatID int64) (map[string]int, error) { func (db *DB) GetStat(chatID int64) (map[string]int, error) {
if err := AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
return nil, err return nil, err
} }
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -204,7 +149,7 @@ func GetStat(chatID int64) (map[string]int, error) {
res := map[string]int{} res := map[string]int{}
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.WorkoutsCollectionName)
cursor, err := collection.Find( cursor, err := collection.Find(
ctx, ctx,
bson.M{ bson.M{
@ -226,7 +171,7 @@ func GetStat(chatID int64) (map[string]int, error) {
log.Fatal(err) log.Fatal(err)
} }
caloriesCollection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName) caloriesCollection := client.Database(db.cfg.DBName).Collection(db.cfg.CaloriesCollectionName)
caloriesCursor, err := caloriesCollection.Find( caloriesCursor, err := caloriesCollection.Find(
ctx, ctx,
bson.M{ bson.M{
@ -251,23 +196,23 @@ func GetStat(chatID int64) (map[string]int, error) {
return res, err return res, err
} }
func GetAllChats() ([]int64, error) { func (db *DB) GetAllChats() ([]int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(db.cfg.MongoURL))
if err != nil { if err != nil {
return nil, err return nil, err
} }
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) collection := client.Database(db.cfg.DBName).Collection(db.cfg.ChatsCollectionName)
cursor, err := collection.Find(ctx, bson.M{}) cursor, err := collection.Find(ctx, bson.M{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
var res []int64 var res []int64
for cursor.Next(context.Background()) { for cursor.Next(context.Background()) {
var result ChatInfoDTO var result ChatInfo
if err := cursor.Decode(&result); err != nil { if err := cursor.Decode(&result); err != nil {
log.Fatal(err) log.Fatal(err)
} }

25
db/workout.go Normal file
View File

@ -0,0 +1,25 @@
package db
import "time"
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"`
}
func NewWorkout(
name string,
count int,
username string,
) *Workout {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Workout{
Name: name,
Count: count,
Username: username,
CreatedAt: time.Now().In(loc),
}
}

61
main.go
View File

@ -16,7 +16,7 @@ import (
) )
const ( const (
version = "v1.2.0" version = "v1.3.0"
) )
type Opts struct { type Opts struct {
@ -26,8 +26,8 @@ type Opts struct {
var opts Opts var opts Opts
func readToken() (string, error) { func readFile(filename string) (string, error) {
b, err := ioutil.ReadFile("token.txt") b, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -50,7 +50,7 @@ func run() {
} }
if opts.Token == "" { if opts.Token == "" {
token, err := readToken() token, err := readFile("token.txt")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -63,6 +63,25 @@ func run() {
panic(err) panic(err)
} }
mongoURL, err := readFile("mongo_url.txt")
if err != nil {
panic(err)
}
mongoURL = strings.ReplaceAll(mongoURL, "\n", "")
fmt.Println(mongoURL)
dbName, err := readFile("db_name.txt")
if err != nil {
panic(err)
}
dbName = strings.ReplaceAll(dbName, "\n", "")
fmt.Println(dbName)
dataBase := db.NewDB(
mongoURL,
dbName,
)
u := tgbot.NewUpdate(0) u := tgbot.NewUpdate(0)
u.Timeout = 60 u.Timeout = 60
@ -73,7 +92,7 @@ func run() {
go func() { go func() {
http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) {
chats, err := db.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")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -81,7 +100,7 @@ func run() {
return return
} }
for _, chatID := range chats { for _, chatID := range chats {
sendGoToChat(bot, chatID) sendGoToChat(bot, dataBase, chatID)
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -89,7 +108,7 @@ func run() {
}) })
http.HandleFunc("/stat", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/stat", func(w http.ResponseWriter, r *http.Request) {
chats, err := db.GetAllChats() chats, err := dataBase.GetAllChats()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
@ -98,7 +117,7 @@ func run() {
return return
} }
for _, chatID := range chats { for _, chatID := range chats {
if err := sendStatToChat(bot, chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\n"); err != nil { if err := sendStatToChat(bot, dataBase, chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\n"); err != nil {
fmt.Println(err) fmt.Println(err)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -128,7 +147,7 @@ func run() {
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
username := update.Message.From.UserName username := update.Message.From.UserName
userInfoDTO, err := db.GetChatInfo(chatID) userInfoDTO, err := dataBase.GetChatInfo(chatID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
@ -140,7 +159,7 @@ func run() {
log.Println(err) log.Println(err)
continue continue
} }
if err := db.AddWorkout(chatID, db.NewWorkout("Отжимания", count, username)); err != nil { if err := dataBase.AddWorkout(chatID, db.NewWorkout("Отжимания", count, username)); err != nil {
log.Println(err) log.Println(err)
continue continue
} }
@ -151,7 +170,7 @@ func run() {
msg := tgbot.NewMessage(chatID, msgText) msg := tgbot.NewMessage(chatID, msgText)
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false) msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = bot.Send(msg) _, _ = bot.Send(msg)
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err) log.Println(err)
} }
continue continue
@ -161,7 +180,7 @@ func run() {
log.Println(err) log.Println(err)
continue continue
} }
if err := db.AddCalories(chatID, db.NewCalories(count, username)); err != nil { if err := dataBase.AddCalories(chatID, db.NewCalories(count, username)); err != nil {
log.Println(err) log.Println(err)
continue continue
} }
@ -170,7 +189,7 @@ func run() {
continue continue
} }
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %s, записал.", text))) _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %s, записал.", text)))
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err) log.Println(err)
} }
continue continue
@ -180,7 +199,7 @@ func run() {
switch command { switch command {
case commands.Start: case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID))) _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err) log.Println(err)
} }
case commands.Help: case commands.Help:
@ -188,15 +207,15 @@ func run() {
case commands.Ping: case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong")) _, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go: case commands.Go:
sendGoToChat(bot, chatID) sendGoToChat(bot, dataBase, chatID)
case commands.Stat: case commands.Stat:
if err := sendStatToChat(bot, chatID, ""); err != nil { if err := sendStatToChat(bot, dataBase, chatID, ""); err != nil {
fmt.Println(err) fmt.Println(err)
continue 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 := db.SetStatusToChat(chatID, db.UserStateEat); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateEat); err != nil {
log.Println(err) log.Println(err)
} }
} }
@ -204,7 +223,7 @@ func run() {
} }
} }
func sendGoToChat(bot *tgbot.BotAPI, chatID int64) { func sendGoToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64) {
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение") msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение")
msg.ReplyMarkup = tgbot.NewReplyKeyboard( msg.ReplyMarkup = tgbot.NewReplyKeyboard(
tgbot.NewKeyboardButtonRow( tgbot.NewKeyboardButtonRow(
@ -216,14 +235,14 @@ func sendGoToChat(bot *tgbot.BotAPI, chatID int64) {
), ),
) )
if _, err := bot.Send(msg); err == nil { if _, err := bot.Send(msg); err == nil {
if err := db.SetStatusToChat(chatID, db.UserStateGo); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateGo); err != nil {
log.Println(err) log.Println(err)
} }
} }
} }
func sendStatToChat(bot *tgbot.BotAPI, chatID int64, prefix string) error { func sendStatToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64, prefix string) error {
stat, err := db.GetStat(chatID) stat, err := dataBase.GetStat(chatID)
if err != nil { if err != nil {
return err return err
} }