-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
51 lines (41 loc) · 1.03 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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package main
import (
"flag"
"log"
"os"
"github.com/keybase/slackbot"
"github.com/nlopes/slack"
)
var ignoreError = flag.Bool("i", false, "Ignore error (always exit 0)")
func handleError(s string, text string) {
if *ignoreError {
log.Printf("[Unable to send: %s] %s", s, text)
os.Exit(0)
}
log.Fatal(s)
}
func main() {
flag.Parse()
text := flag.Arg(0)
channel := os.Getenv("SLACK_CHANNEL")
if channel == "" {
handleError("SLACK_CHANNEL is not set", text)
}
api := slack.New(slackbot.GetTokenFromEnv())
// api.SetDebug(true)
channelIDs, err := slackbot.LoadChannelIDs(*api)
if err != nil {
handleError(err.Error(), text)
}
params := slack.NewPostMessageParameters()
params.AsUser = true
channelID := channelIDs[channel]
_, _, err = api.PostMessage(channelID, text, params)
if err != nil {
handleError(err.Error(), text)
} else {
log.Printf("[%s (%s)] %s\n", channel, channelID, text)
}
}