update calories
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-14 01:15:11 +07:00
parent 922e770405
commit 01e132628c
2 changed files with 84 additions and 2 deletions
+30 -2
View File
@@ -20,6 +20,10 @@ var (
"Отжимания",
"Пресс",
}
caloriesMap = map[string]int{
"чай": 79,
"яблоко": 100,
}
)
const (
@@ -198,7 +202,7 @@ func run() {
_, _ = bot.Send(msg)
continue
case db.UserStateEat:
count, err := strconv.Atoi(text)
count, err := calcCalories(text)
if err != nil {
log.Println(err)
continue
@@ -211,7 +215,7 @@ func run() {
_, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой"))
continue
}
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %s, записал.", text)))
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %d, записал.", count)))
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
@@ -246,6 +250,30 @@ func run() {
}
}
func calcCalories(text string) (int, error) {
count, ok := caloriesMap[strings.ToLower(text)]
if ok {
return count, nil
}
arr := strings.Split(text, " ")
if len(arr) == 2 {
coun1, err := strconv.Atoi(arr[0])
if err != nil {
return 0, nil
}
count2, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return coun1 * count2 / 100, nil
}
count, err := strconv.Atoi(text)
if err != nil {
return 0, nil
}
return count, nil
}
func sendGoToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64) {
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, выбирай:")
row := tgbot.NewKeyboardButtonRow()