-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
99 lines (81 loc) · 2.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/oldtyt/frigate-telegram/internal/config"
"github.com/oldtyt/frigate-telegram/internal/frigate"
"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
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()
// Get config
conf := config.New()
// Prepare 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)
if err != nil {
log.Error.Fatalln("Error initalizing telegram bot: " + err.Error())
}
bot.Debug = conf.Debug
log.Info.Println("Authorized on account " + bot.Self.UserName)
// Send startup msg.
_, errmsg := bot.Send(tgbotapi.NewMessage(conf.TelegramChatID, startupMsg))
if errmsg != nil {
log.Error.Println(errmsg.Error())
}
// Starting ping command handler(healthcheck)
go PongBot(bot)
FrigateEventsURL := conf.FrigateURL + "/api/events"
if conf.SendTextEvent {
go frigate.NotifyEvents(bot, FrigateEventsURL)
}
// Starting loop for getting events from Frigate
for {
FrigateEvents := frigate.GetEvents(FrigateEventsURL, bot, true)
frigate.ParseEvents(FrigateEvents, bot, false)
time.Sleep(time.Duration(conf.SleepTime) * time.Second)
log.Debug.Println("Sleeping for " + strconv.Itoa(conf.SleepTime) + " seconds.")
}
}