-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.53 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
package main
import (
"fmt"
"github.com/umbrellium/mario/Godeps/_workspace/src/golang.org/x/net/websocket"
"log"
"os"
"strings"
)
func main() {
// instantiate slack
var s Slack
fmt.Println("Running Mario. Press ctrl+C to stop it")
// slack token must be set as environmet var or passed as command line
token := os.Getenv("TOKEN")
if token == "" {
token = os.Args[1]
if token == "" {
log.Fatal("You must pass a token to connect to Slack")
}
}
var ws *websocket.Conn
// connect to slack
ws, marioID, err := connectToSlack(token)
// pass websocket to slack struct
s.Socket = ws
if err != nil {
log.Fatal(err)
}
for {
message, err := s.getMessage()
if err != nil {
log.Fatal(err)
}
// parse message and act accordingly
if message.Type == "message" && strings.HasPrefix(message.Text, "<@"+marioID+">") {
text := strings.TrimPrefix(message.Text, "<@"+marioID+"> ")
text = strings.TrimSpace(text)
messageHandled := false
for _, task := range tasks {
// we are using text to perform a reg ex and decide which method to call
if task.Hear(&s, message, text) {
messageHandled = true
break
}
}
// Mario cannot understand command
if messageHandled == false {
message.Text = `I don't understand what you are asking me to do.
Please ensure that your message doesn't contain any spelling mistake.
You can type '@mario help' to see a list of the available tasks I can perform.`
err := s.postMessage(message)
if err != nil {
log.Fatal(err)
}
}
}
}
}