Skip to content

Commit

Permalink
feat: reformate text message and added ping pong cmd handler
Browse files Browse the repository at this point in the history
  • Loading branch information
OldTyT committed Nov 6, 2023
1 parent 996635f commit 0e88868
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
19 changes: 10 additions & 9 deletions internal/frigate/frigate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
45 changes: 43 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {

Check failure on line 78 in main.go

View workflow job for this annotation

GitHub Actions / Lint check

S1006: should use for {} instead of for true {} (gosimple)
FrigateEvents := frigate.GetEvents(FrigateEventsURL)
Expand Down

0 comments on commit 0e88868

Please sign in to comment.