This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
200 lines (166 loc) · 3.79 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"sync"
"github.com/mitjaziv/slackbot/command"
"github.com/mitjaziv/slackbot/service"
)
var commands = map[string]func() command.Command{
"^echo ": command.Echo,
"^time$": command.Time,
"^hello$": command.Hello,
"^menu": command.Menu,
}
type slackBot struct {
exit chan bool
connector service.Connector
commands map[string]command.Command
services map[string]string
sync.RWMutex
}
func newSlackBot(commands map[string]command.Command) *slackBot {
commands["^help$"] = help(commands)
return &slackBot{
exit: make(chan bool),
commands: commands,
connector: service.NewConnector(),
services: make(map[string]string),
}
}
func main() {
// Token path
tpath := flag.String("tpath", "/etc/slackbot/TOKEN", "token path")
// Debug mode
debug := flag.Bool("debug", false, "debug mode")
// Parse flags
flag.Parse()
// Get token from file
t, err := ioutil.ReadFile(*tpath)
if err != nil {
log.Printf("error reading slack token %v", err)
os.Exit(1)
}
// Convert token to string
token := strings.TrimSpace(string(t))
// Create commands map
cmds := make(map[string]command.Command)
// register default built in commands
for pattern, cmd := range commands {
cmds[pattern] = cmd()
}
// Create bot
b := newSlackBot(cmds)
// Start bot
if err := b.start(*debug, token); err != nil {
log.Printf("error starting bot %v", err)
os.Exit(1)
}
// Run bot
if err := b.run(); err != nil {
log.Printf("error running bot %v", err)
os.Exit(1)
}
// Stop bot
if err := b.stop(); err != nil {
log.Printf("error stopping bot %v", err)
}
}
func (b *slackBot) start(debug bool, token string) error {
log.Println("[slackbot] starting")
// Initialize connection
if err := b.connector.Init(debug, token); err != nil {
return err
}
// Start/Open connection
if err := b.connector.Start(); err != nil {
return err
}
return nil
}
func (b *slackBot) run() error {
log.Println("[slackbot] connecting")
c, err := b.connector.Stream()
if err != nil {
return err
}
// Process connection until exit event
for {
select {
case <-b.exit:
log.Println("[slackbot] closing connection")
return c.Close()
default:
var recvEv service.Event
// receive input
if err := c.Recv(&recvEv); err != nil {
return err
}
// only process TextEvent
if recvEv.Type != service.TextEvent {
continue
}
if len(recvEv.Data) == 0 {
continue
}
if err := b.process(c, recvEv); err != nil {
return err
}
}
}
}
func (b *slackBot) stop() error {
log.Println("[slackbot] stopping")
close(b.exit)
// closing connection
if err := b.connector.Stop(); err != nil {
log.Printf("[slackbot] %v", err)
}
return nil
}
func (b *slackBot) process(c *service.Connection, ev service.Event) error {
args := strings.Split(string(ev.Data), " ")
if len(args) == 0 {
return nil
}
b.RLock()
defer b.RUnlock()
// try built in command or skip if it doesn't match
for pattern, cmd := range b.commands {
if m, err := regexp.Match(pattern, ev.Data); err != nil || !m {
continue
}
// matched, exec command
rsp, err := cmd.Exec(args...)
if err != nil {
rsp = []byte("error executing cmd: " + err.Error())
}
// send response
return c.Send(&service.Event{
Meta: ev.Meta,
From: ev.To,
To: ev.From,
Type: service.TextEvent,
Data: rsp,
})
}
return nil
}
// Crete help command
func help(commands map[string]command.Command) command.Command {
var cmds []command.Command
for _, cmd := range commands {
cmds = append(cmds, cmd)
}
return command.NewCommand("help", func(args ...string) ([]byte, error) {
response := []string{"\n"}
for _, cmd := range cmds {
response = append(response, cmd.Name())
}
return []byte(strings.Join(response, "\n")), nil
})
}