Skip to content

Amazon SNS Subject support #797

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions apprise/plugins/NotifySNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@

# Extend HTTP Error Messages
AWS_HTTP_ERROR_MAP = {
403: 'Unauthorized - Invalid Access/Secret Key Combination.',
403: 'Unauthorized - Invalid Access/Secret Key Combination, or \
insufficient IAM Permissions: \
[`sns:Publish`,`sns:CreateTopic`]',
}


Expand Down Expand Up @@ -89,9 +91,10 @@ class NotifySNS(NotifyBase):
# Source: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html
body_maxlen = 160

# A title can not be used for SMS Messages. Setting this to zero will
# cause any title (if defined) to get placed into the message body.
title_maxlen = 0
# Topic Targets on SNS may accept titles up to 100 characters. The title
# of the message will also be placed in the message body where present.
# Source: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html
title_maxlen = 100

# Define object templates
templates = (
Expand Down Expand Up @@ -248,6 +251,10 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
# Get Phone No
no = phone.pop(0)

# Includes the title if present, simulating `title_maxlen = 0`
if title != '':
body = title + "\n" + body

# Prepare SNS Message Payload
payload = {
'Action': u'Publish',
Expand Down Expand Up @@ -285,6 +292,10 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
error_count += 1
continue

# Backwards Compatibility: Prepends the body with the title,
# simulating the behaviour of `title_maxlen = 0`
body = title + '\n' + body

# Build our payload now that we know our topic_arn
payload = {
'Action': u'Publish',
Expand All @@ -293,6 +304,13 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
'Message': body,
}

# If a Title exists, add it to the payload, since it's supported
# for SNS Topic Targets
if title != '':
payload.update({
'Subject': title
})

# Send our payload to AWS
(result, _) = self._post(payload=payload, to=topic)
if not result:
Expand Down