-
Notifications
You must be signed in to change notification settings - Fork 0
/
talbot.go
59 lines (46 loc) · 1.54 KB
/
talbot.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
package main // import "github.com/james-bowman/talbot"
import (
"flag"
"fmt"
"github.com/james-bowman/slack"
"github.com/james-bowman/talbot/brain"
"io/ioutil"
"log"
"os"
)
func main() {
slackToken := getToken()
conn, err := slack.Connect(slackToken)
if err != nil {
log.Fatal(err)
}
slack.EventProcessor(conn, brain.OnAskedMessage, brain.OnHeardMessage)
}
// getToken for authenticating with Slack. Ordered lookup process trying first the command line,
// then environment variables and finally a config file
func getToken() string {
var slackToken string
// look for the token on the command line
log.Println("Looking for Slack auth token as command line argument")
flag.StringVar(&slackToken, "slacktoken", "", "Slack authentication token - if not specified, will look for an environment variable or config file")
flag.Parse()
if slackToken == "" {
// if not specified look for it in an environment variable
log.Println("Slack auth token not found - looking for environment variable")
slackToken = os.Getenv("TALBOT_SLACK_TOKEN")
}
if slackToken == "" {
// if not specified look for it in a config file
log.Println("Slack auth token not found - looking for config file")
slackTokenFileName := "slack.token"
slackTokenFile, err := ioutil.ReadFile(slackTokenFileName)
if err != nil {
log.Panic(fmt.Sprintf("Error opening slack authentication token file %s: %s", slackTokenFileName, err))
}
slackToken = string(slackTokenFile)
}
if slackToken != "" {
log.Println("Slack auth token found")
}
return slackToken
}