Ahmed El-Mahdy
Ahmed El-Mahdy

Reputation: 11

Invalid type for parameter Message

I try to use "signal" in Django to send SNS email in AWS and my code is:

import boto3
from properties.models import PropertyList
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

@receiver(post_save, sender=PropertyList)
def send_property_details(sender, instance, created, **kwargs):
    if created:
        sns = boto3.client('sns')

        response = sns.publish(
            TopicArn='',# I write value of TopicArn
            Message={
            "name": instance.title,
            "description": instance.description
                },
            MessageStructure='json',
            Subject='New Property Created',
            MessageAttributes={
                'default':{
                    'DataType':'String',
                    'StringValue':instance.title
                    }
                },
            )

        print(response['MessageId'])

I get error as:

Parameter validation failed: Invalid type for parameter Message, value: {'name': 'aws', 'description': 'test'}, type: , valid types:

In AWS docs said If I want to send different messages for each transport protocol, set the value of the MessageStructure parameter to JSON and use a JSON object for the Message parameter. What is wrong in my code?

Note: I want to send more than values so I need to send JSON

Upvotes: 1

Views: 10102

Answers (1)

Evhz
Evhz

Reputation: 9285

In the example you paste the Message is a dictionary. That may be the reason for the error. Try to modify Message as follows:

import boto3, json    
...

mesg = json.dumps({
    "default": "defaultfield", # added this 
    "name": instance.title,
    "description": instance.description
})
response = sns.publish(
    TopicArn='topicARNvalue',
    Message=mesg,
    MessageStructure='json',
    Subject='New Property Created',
    MessageAttributes={}
)

Upvotes: 5

Related Questions