-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslacky.go
86 lines (70 loc) · 1.85 KB
/
slacky.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
package main
import (
"fmt"
"net/http"
"os"
"log"
"io/ioutil"
"encoding/json"
"strings"
globals "github.com/aerophite/slacky/globals"
logging "github.com/aerophite/slacky/logging"
hangman "github.com/aerophite/slacky/hangman"
"github.com/ejholmes/slash"
"golang.org/x/net/context"
)
type Config struct {
Log logging.Log `json:"log"`
}
var (
config Config
)
func init() {
file, err := ioutil.ReadFile("./config.json")
if err != nil {
log.Fatal("File doesn't exist")
}
if err := json.Unmarshal(file, &config); err != nil {
log.Fatal("Cannot parse config.json: " + err.Error())
}
if (config.Log.Directory == "default") {
dir, err := os.Getwd()
if err != nil {
fmt.Println("[Fatal Error] " + err.Error())
log.Fatal(err)
}
config.Log.Directory = dir + "/logs/"
}
}
func main() {
h := slash.HandlerFunc(Handle)
s := slash.NewServer(h)
logging.WriteToLog("Slack Thingy Started!", config.Log);
http.ListenAndServe(":8082", s)
}
func Handle(ctx context.Context, r slash.Responder, command slash.Command) error {
fields := strings.Fields(strings.TrimSpace(command.Text))
field, fields := fields[0], fields[1:]
message := globals.Message{
command.Token,
globals.Team{command.TeamID, command.TeamDomain},
field,
command.Text,
fields,
globals.Channel{command.ChannelID, command.ChannelName},
globals.User{command.UserID, command.UserName, command.ResponseURL},
r}
switch command.Command {
case "/hangman":
hangman.Hangman(message)
}
return nil
}
func printErrors(h slash.Handler) slash.Handler {
return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, command slash.Command) error {
if err := h.ServeCommand(ctx, r, command); err != nil {
fmt.Printf("error: %v\n", err)
}
return nil
})
}