Skip to content

Commit

Permalink
Merge pull request #6 from OldTyT/5-entitiy-too-large
Browse files Browse the repository at this point in the history
fix: bug with entity to large
  • Loading branch information
OldTyT authored Feb 3, 2024
2 parents b140c9f + a39fd6a commit b152a0b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
19 changes: 8 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package config
import (
"os"
"strconv"
"strings"
)

const Version string = "v0.0.1"

type Config struct {
Debug bool
TelegramBotToken string
Expand Down Expand Up @@ -87,14 +84,14 @@ func getEnvAsBool(name string, defaultVal bool) bool {
}

// Helper to read an environment variable into a string slice or return default value
func getEnvAsSlice(name string, defaultVal []string, sep string) []string {
valStr := getEnv(name, "")
// func getEnvAsSlice(name string, defaultVal []string, sep string) []string {
// valStr := getEnv(name, "")

if valStr == "" {
return defaultVal
}
// if valStr == "" {
// return defaultVal
// }

val := strings.Split(valStr, sep)
// val := strings.Split(valStr, sep)

return val
}
// return val
// }
25 changes: 15 additions & 10 deletions internal/frigate/frigate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -74,8 +72,6 @@ type EventStruct struct {

var Events EventsStruct
var Event EventStruct
var floatType = reflect.TypeOf(float64(0))
var stringType = reflect.TypeOf("")

func GETZones(Zones []any) []string {
var my_zones []string
Expand Down Expand Up @@ -144,7 +140,7 @@ func GetEvents(FrigateURL string, bot *tgbotapi.BotAPI, SetBefore bool) EventsSt
}

// Read data from response
byteValue, err := ioutil.ReadAll(resp.Body)
byteValue, err := io.ReadAll(resp.Body)
if err != nil {
ErrorSend("Can't read JSON: "+err.Error(), bot, "ALL")
}
Expand Down Expand Up @@ -222,7 +218,8 @@ func SendMessageEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) {
text += "┣*Event id*\n┗ `" + FrigateEvent.ID + "`\n"
text += "┣*Zones*\n┗ `" + strings.Join(GETZones(FrigateEvent.Zones), ", ") + "`\n"
text += "[Events URL](" + conf.FrigateExternalURL + "/events?cameras=" + FrigateEvent.Camera + "&labels=" + FrigateEvent.Label + "&zones=" + strings.Join(GETZones(FrigateEvent.Zones), ",") + ")\n"
text += "[General URL](" + conf.FrigateExternalURL
text += "[General URL](" + conf.FrigateExternalURL + "\n"
text += "[Source clip](" + conf.FrigateExternalURL + "/api/events/" + FrigateEvent.ID + "/clip.mp4)\n"

// Save thumbnail
FilePathThumbnail := SaveThumbnail(FrigateEvent.ID, FrigateEvent.Thumbnail, bot)
Expand All @@ -239,9 +236,17 @@ func SendMessageEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) {
FilePathClip := SaveClip(FrigateEvent.ID, bot)
defer os.Remove(FilePathClip)

// Add clip to media group
MediaClip := tgbotapi.NewInputMediaVideo(tgbotapi.FilePath(FilePathClip))
medias = append(medias, MediaClip)
videoInfo, err := os.Stat(FilePathClip)
if err != nil {
ErrorSend("Error receiving information about the clip file: "+err.Error(), bot, FrigateEvent.ID)
}

if videoInfo.Size() < 52428800 {
// Telegram don't send large file see for more: https://github.com/OldTyT/frigate-telegram/issues/5
// Add clip to media group
MediaClip := tgbotapi.NewInputMediaVideo(tgbotapi.FilePath(FilePathClip))
medias = append(medias, MediaClip)
}
}

// Create message
Expand Down Expand Up @@ -304,7 +309,7 @@ func SendTextEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) {

func NotifyEvents(bot *tgbotapi.BotAPI, FrigateEventsURL string) {
conf := config.New()
for true {
for {
FrigateEvents := GetEvents(FrigateEventsURL, bot, false)
ParseEvents(FrigateEvents, bot, true)
time.Sleep(time.Duration(conf.WatchDogSleepTime) * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package log

import (
"io/ioutil"
"io"
"log"
"os"

Expand All @@ -18,7 +18,7 @@ var (

func LogFunc() {
conf := config.New()
Trace = log.New(ioutil.Discard, "TRACE: ", log.Ldate|log.Ltime|log.Lshortfile)
Trace = log.New(io.Discard, "TRACE: ", log.Ldate|log.Ltime|log.Lshortfile)
file, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
Expand Down
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (
"github.com/oldtyt/frigate-telegram/internal/log"
)

// FrigateEvents is frigate events struct
var FrigateEvents frigate.EventsStruct

// FrigateEvent is frigate event struct
var FrigateEvent frigate.EventStruct

// PongBot is needed to check the work of the bot.
func PongBot(bot *tgbotapi.BotAPI) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
Expand Down Expand Up @@ -59,10 +63,9 @@ func main() {
conf := config.New()

// Prepare startup msg
startup_msg := "Starting frigate-telegram.\n"
startup_msg += "Version: " + config.Version + "\n"
startup_msg += "Frigate URL: " + conf.FrigateURL + "\n"
log.Info.Println(startup_msg)
startupMsg := "Starting frigate-telegram.\n"
startupMsg += "Frigate URL: " + conf.FrigateURL + "\n"
log.Info.Println(startupMsg)

// Initializing telegram bot
bot, err := tgbotapi.NewBotAPI(conf.TelegramBotToken)
Expand All @@ -73,7 +76,7 @@ func main() {
log.Info.Println("Authorized on account " + bot.Self.UserName)

// Send startup msg.
_, errmsg := bot.Send(tgbotapi.NewMessage(conf.TelegramChatID, startup_msg))
_, errmsg := bot.Send(tgbotapi.NewMessage(conf.TelegramChatID, startupMsg))
if errmsg != nil {
log.Error.Println(errmsg.Error())
}
Expand All @@ -87,7 +90,7 @@ func main() {
go frigate.NotifyEvents(bot, FrigateEventsURL)
}
// Starting loop for getting events from Frigate
for true {
for {
FrigateEvents := frigate.GetEvents(FrigateEventsURL, bot, true)
frigate.ParseEvents(FrigateEvents, bot, false)
time.Sleep(time.Duration(conf.SleepTime) * time.Second)
Expand Down

0 comments on commit b152a0b

Please sign in to comment.