heating-monitor/internal/bot/controller.go
2024-11-28 13:52:58 +01:00

49 lines
1.1 KiB
Go

package bot
import (
"log"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"gorm.io/gorm"
)
func ProcesarMensaje(update tgbotapi.Update, db *gorm.DB) {
if update.Message == nil || update.Message.Text == "" {
return
}
parts := strings.Fields(update.Message.Text)
command := strings.ToUpper(parts[0])
var timestamp time.Time
if len(parts) == 1 {
timestamp = update.Message.Time()
} else {
parsedTime, err := time.Parse("15:04", parts[1])
if err != nil {
log.Println("Formato de hora inválido:", parts[1])
return
}
now := time.Now()
timestamp = time.Date(now.Year(), now.Month(), now.Day(), parsedTime.Hour(), parsedTime.Minute(), 0, 0, now.Location())
}
if command != "ON" && command != "OFF" {
log.Println("Comando desconocido:", command)
return
}
event := HeatingEvent{
EventType: command,
Timestamp: timestamp,
}
if err := db.Create(&event).Error; err != nil {
log.Printf("Error al guardar el evento %s: %v", command, err)
return
}
log.Printf("Evento %s registrado con éxito a las %s", command, timestamp.Format("15:04"))
}