-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (85 loc) · 2.63 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
package main
import (
"flag"
"fmt"
"os"
"github.com/lnovara/workbot/api"
"github.com/lnovara/workbot/userdb"
"github.com/lnovara/workbot/version"
"github.com/sirupsen/logrus"
)
const (
// BANNER is printed for help/info output
BANNER = `
_ _ _
__ _____ _ __| | _| |__ ___ | |_
\ \ /\ / / _ \| '__| |/ / '_ \ / _ \| __|
\ V V / (_) | | | <| |_) | (_) | |
\_/\_/ \___/|_| |_|\_\_.__/ \___/ \__|
A Telegram bot that help you keep track of your working hours.
Version: %s
Build: %s
`
)
var (
dbFilePath string
googleAPIKey string
googleClientSecretFilePath string
telegramToken string
debug bool
)
func init() {
flag.StringVar(&dbFilePath, "db", "./workbot-users.sqlite3", "User database path")
flag.StringVar(&googleAPIKey, "google-api-key", os.Getenv("GOOGLE_API_KEY"), "Google API key (or env var GOOGLE_API_KEY)")
flag.StringVar(&googleClientSecretFilePath, "google-client-secrets", "./client_secrets.json", "Path to Google's client_secret.json file")
flag.StringVar(&telegramToken, "telegram-token", os.Getenv("TELEGRAM_TOKEN"), "Telegram API token (or env var TELEGRAM_TOKEN)")
flag.BoolVar(&debug, "d", false, "run in debug mode")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, fmt.Sprintf(BANNER, version.VERSION, version.GITCOMMIT))
flag.PrintDefaults()
}
flag.Parse()
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if googleAPIKey == "" {
usageAndExit("Google API key cannot be empty.", 1)
}
if telegramToken == "" {
usageAndExit("Telegram API key cannot be empty.", 1)
}
}
func main() {
var err error
logrus.Info("Welcome to WorkBot!!!")
api.NewTelegramBot(telegramToken, debug)
if err != nil {
logrus.Fatalf("Could not initialize Telegram Bot API: %s", err.Error())
}
logrus.Debug("Telegram Bot API initialization done")
err = userdb.NewUserDB(dbFilePath)
if err != nil {
logrus.Fatalf("Could not create user database %s: %s", dbFilePath, err.Error())
}
logrus.Debug("Database initialization done")
err = api.NewMapsClient(googleAPIKey)
if err != nil {
logrus.Fatalf("Could not initialize Google Maps API: %s", err.Error())
}
logrus.Debug("Google Maps client initialization done")
api.NewOAuthConfig(googleClientSecretFilePath)
if err != nil {
logrus.Fatalf("Could not initialize Google OAuth2 config: %s", err.Error())
}
logrus.Debug("OAuth config initialization done")
api.HandleBotUpdates()
}
func usageAndExit(message string, exitCode int) {
if message != "" {
fmt.Fprintf(os.Stderr, message)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(exitCode)
}