-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (126 loc) · 3.13 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
package main
import (
"bytes"
"fmt"
_ "github.com/davecgh/go-spew/spew"
"github.com/nlopes/slack"
"log"
"strings"
"gopkg.in/yaml.v2"
"io/ioutil"
)
const (
configFile string = "config.yml"
)
type BotMessage struct {
Channel string `json:"channel,omitempty"`
User string `json:"user,omitempty"`
Text string `json:"text,omitempty"`
Team string `json:"team,omitempty"`
}
type Config struct {
Api Api
}
type Api struct {
Key string `key`
}
var (
botId string
)
func isMentioned(event *slack.MessageEvent) bool {
if event.Type == "message" && strings.HasPrefix(event.Text, "<@"+botId+">") {
return true
}
return false
}
func radioMessage(air OnAir) string {
var buffer bytes.Buffer
buffer.WriteString("Nu op Top2000: " + air.Results[0].Songfile.Title + " - " + air.Results[0].Songfile.Artist)
return buffer.String()
}
func handleRadioReport(command []string, message BotMessage, reply chan<- BotMessage) {
if len(command) > 1 {
data, err := getAapjeData()
if err != nil {
log.Fatal(err)
}
message.Text = radioMessage(data)
fmt.Printf("Sending reply to channel\n")
reply <- message
fmt.Printf("Reply sent to channel\n")
}
}
func BotMentionChannelHandler(incoming <-chan BotMessage, reply chan<- BotMessage) {
fmt.Printf("Started bot handler channel\n")
for message := range incoming {
fmt.Printf("Handling mention\n")
command := strings.Fields(message.Text)
if len(command) > 0 {
myCommand := command[1]
switch {
case strings.Contains(myCommand, "nu"):
go handleRadioReport(command, message, reply)
}
}
}
}
func BotReplyChannelHandler(incoming <-chan BotMessage, api *slack.Client) {
fmt.Printf("Starting bot reply handler channel\n")
for message := range incoming {
fmt.Printf("Handling reply\n")
//spew.Dump(message)
replyParameters := slack.PostMessageParameters{}
replyParameters.AsUser = true
fmt.Printf("Posting reply")
_, _, err := api.PostMessage(message.Channel, message.Text, replyParameters)
if err != nil {
log.Fatal(err)
}
}
}
func readConfig() Config {
fmt.Printf("Obtaining config\n")
var config Config
source, err := ioutil.ReadFile(configFile)
if err != nil {
panic(err)
}
fmt.Printf("Reading configfile\n")
yaml.Unmarshal(source, &config)
return config
}
func getToken() string {
config := readConfig()
fmt.Printf("Returning API token\n")
return config.Api.Key
}
func main() {
api := slack.New(getToken())
rtm := api.NewRTM()
botMentionChannel := make(chan BotMessage)
botReplyChannel := make(chan BotMessage)
go rtm.ManageConnection()
go BotMentionChannelHandler(botMentionChannel, botReplyChannel)
go BotReplyChannelHandler(botReplyChannel, api)
for {
select {
case msg := <-rtm.IncomingEvents:
switch event := msg.Data.(type) {
case *slack.ConnectedEvent:
fmt.Printf("Connected!\n")
botId = event.Info.User.ID
case *slack.MessageEvent:
if isMentioned(event) {
fmt.Printf("Was mentioned!\n")
fmt.Printf("Sending to channel!\n")
botMentionChannel <- BotMessage{
Channel: event.Channel,
User: event.User,
Text: event.Text,
Team: event.Team,
}
}
}
}
}
}