-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (91 loc) · 2.89 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
100
101
102
103
104
105
106
107
108
109
110
package main
//go:generate gotext -srclang=en update -out=catalog_gen.go -lang=en,ru
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/FreeFeed/freefeed-tg-client/app"
"github.com/FreeFeed/freefeed-tg-client/store"
"github.com/davidmz/debug-log"
"github.com/davidmz/go-try"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
shutdownTimeout = 10 * time.Second
)
func main() {
defer try.Handle(func(err error) { log.Fatalln("Fatal error:", err) })
var (
tgToken string
tgTokenFile string
frfHost string
userAgent string
dataDir string
debugSources string
noContent bool
)
flag.StringVar(&tgToken, "token", "", "Telegram bot token")
flag.StringVar(&tgTokenFile, "token-file", "", "Path to the file with Telegram bot token")
flag.StringVar(&frfHost, "host", "freefeed.net", "FreeFeed API/frontend hostname")
flag.StringVar(&dataDir, "data", "data", "Data directory (must be writable)")
flag.StringVar(&userAgent, "ua",
"FreeFeedTelegramClient/1.0 (https://github.com/FreeFeed/freefeed-tg-client)",
"User-Agent for backend requests")
flag.StringVar(&debugSources, "debug", "", "Debug sources, set to '*' to see all messages")
flag.BoolVar(&noContent, "no-content", false, "Do not include post/comment content into the TG messages")
flag.Parse()
if tgToken == "" && tgTokenFile == "" {
fmt.Fprintf(flag.CommandLine.Output(), "Flags of %s:\n", filepath.Base(os.Args[0]))
fmt.Fprintf(flag.CommandLine.Output(), "/!\\ Eider -token or -token-file must be specified\n")
flag.PrintDefaults()
os.Exit(0)
}
if debugSources != "" {
os.Setenv("DEBUG", debugSources)
}
if tgToken == "" && tgTokenFile != "" {
tokenData := try.ItVal(os.ReadFile(tgTokenFile))
tgToken = strings.TrimSpace(string(tokenData))
}
debugLogger := debug.NewLogger("tg-client")
errorLogger := debug.NewLogger("tg-client:error")
tgbotapi.SetLogger(debug.NewLogger("tg-client:tgbot"))
debugLogger.Println("Starting BotAPI")
tgBot, err := tgbotapi.NewBotAPI(tgToken)
if err != nil {
try.Throw(fmt.Errorf("cannot start BotAPI: %w", err))
}
debugLogger.Printf("Bot authorized on account %s", tgBot.Self.UserName)
debugLogger.Printf("Starting application")
a := &app.App{
DebugLogger: debugLogger,
ErrorLogger: errorLogger,
Store: store.NewFsStore(dataDir),
TgAPI: tgBot,
FreeFeedHost: frfHost,
UserAgent: userAgent,
NoContent: noContent,
}
handleStopSignals(a.Close, debugLogger)
a.Start()
debugLogger.Println("Bye!")
}
func handleStopSignals(cancel func(), log debug.Logger) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
log.Println(s, "signal received, waiting for bot to exit")
time.AfterFunc(shutdownTimeout, func() {
log.Println("shutdown timeout, exiting forcefully")
os.Exit(1)
})
cancel()
}()
}