-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (90 loc) · 2.26 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
package main
import (
"os/signal"
"strconv"
"syscall"
"time"
"github.com/Brawl345/rssbot/config"
"github.com/Brawl345/rssbot/handler"
_ "github.com/joho/godotenv/autoload"
"gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/middleware"
"log"
"os"
"github.com/Brawl345/rssbot/storage"
)
func main() {
tmpl, err := config.GetTemplate("post.gohtml")
if err != nil {
log.Fatal("Invalid template: ", err)
}
cfg := &config.Config{
Template: tmpl,
}
db, err := storage.Connect()
if err != nil {
log.Fatal(err)
}
log.Println("Database connection established")
n, err := db.Migrate()
if err != nil {
log.Fatalln(err)
}
if n > 0 {
log.Printf("Applied %d migration(s)", n)
}
pref := telebot.Settings{
Token: os.Getenv("BOT_TOKEN"),
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
}
bot, err := telebot.NewBot(pref)
if err != nil {
log.Fatal(err)
}
log.Printf("Logged in as @%s (%d)", bot.Me.Username, bot.Me.ID)
h := handler.Handler{
Bot: bot,
Config: cfg,
DB: db,
}
adminId, err := strconv.ParseInt(os.Getenv("ADMIN_ID"), 10, 64)
if err != nil {
// No admin = unsupported.
// Else we would constantly have to check if the user is also
// a member of the channel if feeds should be posted in a channel.
log.Fatalln("ADMIN_ID is missing.")
} else {
bot.Use(middleware.Whitelist(adminId))
}
bot.Handle("/start", h.OnStart)
bot.Handle("/help", h.OnStart)
bot.Handle("/sub", h.OnSubscribe)
bot.Handle("/del", h.OnUnsubscribe)
bot.Handle("/unsub", h.OnUnsubscribe)
bot.Handle("/feeds", h.OnList)
bot.Handle("/rss", h.OnList)
bot.Handle("/feeds_all", h.OnListAll)
bot.Handle("/rss_all", h.OnListAll)
bot.Handle("/repl_add", h.OnAddReplacement)
bot.Handle("/repl_add_re", h.OnAddRegexReplacement)
bot.Handle("/repl_list", h.OnListReplacements)
bot.Handle("/repl_del", h.OnDeleteReplacement)
time.AfterFunc(5*time.Second, h.OnCheck)
channel := make(chan os.Signal)
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
signal.Notify(channel, os.Interrupt, syscall.SIGKILL)
signal.Notify(channel, os.Interrupt, syscall.SIGINT)
go func() {
<-channel
log.Println("Stopping...")
bot.Stop()
err := db.Close()
if err != nil {
log.Println(err)
os.Exit(1)
return
}
os.Exit(0)
}()
bot.Start()
}