-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
76 lines (64 loc) · 1.8 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
package main
import (
"flag"
"github.com/BurntSushi/toml"
"github.com/daskol/telepyth/srv"
"log"
)
var storage *srv.Storage
type Config struct {
Token string `toml:"token"`
Storage string `toml:"storage"`
Polling bool `toml:"polling"`
Timeout int `toml:"timeout"`
MetricsLog string `toml:"metrics_log"`
}
func main() {
configPath := flag.String("config", "", "Path to toml config file.")
metricsLog := flag.String("metrics-log", "metrics.tsv",
"Tab-separated values.")
token := flag.String("token", "", "A unique authentication token.")
dbPath := flag.String("database", "bolt.db",
"Create or open a database at the given path.")
polling := flag.Bool("polling", false, "Use long polling to get updates")
timeout := flag.Int("timeout", 30, "Timeout in seconds for long polling.")
flag.Parse()
config := &Config{
Token: *token,
Storage: *dbPath,
Polling: *polling,
Timeout: *timeout,
MetricsLog: *metricsLog,
}
if len(*configPath) != 0 {
log.Println("load config from " + *configPath)
if _, err := toml.DecodeFile(*configPath, config); err != nil {
log.Fatal(err)
}
}
log.Println("open database at " + config.Storage)
if db, err := srv.NewStorage(config.Storage); err != nil {
log.Fatal(err)
} else {
storage = db
defer storage.Close()
}
log.Println("use token " + config.Token)
api := srv.New(config.Token)
if me, err := api.GetMe(); err != nil {
log.Fatal("exit: ", err)
} else {
log.Println("Telegram Bot API: /getMe:")
log.Println(" Id:", me.Id)
log.Println(" First Name:", me.FirstName)
log.Println(" Last Name:", me.LastName)
log.Println(" Username:", me.UserName)
}
log.Fatal((&srv.TelePyth{
Api: api,
Storage: storage,
Polling: true,
Timeout: 30,
MetricsLog: *metricsLog,
}).Serve())
}