Showing posts with label send sms using aws sns. Show all posts
Showing posts with label send sms using aws sns. Show all posts

Thursday, March 3, 2022

Configure SNS service on AWS

To send sms in india, sns service should be configured in Singapore. Mumbai is not providing facility right now. 

To send sms in US, service should be configured in US region.

1) Open SNS in AWS Management Console.  

Create Topic (Topic name + Display name > create topic)

2) Create Subscription (ARN : Created Topic Name + Protocol : Email > Create subscription)

3) Create user in IAM and attach policy SNS full access.

4) Now try sending sms using python script 

pip3 install boto3

nano sms.py

    import boto3
    # Create an SNS client
    access_key = 'ACLAXXXXXXXXXXXXXZBA'
    secret = 'Zwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region = "ap-southeast-1"
    number = '+9199xxxxxxxx'
    sender_id = 'senderidname'
    sms_message = 'Your code: 123456'
    sns = boto3.client('sns', aws_access_key_id=access_key, aws_secret_access_key=secret, region_name=region)
    sns.publish(PhoneNumber=number, Message=sms_message, MessageAttributes={'AWS.SNS.SMS.SenderID': {'DataType': 'String', 'StringValue':     sender_id}, 'AWS.SNS.SMS.SMSType': {'DataType': 'String', 'StringValue': 'Transactional'}})
python sms.py 

5) If you do not receive sms then check error in cloudwatch but before it you need to enable the logs.
Click on Text Messaging (SMS) option > Text messaging preferences > Edit
Delivery status logging - optional > Create new service role

It will take you on IAM page and it will create roles for
SNS Success feedback and SNS Failure feedback > Allow

Once roles are created

Delivery status logging - optional > Use existing service role > add ARN of failure feedback
arn:aws:iam::360209543005:role/SNSFailureFeedback > save changes

Now errors will be reported in cloudwatch log group
check the error > cloudwatch > Log groups

 "delivery":
        "phoneCarrier": "Vodaphone - Kerela",
        "mnc": 90,
        "numberOfMessageParts": 1,
        "destination": "+9199xxxxxxxx",
        "priceInUSD": 0.00223,
        "smsType": "Promotional",
        "mcc": 404,
        "providerResponse": "Phone carrier has blocked this message",
        "dwellTimeMs": 172,
        "dwellTimeMsUntilDeviceAck": 669
    },
    "status": "FAILURE"
Phone carrier has blocked the sms that's why it is not delivered because you were sending promotional sms type.
You should have sent Transaction sms type.
Either select default sms type transactional
Click on Text Messaging (SMS) option > Text messaging preferences > Edit > Default message type > transactional
OR send message type in the code, just like above example did. Now message will be delivered.

Send message by sender ID
i) US does not support sender ID
ii) To send message in India by sender id
Generate request in support for sender ID.
https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-awssupport-sender-id.html