/
main.go
99 lines (86 loc) · 2.59 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
package main
import (
"encoding/json"
"flag"
c "github.com/byxorna/collinsbot/collins"
"github.com/byxorna/collinsbot/handlers"
"github.com/nlopes/slack"
"log"
"os"
)
var (
cli struct {
file string
token string
botname string
debug bool
}
settings Settings
//ws *slack.SlackWS
postParams slack.PostMessageParameters
//collins *c.Client
handlerContext *handlers.Context
// message handlers are functions that process a message event
// similar to http route handlers. The first to return true stops processing
messagehandlers = []handlers.Handler{
handlers.Handler{"Help", handlers.Help},
handlers.Handler{"YouAlive", handlers.YouAlive},
handlers.Handler{"AssetTag", handlers.AssetTag},
handlers.Handler{"AssetHostname", handlers.AssetHostname},
}
)
func init() {
flag.StringVar(&cli.token, "token", "", "Slack API token")
flag.StringVar(&cli.botname, "botname", "", "Bot name")
flag.StringVar(&cli.file, "config", "", "File containing Slack API token")
flag.BoolVar(&cli.debug, "debug", false, "Turn on Slack API debugging")
flag.Parse()
}
func main() {
if cli.file != "" {
log.Printf("Loading config from %s\n", cli.file)
f, err := os.Open(cli.file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = json.NewDecoder(f).Decode(&settings)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("You need to pass a json file to -config")
}
// override whats in the settings with whats on the cli
if cli.token != "" {
log.Printf("Slack token passed via CLI: %s\n", cli.token)
settings.Token = cli.token
}
if settings.Botname == "" || cli.botname != "" {
settings.Botname = cli.botname
}
if settings.Token == "" {
log.Fatal("You need to give me an API token!")
}
collins := c.New(settings.Collins.Username, settings.Collins.Password, settings.Collins.Host)
// set up posting params
postParams = slack.NewPostMessageParameters()
//postParams.Username = settings.Botname
//postParams.LinkNames = 1
// we will perform proper formatting per https://api.slack.com/docs/formatting, do make the server do no processing
postParams.Parse = "none"
api := slack.New(settings.Token)
api.SetDebug(cli.debug)
authresp, err := api.AuthTest()
if err != nil {
log.Fatal(err)
}
log.Printf("Authed with Slack successfully as %s (%s)\n", authresp.User, authresp.UserId)
log.Printf("Creating new handler context with message handlers:\n")
for _, v := range messagehandlers {
log.Printf(" %s\n", v)
}
handlerContext = handlers.New(collins, authresp, api, messagehandlers)
log.Printf("Starting up message handler")
handlerContext.Run()
}