diff --git a/plugin.json b/plugin.json index 16bf974..b18cad3 100644 --- a/plugin.json +++ b/plugin.json @@ -43,6 +43,14 @@ "help_text": "Generated token to validate incoming requests from AWS SNS.", "placeholder": "", "default": null + }, + { + "key": "EnableUnknownTypeMessages", + "display_name": "Enable unknown type notifications support:", + "type": "bool", + "help_text": "If the AWS SNS plugin does not support a particular type of SNS notifications, enabling this setting will display those notifications as formatted JSON or plain text.", + "placeholder": "", + "default": false } ] } diff --git a/server/configuration.go b/server/configuration.go index a967c43..f2a9500 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -18,9 +18,10 @@ import ( // If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep // copy appropriate for your types. type configuration struct { - TeamChannel string - AllowedUserIds string - Token string + TeamChannel string + AllowedUserIds string + Token string + EnableUnknownTypeMessages bool } // Clone shallow copies the configuration. Your implementation may require a deep copy if diff --git a/server/plugin.go b/server/plugin.go index 96e79bb..1302b67 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "fmt" "io" @@ -267,6 +268,10 @@ func (p *Plugin) handleNotification(body io.Reader, channel *TeamChannel) { p.sendPostNotification(p.createSNSMessageNotificationAttachment(notification.Subject, messageNotification), channel) return } + + if p.configuration.EnableUnknownTypeMessages { + p.sendPostNotification(p.createSNSUnknownTypeMessage(notification.Subject, notification.Message), channel) + } } func (p *Plugin) sendPostNotification(attachment model.SlackAttachment, channel *TeamChannel) { @@ -372,6 +377,26 @@ func (p *Plugin) createSNSCloudformationEventAttachment(subject string, messageN return attachment } +func (p *Plugin) createSNSUnknownTypeMessage(subject string, message string) model.SlackAttachment { + p.API.LogDebug("AWSSNS HandleNotification Unknown Type Message", "SUBJECT", subject) + + text := message + + jsonBytes := []byte(message) + prettyJSON := &bytes.Buffer{} + err := json.Indent(prettyJSON, jsonBytes, "", " ") + if err == nil { + text = "```json\n" + prettyJSON.String() + "\n```" + } + + post := model.SlackAttachment{ + Title: subject, + Text: text, + } + + return post +} + func (p *Plugin) createSNSMessageNotificationAttachment(subject string, messageNotification SNSMessageNotification) model.SlackAttachment { p.API.LogDebug("AWSSNS HandleNotification", "MESSAGE", subject) var fields []*model.SlackAttachmentField