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

Unknown type messages support #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand Down
7 changes: 4 additions & 3 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Loading