+6
-61
@@ -1,21 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"valera/internal/calories"
|
||||
"valera/internal/commands"
|
||||
"valera/internal/config"
|
||||
"valera/internal/db"
|
||||
"valera/internal/pause"
|
||||
|
||||
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/umputun/go-flags"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -28,71 +24,22 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
version = "v1.6.2"
|
||||
version = "v1.6.3"
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
var opts Opts
|
||||
|
||||
func readFile(filename string) (string, error) {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
str := string(b)
|
||||
return str, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
run()
|
||||
}
|
||||
|
||||
func run() {
|
||||
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
|
||||
p.SubcommandsOptional = true
|
||||
if _, err := p.Parse(); err != nil {
|
||||
if err.(*flags.Error).Type != flags.ErrHelp {
|
||||
log.Println(errors.New("[ERROR] cli error: " + err.Error()))
|
||||
}
|
||||
os.Exit(2)
|
||||
}
|
||||
cfg := config.NewAppConfig()
|
||||
|
||||
if opts.Token == "" {
|
||||
token, err := readFile("token.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
opts.Token = strings.ReplaceAll(token, "\n", "")
|
||||
}
|
||||
fmt.Println(opts.Token)
|
||||
|
||||
bot, err := tgbot.NewBotAPI(opts.Token)
|
||||
bot, err := tgbot.NewBotAPI(cfg.TgConfig.Token)
|
||||
if err != nil {
|
||||
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, err := db.NewDB(
|
||||
mongoURL,
|
||||
dbName,
|
||||
)
|
||||
dataBase, err := db.NewDB(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -150,8 +97,6 @@ func run() {
|
||||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}()
|
||||
|
||||
log.Println("Run", opts.Name)
|
||||
|
||||
for update := range updates {
|
||||
|
||||
if update.Message == nil {
|
||||
@@ -252,7 +197,7 @@ func run() {
|
||||
continue
|
||||
}
|
||||
|
||||
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
|
||||
command := commands.Command(text)
|
||||
switch command {
|
||||
case commands.Start:
|
||||
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
|
||||
|
||||
Reference in New Issue
Block a user