-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 1.88 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
package main
import (
"log"
"os"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
// Check if all the right environment variables are set.
checkEnvironment()
// Clone the repo if it does not exist
err := cloneIfNotExist()
if err != nil {
log.Panic("Unable to download the repository: ", err.Error())
}
// Load the subscribed users into memory
err = loadUsers()
if err != nil {
log.Panic("Unable to load the user file: ", err.Error())
}
log.Println("Users loaded")
// Setup the telegram repo
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Panic(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
// Setup the background task (git crawling)
startBackgroundManager(bot)
// Handle the updates
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
// Handle the current update in a new go routine
if update.Message != nil {
go handleMessage(bot, &update)
}
// Handle the current update in a new go routine
if update.CallbackQuery != nil {
go handleCallBackQuery(bot, &update)
}
}
}
// This function checks if the bot got started with all necessary environment variables set.
// If not it will print an error message into the terminal and terminate the program.
func checkEnvironment() {
isOk := true
if os.Getenv("TELEGRAM_TOKEN") == "" {
isOk = false
log.Println("The TELEGRAM_TOKEN environment variable is not set.")
}
if os.Getenv("TELEGRAM_ADMIN") == "" {
isOk = false
log.Println("The TELEGRAM_ADMIN environment variable is not set.")
}
if os.Getenv("GIT_URL") == "" {
isOk = false
log.Println("The GIT_URL environment variable is not set.")
}
if isOk == false {
log.Println("You can find more information about how to configure the bot at:")
log.Println("https://github.com/flofriday/EP2-Bot")
os.Exit(1)
}
}