-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (85 loc) · 3.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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/fatih/color"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/theovidal/parcolar/info"
"github.com/theovidal/parcolar/lib"
"github.com/theovidal/parcolar/math"
"github.com/theovidal/parcolar/pronote"
"github.com/theovidal/parcolar/wolfram"
)
func main() {
color.Green(" ________ ________ ________ ________ ________ ___ ________ ________ \n|\\ __ \\|\\ __ \\|\\ __ \\|\\ ____\\|\\ __ \\|\\ \\ |\\ __ \\|\\ __ \\ \n\\ \\ \\|\\ \\ \\ \\|\\ \\ \\ \\|\\ \\ \\ \\___|\\ \\ \\|\\ \\ \\ \\ \\ \\ \\|\\ \\ \\ \\|\\ \\ \n \\ \\ ____\\ \\ __ \\ \\ _ _\\ \\ \\ \\ \\ \\\\\\ \\ \\ \\ \\ \\ __ \\ \\ _ _\\ \n \\ \\ \\___|\\ \\ \\ \\ \\ \\ \\\\ \\\\ \\ \\____\\ \\ \\\\\\ \\ \\ \\____\\ \\ \\ \\ \\ \\ \\\\ \\| \n \\ \\__\\ \\ \\__\\ \\__\\ \\__\\\\ _\\\\ \\_______\\ \\_______\\ \\_______\\ \\__\\ \\__\\ \\__\\\\ _\\ \n \\|__| \\|__|\\|__|\\|__|\\|__|\\|_______|\\|_______|\\|_______|\\|__|\\|__|\\|__|\\|__|")
lib.LoadEnv(".env")
lib.OpenCache()
lib.OpenDirs()
defer os.RemoveAll(lib.TempDir)
api, err := telegram.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Panic(err)
}
bot := &lib.Bot{
BotAPI: api,
Commands: map[string]lib.Command{
// ―――――― Default ――――――
"help": HelpCommand(),
"start": HelpCommand(),
// ―――――― Information ――――――
"definition": info.DefinitionCommand(),
"translate": info.TranslateCommand(),
"wordreference": info.WordReferenceCommand(),
"wolfram": wolfram.Command(),
// ―――――― Mathematics ――――――
"calc": math.CalcCommand(),
"latex": math.LatexCommand(),
"plot": math.PlotCommand(),
// ―――――― PRONOTE ――――――
"contents": pronote.ContentsCommand(),
"homework": pronote.HomeworkCommand(),
"timetable": pronote.TimetableCommand(),
"timetablechart": pronote.TimetableChartCommand(),
},
Cache: lib.OpenCache(),
}
if os.Getenv("DEBUG") == "true" {
bot.Debug = true
lib.StandardLogger.Debug = true
}
lib.LogSuccess("Authorized on account %s", bot.Self.UserName)
go pronote.TimetableLoop(bot)
updateChannel := telegram.NewUpdate(0)
updateChannel.Timeout = 60
updates, err := bot.GetUpdatesChan(updateChannel)
go func() {
for update := range updates {
if update.InlineQuery != nil {
err = info.ParcoursupCommand(bot, &update)
if err != nil {
lib.LogError("Error handling an inline request: %s", err)
}
} else if update.CallbackQuery != nil {
err = HandleCommand(bot, update, true)
if err != nil {
lib.LogError("Error handling a callback: %s", err)
}
} else if update.Message.IsCommand() {
if update.Message.From.UserName != os.Getenv("TELEGRAM_USER") {
continue
}
err = HandleCommand(bot, update, false)
if err != nil {
lib.LogError("Error handling a command: %s", err)
}
}
}
}()
// Wait here until CTRL-C or other term signal is received.
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
lib.LogInfo("Gracefully shutting down bot 💤")
}