Skip to content

Commit

Permalink
Merge pull request #2 from Crazybus/im_so_attached_to_you
Browse files Browse the repository at this point in the history
Properly format attachments instead of just skipping them
  • Loading branch information
Crazybus authored Jul 9, 2018
2 parents bc414bd + b8dad0f commit 57b14c2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
33 changes: 27 additions & 6 deletions moonsla.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ func formatMentions(msg string, users map[string]string) string {
return msg
}

func formatAttachments(attachments []slack.Attachment) string {

var messages []string

for _, a := range attachments {

text := a.Text
if a.Title != "" {
text = a.Title + ": " + text
}

messages = append(messages, text)
}
return strings.Join(messages, "\n")
}

func filterChannel(name string, channels map[string]string, whitelist []string) (whitelisted bool, cName string) {
whitelisted = false

Expand Down Expand Up @@ -105,11 +121,6 @@ func main() {

case *slack.MessageEvent:

// Skip empty messages
if ev.Text == "" {
continue
}

whitelisted, cName := filterChannel(ev.Channel, channels, whitelist)
if !whitelisted {
continue
Expand All @@ -121,10 +132,20 @@ func main() {
uName = ev.User
}

if ev.Username != "" {
uName = ev.Username
}

t := getTimeStamp(ev.EventTimestamp)
timeStamp := fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())

msg := formatMentions(ev.Text, users)
text := ev.Text

if len(ev.Attachments) > 0 {
text = formatAttachments(ev.Attachments)
}

msg := formatMentions(text, users)

fmt.Printf("%v - %v - %v: %v\n", timeStamp, cName, uName, msg)

Expand Down
52 changes: 52 additions & 0 deletions moonsla_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"testing"

"github.com/nlopes/slack"
)

func TestGetTimeStamp(t *testing.T) {
Expand Down Expand Up @@ -146,3 +148,53 @@ func TestFormatMentions(t *testing.T) {
})
}
}

func TestFormatAttachments(t *testing.T) {
var tests = []struct {
description string
attachments []slack.Attachment
want string
}{
{
"Print attachment as single line",
[]slack.Attachment{slack.Attachment{
Title: "",
Text: "test message",
}},
"test message",
},
{
"Print attachment with title",
[]slack.Attachment{slack.Attachment{
Title: "title",
Text: "test message",
}},
"title: test message",
},
{
"Print multie attachments",
[]slack.Attachment{
slack.Attachment{
Title: "",
Text: "first message",
},
slack.Attachment{
Title: "",
Text: "second message",
}},
"first message\nsecond message",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {

got := formatAttachments(test.attachments)

want := test.want

if got != want {
t.Errorf("got '%v' want '%v'", got, want)
}
})
}
}

0 comments on commit 57b14c2

Please sign in to comment.