-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 1.32 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/Brawl345/tagesschau-eilbot/handler"
"github.com/Brawl345/tagesschau-eilbot/storage"
_ "github.com/joho/godotenv/autoload"
"gopkg.in/telebot.v3"
)
func main() {
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.Fatalln(err)
}
log.Printf("Logged in as @%s (%d)", bot.Me.Username, bot.Me.ID)
h := handler.Handler{
Bot: bot,
DB: db,
}
time.AfterFunc(5*time.Second, h.OnTimer)
bot.Handle("/help", h.OnHelp)
bot.Handle("/hilfe", h.OnHelp)
bot.Handle("/start", h.OnStart)
bot.Handle("/stop", h.OnStop)
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()
}