diff --git a/internal/frigate/frigate.go b/internal/frigate/frigate.go index 6b50b2a..8599f5f 100644 --- a/internal/frigate/frigate.go +++ b/internal/frigate/frigate.go @@ -176,20 +176,20 @@ func SendMessageEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) { conf := config.New() // Prepare text message - text := "" - text = text + "Camera: " + FrigateEvent.Camera + "\n" - text = text + "Label: " + FrigateEvent.Label + "\n" + text := "*Event*\n" + text = text + "┣*Camera*\n┗ `" + FrigateEvent.Camera + "`\n" + text = text + "┣*Label*\n┗ `" + FrigateEvent.Label + "`\n" t_start := time.Unix(int64(FrigateEvent.StartTime), 0) - text = text + fmt.Sprintf("Start time: %s", t_start) + "\n" + text = text + fmt.Sprintf("┣*Start time*\n┗ `%s", t_start) + "`\n" if FrigateEvent.EndTime == 0 { - text = text + "End time: In progess" + "\n" + text = text + "┣*End time*\n┗ `In progess`" + "\n" } else { t_end := time.Unix(int64(FrigateEvent.EndTime), 0) - text = text + fmt.Sprintf("End time: %s", t_end) + "\n" + text = text + fmt.Sprintf("┣*End time*\n┗ `%s", t_end) + "`\n" } - text = text + fmt.Sprintf("Top score: %f", (FrigateEvent.TopScore*100)) + "%\n" - text = text + "Event id: " + FrigateEvent.ID + "\n" - text = text + conf.FrigateExternalURL + "/events?cameras=" + FrigateEvent.Camera + "&labels=" + FrigateEvent.Label + text = text + fmt.Sprintf("┣*Top score*\n┗ `%f", (FrigateEvent.TopScore*100)) + "%`\n" + text = text + "┣*Event id*\n┗ `" + FrigateEvent.ID + "`\n" + text = text + "┣*Event URL*\n┗ " + conf.FrigateExternalURL + "/events?cameras=" + FrigateEvent.Camera + "&labels=" + FrigateEvent.Label // Save thumbnail FilePathThumbnail := SaveThumbnail(FrigateEvent.Thumbnail) @@ -198,6 +198,7 @@ func SendMessageEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) { var medias []interface{} MediaThumbnail := tgbotapi.NewInputMediaPhoto(tgbotapi.FilePath(FilePathThumbnail)) MediaThumbnail.Caption = text + MediaThumbnail.ParseMode = tgbotapi.ModeMarkdown medias = append(medias, MediaThumbnail) if FrigateEvent.HasClip && FrigateEvent.EndTime != 0 { diff --git a/main.go b/main.go index a9e61aa..85c18ae 100644 --- a/main.go +++ b/main.go @@ -6,14 +6,52 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/oldtyt/frigate-telegram/internal/config" - "github.com/oldtyt/frigate-telegram/internal/log" "github.com/oldtyt/frigate-telegram/internal/frigate" + "github.com/oldtyt/frigate-telegram/internal/log" ) - var FrigateEvents frigate.EventsStruct var FrigateEvent frigate.EventStruct +func PongBot(bot *tgbotapi.BotAPI) { + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message == nil { // ignore any non-Message updates + continue + } + + if !update.Message.IsCommand() { // ignore any non-command Messages + continue + } + + // Create a new MessageConfig. We don't have text yet, + // so we leave it empty. + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") + + // Extract the command from the Message. + switch update.Message.Command() { + case "help": + msg.Text = "I understand /ping." + case "ping": + msg.Text = "pong" + case "pong": + msg.Text = "ping" + case "status": + msg.Text = "I'm ok." + default: + msg.Text = "I don't know that command" + } + + if _, err := bot.Send(msg); err != nil { + log.Error.Fatalln("Error sending message: " + err.Error()) + } + } +} + func main() { // Initializing logger log.LogFunc() @@ -33,6 +71,9 @@ func main() { FrigateEventsURL := conf.FrigateURL + "/api/events" + // Starting ping command handler(healthcheck) + go PongBot(bot) + // Starting loop for getting events from Frigate for true { FrigateEvents := frigate.GetEvents(FrigateEventsURL)