Skip to content

Commit

Permalink
gofmt and go imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Davies committed May 4, 2018
1 parent 9339c3c commit 7b7e077
Showing 1 changed file with 71 additions and 71 deletions.
142 changes: 71 additions & 71 deletions welcomebot.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package main

import (
"encoding/json"
"fmt"
"os"
"fmt"
"strings"
"encoding/json"
log "github.com/Sirupsen/logrus"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/nlopes/slack"
)

type publicResponse struct {
Channel string `json:"channel"`
Raw bool `json:"raw_response"`
Response string `json:"response"`
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"`
Channel string `json:channel"`
Raw bool `json:"raw_response"`
Response string `json:"response"`
}

type Config struct {
PublicResponses []publicResponse `json:"responses"`
DmResponses []dmResponse `json:"dmresponses"`
PublicResponses []publicResponse `json:"responses"`
DmResponses []dmResponse `json:"dmresponses"`
}

var (
Expand All @@ -33,7 +34,7 @@ var (
func main() {

token := os.Getenv("SLACK_TOKEN")
config := loadConfig("config.json")
config := loadConfig("config.json")
api := slack.New(token)
api.SetDebug(false)

Expand All @@ -47,21 +48,21 @@ Loop:
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.ConnectedEvent:
botId = ev.Info.User.ID
botId = ev.Info.User.ID
log.Infof("Connection counter:", ev.ConnectionCount)
case *slack.MessageEvent:
//only interested in public channels
cInfo, err := api.GetChannelInfo(ev.Channel)
if err == nil {
if ev.SubType == "channel_join" {
log.Infof("channel_join seen on channel: %s", ev.Msg.Channel)
respondToJoin(rtm, ev, cInfo.Name, config)
}
if ev.User != botId && strings.HasPrefix(ev.Text, "<@" + botId + ">") {
log.Infof("message seen on public channel: %s", ev.Msg.Channel)
respondToMessage(rtm, ev, cInfo.Name, config)
}
}
//only interested in public channels
cInfo, err := api.GetChannelInfo(ev.Channel)
if err == nil {
if ev.SubType == "channel_join" {
log.Infof("channel_join seen on channel: %s", ev.Msg.Channel)
respondToJoin(rtm, ev, cInfo.Name, config)
}
if ev.User != botId && strings.HasPrefix(ev.Text, "<@"+botId+">") {
log.Infof("message seen on public channel: %s", ev.Msg.Channel)
respondToMessage(rtm, ev, cInfo.Name, config)
}
}
case *slack.RTMError:
log.Errorf("Error: %s\n", ev.Error())

Expand All @@ -77,11 +78,11 @@ Loop:
}

func sendMessage(rtm *slack.RTM, channel string, message string, raw bool) {
if raw {
rtm.PostMessage(channel, message, slack.PostMessageParameters{EscapeText: false})
} else {
rtm.SendMessage(rtm.NewOutgoingMessage(message, channel))
}
if raw {
rtm.PostMessage(channel, message, slack.PostMessageParameters{EscapeText: false})
} else {
rtm.SendMessage(rtm.NewOutgoingMessage(message, channel))
}
}

func respondToMessage(rtm *slack.RTM, ev *slack.MessageEvent, name string, config Config) {
Expand All @@ -90,61 +91,60 @@ func respondToMessage(rtm *slack.RTM, ev *slack.MessageEvent, name string, confi
"help": true,
}

text := ev.Msg.Text
prefix := fmt.Sprintf("<@%s> ", botId)
text := ev.Msg.Text
prefix := fmt.Sprintf("<@%s> ", botId)
text = strings.TrimPrefix(text, prefix)
text = strings.TrimSpace(text)
text = strings.ToLower(text)

if acceptedGreetings[text] {
for _, publicResponse := range config.PublicResponses {
if publicResponse.Channel == name {
publicMsg := fmt.Sprintf("*Public response for this channel*:\n\n%s", publicResponse.Response)
sendMessage(rtm, ev.Msg.Channel, publicMsg, publicResponse.Raw)
}
}

for _, dmResponse := range config.DmResponses {
if dmResponse.Channel == name {
dmMsg := fmt.Sprintf("*DM response for this channel*:\n\n%s", dmResponse.Response)
sendMessage(rtm, ev.Msg.Channel, dmMsg, dmResponse.Raw)
}
}
if publicResponse.Channel == name {
publicMsg := fmt.Sprintf("*Public response for this channel*:\n\n%s", publicResponse.Response)
sendMessage(rtm, ev.Msg.Channel, publicMsg, publicResponse.Raw)
}
}

for _, dmResponse := range config.DmResponses {
if dmResponse.Channel == name {
dmMsg := fmt.Sprintf("*DM response for this channel*:\n\n%s", dmResponse.Response)
sendMessage(rtm, ev.Msg.Channel, dmMsg, dmResponse.Raw)
}
}
}
}

func respondToJoin(rtm *slack.RTM, ev *slack.MessageEvent, name string, config Config) {

for _, publicResponse := range config.PublicResponses {
if publicResponse.Channel == name {
log.Infof("Sending public reply to channel %s", name)
sendMessage(rtm, ev.Msg.Channel, publicResponse.Response, publicResponse.Raw)
}
}

for _, publicResponse := range config.PublicResponses {
if publicResponse.Channel == name {
log.Infof("Sending public reply to channel %s", name)
sendMessage(rtm, ev.Msg.Channel, publicResponse.Response, publicResponse.Raw)
}
}

for _, dmResponse := range config.DmResponses {
if dmResponse.Channel == name {
sta, stb, channel, err := rtm.OpenIMChannel(ev.User)
if err != nil && sta && stb {
log.Warnf("Failed to open IM channel to user: %s", err)
}
log.Infof("Sending DM to user %s", ev.User)
sendMessage(rtm, channel, dmResponse.Response, dmResponse.Raw)
}
}
for _, dmResponse := range config.DmResponses {
if dmResponse.Channel == name {
sta, stb, channel, err := rtm.OpenIMChannel(ev.User)
if err != nil && sta && stb {
log.Warnf("Failed to open IM channel to user: %s", err)
}
log.Infof("Sending DM to user %s", ev.User)
sendMessage(rtm, channel, dmResponse.Response, dmResponse.Raw)
}
}

}

func loadConfig(file string) Config {

var config Config
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
log.Fatalf("Error opening config file %s", err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config
var config Config
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
log.Fatalf("Error opening config file %s", err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config
}

0 comments on commit 7b7e077

Please sign in to comment.