-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
97 lines (90 loc) · 2.4 KB
/
app.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
package main
import (
"context"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"log"
"strings"
"time"
)
const (
highTempThreshold = 50.0
sleepDuration = 10 * time.Minute
)
type Application struct {
conf *Config
bot *bot.Bot
bootTime time.Time
tempLoader TemperatureLoader
}
func NewApplication(conf *Config) (*Application, error) {
permission := func(next bot.HandlerFunc) bot.HandlerFunc {
return func(ctx context.Context, bot *bot.Bot, update *models.Update) {
if update.Message != nil && conf.TargetId != update.Message.Chat.ID {
return
}
if update.CallbackQuery != nil && conf.TargetId != update.CallbackQuery.From.ID {
return
}
next(ctx, bot, update)
}
}
b, err := bot.New(
conf.Token,
bot.WithSkipGetMe(),
bot.WithMiddlewares(permission),
)
if err != nil {
return nil, err
}
return &Application{
conf: conf,
bot: b,
bootTime: time.Now(),
tempLoader: LoadSensorsTemperature,
}, nil
}
func (a *Application) sendTemperatureToTelegram(ctx context.Context, render func(*SensorsTemperature) string) {
temp, err := a.tempLoader()
if err != nil {
log.Printf("error getting system temp: %v", err)
return
}
log.Println(RenderLogMessage(temp))
var sb strings.Builder
sb.WriteString(render(temp))
sb.WriteString("\n\n")
sb.WriteString("Uptime: ")
sb.WriteString(time.Since(a.bootTime).String())
sb.WriteString(" ; Boot time: ")
sb.WriteString(a.bootTime.Format(time.DateTime))
_, err = a.bot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: a.conf.TargetId,
Text: sb.String(),
ParseMode: models.ParseModeHTML,
DisableNotification: temp.IsHigherThanThreshold(highTempThreshold),
})
if err != nil {
log.Printf("error sending message: %v", err)
}
}
func (a *Application) startMonitoring(ctx context.Context) {
for {
a.sendTemperatureToTelegram(ctx, RenderTableMessage)
time.Sleep(sleepDuration)
}
}
func (a *Application) startPolling(ctx context.Context) {
a.bot.RegisterHandler(bot.HandlerTypeMessageText, "/temp", bot.MatchTypePrefix, func(ctx context.Context, bot *bot.Bot, update *models.Update) {
a.sendTemperatureToTelegram(ctx, RenderTableMessage)
})
_, _ = a.bot.SetMyCommands(ctx, &bot.SetMyCommandsParams{
Commands: []models.BotCommand{
{
Command: "temp",
Description: "/temp - get system temperature",
},
},
})
a.bot.Start(ctx)
}