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

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
+40 -21
View File
@@ -16,7 +16,7 @@ import (
)
const (
version = "v1.2.0"
version = "v1.3.0"
)
type Opts struct {
@@ -26,8 +26,8 @@ type Opts struct {
var opts Opts
func readToken() (string, error) {
b, err := ioutil.ReadFile("token.txt")
func readFile(filename string) (string, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
@@ -50,7 +50,7 @@ func run() {
}
if opts.Token == "" {
token, err := readToken()
token, err := readFile("token.txt")
if err != nil {
panic(err)
}
@@ -63,6 +63,25 @@ func run() {
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.Timeout = 60
@@ -73,7 +92,7 @@ func run() {
go func() {
http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) {
chats, err := db.GetAllChats()
chats, err := dataBase.GetAllChats()
if err != nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
@@ -81,7 +100,7 @@ func run() {
return
}
for _, chatID := range chats {
sendGoToChat(bot, chatID)
sendGoToChat(bot, dataBase, chatID)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
@@ -89,7 +108,7 @@ func run() {
})
http.HandleFunc("/stat", func(w http.ResponseWriter, r *http.Request) {
chats, err := db.GetAllChats()
chats, err := dataBase.GetAllChats()
if err != nil {
fmt.Println(err)
w.Header().Add("Content-Type", "application/json")
@@ -98,7 +117,7 @@ func run() {
return
}
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)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
@@ -128,7 +147,7 @@ func run() {
chatID := update.Message.Chat.ID
username := update.Message.From.UserName
userInfoDTO, err := db.GetChatInfo(chatID)
userInfoDTO, err := dataBase.GetChatInfo(chatID)
if err != nil {
log.Println(err)
continue
@@ -140,7 +159,7 @@ func run() {
log.Println(err)
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)
continue
}
@@ -151,7 +170,7 @@ func run() {
msg := tgbot.NewMessage(chatID, msgText)
msg.ReplyMarkup = tgbot.NewRemoveKeyboard(false)
_, _ = bot.Send(msg)
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil {
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
continue
@@ -161,7 +180,7 @@ func run() {
log.Println(err)
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)
continue
}
@@ -170,7 +189,7 @@ func run() {
continue
}
_, _ = 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)
}
continue
@@ -180,7 +199,7 @@ func run() {
switch command {
case commands.Start:
_, _ = 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)
}
case commands.Help:
@@ -188,15 +207,15 @@ func run() {
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go:
sendGoToChat(bot, chatID)
sendGoToChat(bot, dataBase, chatID)
case commands.Stat:
if err := sendStatToChat(bot, chatID, ""); err != nil {
if err := sendStatToChat(bot, dataBase, chatID, ""); err != nil {
fmt.Println(err)
continue
}
case commands.Eat:
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)
}
}
@@ -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.ReplyMarkup = tgbot.NewReplyKeyboard(
tgbot.NewKeyboardButtonRow(
@@ -216,14 +235,14 @@ func sendGoToChat(bot *tgbot.BotAPI, chatID int64) {
),
)
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)
}
}
}
func sendStatToChat(bot *tgbot.BotAPI, chatID int64, prefix string) error {
stat, err := db.GetStat(chatID)
func sendStatToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64, prefix string) error {
stat, err := dataBase.GetStat(chatID)
if err != nil {
return err
}