-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (105 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/rdner/q3-server-bot/pkg/events"
"github.com/rdner/q3-server-bot/pkg/rcon"
"github.com/rdner/q3-server-bot/pkg/telegram"
"github.com/sirupsen/logrus"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
isDebug bool
telegramToken string
telegramChatID string
q3ServerAddr string
q3Password string
)
flag.BoolVar(&isDebug, "debug", false, "Use this flag if you'd like to see detailed logging output")
flag.StringVar(&telegramToken, "telegram-token", "", "Bot token that you get when you create a Telegram bot. You can use 'TELEGRAM_TOKEN' environment variable instead.")
flag.StringVar(&telegramChatID, "telegram-chat-id", "", "Unique identifier for the target chat or username of the target channel (in the format @channelusername). You can use 'TELEGRAM_CHAT_ID' environment variable instead.")
flag.StringVar(&q3ServerAddr, "q3-server-addr", "", "Address of the quake server including port. You can use 'Q3_SERVER_ADDR' environment variable instead")
flag.StringVar(&q3Password, "q3-password", "", "The rcon password of the server. You can use 'Q3_PASSWORD' environment variable instead")
flag.Parse()
if isDebug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
if telegramToken == "" {
telegramToken = os.Getenv("TELEGRAM_TOKEN")
}
if telegramToken == "" {
fmt.Println("`telegram-token` cannot be empty")
flag.Usage()
os.Exit(1)
}
if telegramChatID == "" {
telegramChatID = os.Getenv("TELEGRAM_CHAT_ID")
}
if telegramChatID == "" {
fmt.Println("`telegram-chat-id` cannot be empty")
flag.Usage()
os.Exit(1)
}
if q3ServerAddr == "" {
q3ServerAddr = os.Getenv("Q3_SERVER_ADDR")
}
if q3ServerAddr == "" {
fmt.Println("`q3-server-addr` cannot be empty")
flag.Usage()
os.Exit(1)
}
if q3Password == "" {
q3Password = os.Getenv("Q3_PASSWORD")
}
if q3Password == "" {
fmt.Println("`q3-password` cannot be empty")
flag.Usage()
os.Exit(1)
}
logrus.Info("initializing...")
sender := rcon.NewSender(q3ServerAddr, q3Password)
em := events.NewManager(sender, 5*time.Second, 5*time.Second)
bot := telegram.NewServerEventsBot(
em,
telegramToken,
telegramChatID,
"https://api.telegram.org",
q3ServerAddr,
3*time.Second,
)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
logrus.Info("starting event capturing and sending messages...")
go func() {
err := bot.Start(ctx)
if err != nil {
logrus.Fatal(err)
}
}()
go func() {
err := em.StartCapturing(ctx)
if err != nil {
logrus.Fatal(err)
}
}()
<-c
logrus.Info("shutting down events...")
em.Close()
logrus.Info("shutting down the bot...")
bot.Close()
cancel()
logrus.Info("all stopped")
os.Exit(1)
}