diff --git a/moonsla.go b/moonsla.go index 308d058..59d77e0 100644 --- a/moonsla.go +++ b/moonsla.go @@ -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 @@ -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 @@ -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) diff --git a/moonsla_test.go b/moonsla_test.go index 920bcfb..13d39df 100644 --- a/moonsla_test.go +++ b/moonsla_test.go @@ -2,6 +2,8 @@ package main import ( "testing" + + "github.com/nlopes/slack" ) func TestGetTimeStamp(t *testing.T) { @@ -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) + } + }) + } +}