This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
75 lines (64 loc) · 1.5 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
// Copyright 2015 The tgbot-ng Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage: tgbot config")
os.Exit(2)
}
configFile := os.Args[1]
cfg, err := parseConfig(configFile)
if err != nil {
log.Fatalln(err)
}
tgbot := newBot(cfg.Name, cfg.Token)
if len(cfg.AllowedIDs) > 0 {
tgbot.setAllowedIDs(cfg.AllowedIDs)
}
if cfg.UpdateInterval > 0 {
tgbot.setUpdateInterval(cfg.UpdateInterval)
}
// Add enabled commands
if cfg.Commands.Ano.Enabled {
tgbot.addCommand(NewCmdAno(cfg.Commands.Ano, tgbot.cli))
}
if cfg.Commands.Bing.Enabled {
tgbot.addCommand(NewCmdBing(cfg.Commands.Bing, tgbot.cli))
}
tgbot.loop()
}
type config struct {
Name string `json:"name"`
Token string `json:"token"`
UpdateInterval int `json:"update_interval"`
AllowedIDs []int `json:"allowed_ids"`
Commands cmdConfigs `json:"commands"`
}
type cmdConfigs struct {
Ano CmdConfigAno `json:"ano"`
Bing CmdConfigBing `json:"bing"`
}
func parseConfig(file string) (config, error) {
f, err := os.Open(file)
if err != nil {
return config{}, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return config{}, err
}
var cfg config
if err := json.Unmarshal(b, &cfg); err != nil {
return config{}, err
}
return cfg, nil
}