valera/main.go

130 lines
3.0 KiB
Go
Raw Normal View History

2023-03-11 08:35:23 +00:00
package main
2023-03-11 09:00:25 +00:00
import (
"errors"
"fmt"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
"io/ioutil"
"log"
"os"
2023-03-11 10:17:30 +00:00
"strconv"
2023-03-11 09:00:25 +00:00
"strings"
"valera/commands"
2023-03-11 10:17:30 +00:00
"valera/db"
2023-03-11 09:00:25 +00:00
)
2023-03-11 08:35:23 +00:00
2023-03-11 08:37:46 +00:00
const (
2023-03-11 10:17:30 +00:00
version = "v1.0.0"
2023-03-11 08:37:46 +00:00
)
2023-03-11 09:34:31 +00:00
2023-03-11 09:00:25 +00:00
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 readToken() (string, error) {
b, err := ioutil.ReadFile("token.txt")
if err != nil {
return "", err
}
str := string(b)
return str, nil
}
2023-03-11 08:35:23 +00:00
func main() {
2023-03-11 09:00:25 +00:00
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)
}
if opts.Token == "" {
token, err := readToken()
if err != nil {
panic(err)
}
opts.Token = strings.ReplaceAll(token, "\n", "")
}
fmt.Println(opts.Token)
bot, err := tgbot.NewBotAPI(opts.Token)
if err != nil {
panic(err)
}
u := tgbot.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
panic(err)
}
log.Println("Run", opts.Name)
for update := range updates {
if update.Message == nil {
continue
}
text := update.Message.Text
chatID := update.Message.Chat.ID
2023-03-11 09:34:31 +00:00
// username := update.Message.From.UserName
2023-03-11 10:30:38 +00:00
userInfoDTO, err := db.GetUserInfo(chatID)
if err != nil {
log.Println(err)
continue
}
if userInfoDTO.GetStatus() == db.UserStateGo {
2023-03-11 10:17:30 +00:00
count, err := strconv.Atoi(text)
if err != nil {
continue
}
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)
2023-03-11 10:30:38 +00:00
db.SetStatusInUser(chatID, db.UserStateNone)
2023-03-11 09:34:31 +00:00
continue
}
2023-03-11 09:00:25 +00:00
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
switch command {
case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
case commands.Help:
2023-03-11 09:34:31 +00:00
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться"))
2023-03-11 09:00:25 +00:00
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
2023-03-11 09:34:31 +00:00
case commands.Go:
2023-03-11 10:17:30 +00:00
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение")
2023-03-11 09:34:31 +00:00
msg.ReplyMarkup = tgbot.NewReplyKeyboard(
tgbot.NewKeyboardButtonRow(
tgbot.NewKeyboardButton("1"),
tgbot.NewKeyboardButton("2"),
tgbot.NewKeyboardButton("3"),
),
)
if _, err = bot.Send(msg); err == nil {
2023-03-11 10:30:38 +00:00
db.SetStatusInUser(chatID, db.UserStateGo)
2023-03-11 09:34:31 +00:00
}
2023-03-11 09:00:25 +00:00
}
}
2023-03-11 08:35:23 +00:00
}