-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram2mqtt.go
67 lines (55 loc) · 1.43 KB
/
telegram2mqtt.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
package main
import (
_ "embed"
"fmt"
"os"
"os/signal"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
APPNAME string = "Telegram2Mqtt"
TELEGRAM_TOPIC string = APPNAME + "/telegram/"
)
var (
//go:embed version.txt
VERSION string
config Config
)
func init() {
// Setup logging
out := zerolog.NewConsoleWriter()
out.NoColor = true
out.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("%-6s", i))
}
out.PartsExclude = []string{zerolog.TimestampFieldName, zerolog.CallerFieldName}
log.Logger = log.With().Caller().Logger()
log.Logger = log.Output(out)
switch strings.ToLower(os.Getenv("LOGLEVEL")) {
case "debug":
log.Logger = log.Level(zerolog.DebugLevel)
case "trace":
log.Logger = log.Level(zerolog.TraceLevel)
default:
log.Logger = log.Level(zerolog.InfoLevel)
}
// Get Config
config = getConfig()
// Print Version
log.Info().Msgf("%s %s", APPNAME, strings.TrimSpace(VERSION))
}
func main() {
zoneName, _ := time.Now().Zone()
log.Debug().Msgf("%s start, Local Time=%s Timezone=%s", APPNAME, time.Now().Local().Format("15:04:05"), zoneName)
startMqttClient()
StartTelegramClient()
// Refresh status
sendToMttRetain(APPNAME+"/status", "Online")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
<-sigChan
log.Debug().Msgf("%s stop, Local Time=%s Timezone=%s", APPNAME, time.Now().Local().Format("15:04:05"), zoneName)
}