Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to each channel in PublicResponses automatically. #17

Merged
merged 2 commits into from
Aug 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@ import (
"github.com/nlopes/slack"
)

type publicResponse struct {
Channel string `json:"channel"`
Raw bool `json:"raw_response"`
Response string `json:"response"`
}

type dmResponse struct {
Channel string `json:"channel"`
Raw bool `json:"raw_response"`
Response string `json:"response"`
}

type ephResponse struct {
type slackResponse struct {
Channel string `json:"channel"`
Raw bool `json:"raw_response"`
Response string `json:"response"`
}

type Config struct {
PublicResponses []publicResponse `json:"responses"`
DmResponses []dmResponse `json:"dmresponses"`
EphResponses []ephResponse `json:"ephresponses"`
PublicResponses []slackResponse `json:"responses"`
DmResponses []slackResponse `json:"dmresponses"`
EphResponses []slackResponse `json:"ephresponses"`
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted. Yeah, no need for these to be separate types if they're all the same. :)


var (
Expand All @@ -49,8 +37,22 @@ func main() {

go rtm.ManageConnection()

// Return a slice of all channels from config.json
allChans := getChannelList(config.PublicResponses, config.DmResponses, config.EphResponses)
// Because duplicates are possible, make a new slice without duplicates
cleanSlice := removeDuplicates(allChans)

// Range over the cleaned up slice and join channels
for _, v := range cleanSlice {
_, err := api.JoinChannel(v)
if err != nil {
log.Errorf("Error joining public channel: %s", err)
}
}

Loop:
for {

select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
Expand Down Expand Up @@ -183,3 +185,29 @@ func loadConfig(file string) Config {
jsonParser.Decode(&config)
return config
}

func getChannelList(publicSlice, dmSlice, ephSlice []slackResponse) []string {
var newSlice []string
for _, v := range publicSlice {
newSlice = append(newSlice, v.Channel)
}
for _, v := range dmSlice {
newSlice = append(newSlice, v.Channel)
}
for _, v := range ephSlice {
newSlice = append(newSlice, v.Channel)
}
return newSlice
}

func removeDuplicates(channelSlice []string) []string {
k := make(map[string]bool)
slice := []string{}
for _, entry := range channelSlice {
if _, value := k[entry]; !value {
k[entry] = true
slice = append(slice, entry)
}
}
return slice
}