-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (78 loc) · 1.85 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
// Package main is the commanad line tool for the ion (chat) bots.
//
// Usage: go run main.go -slack=the_secret_slack_bot_token
// Chat say: "@ion-bot version", replies to: "ion's current version is: 1.0.0".
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/get-ion/bot/slack"
)
// BotService is the interface which should be completed by all bots integrations.
type BotService interface {
// Name should return the name of the bot, will be used to take the token from the cli flags.
Name() string
// Listen should start listening based on the token.
Listen(tok string) error
// Shutdown should close the bot's connection.
Shutdown()
}
func env(s string) string {
osEnvKey := strings.ToUpper(s) + "_BOT_TOKEN" // SLACK_BOT_TOKEN
return os.Getenv(osEnvKey)
}
func lookupToken(botName string) string {
// search by cli flags
flags := os.Args[1:]
for _, s := range flags {
if len(s) <= 2 {
continue
}
// omit -
// i.e: -slack=...
if s[0] == '-' {
s = s[1:]
}
// split name from value
// i.e: slack=val-val-val to name = [slack], value = [val-val-val]
if strings.Contains(s, "=") {
splitted := strings.Split(s, "=")
if len(splitted) >= 2 {
// if name matches
if name := splitted[0]; name == botName {
// return the token value
value := splitted[1]
return value
}
}
}
}
// search by system env
return env(botName)
}
var bots = []BotService{
slack.New(),
}
func main() {
for _, b := range bots {
name := b.Name()
token := lookupToken(name)
if token != "" {
log.Printf("%s bot is listening\n", name)
go b.Listen(token)
}
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGINT, os.Kill, syscall.SIGKILL, syscall.SIGTERM)
select {
case <-ch:
log.Println("exiting...")
for _, b := range bots {
b.Shutdown()
}
os.Exit(0)
}
}