I am using AWS SDK to send the sms to the mobile number for sending OTP through the SMS.
The Issue here I am facing is the SMS are only being sent during the day time according to Indian Standard Time (9 am to 9pm). If I try to send SMS request to the AWS SNS endpoint after 9 pm, then it will send the message after 9 am the next day.
The code is as below.
String ACCESS_KEY = env.getProperty("aws.access.key");
String SECRET_KEY = env.getProperty("aws.secret.access.key");
// Above we get the access and secret access key as credentials for the user.
String otp = getRandomNumberString(); // generates an OTP number of 4 digit
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(ACCESS_KEY,
SECRET_KEY));
String message = "Your Connect OTP for Login/Signup is: " + otp
+ ".\nNote: Please DO NOT SHARE THIS OTP with anyone.\nThanks";
String phoneNumber = "+91" + String.valueOf(mobile); // Ex: +91XXX4374XX
String messageID = sendSMSMessage(snsClient, message, phoneNumber);
// The definition of method named "sendSMSMessage" as above is written below in next code block
//definition of method named "sendSMSMessage"
public static String sendSMSMessage(AmazonSNSClient snsClient, String message, String phoneNumber) {
PublishResult result = snsClient
.publish(new PublishRequest().withMessage(message).withPhoneNumber(phoneNumber));
return result.getMessageId();
}
There are two types of SMS that you can send.
-Promotional Messages
-Transaction Messages
Promotional messages are like Advertisment. These Messges will not be delivered to DND numbers and also these messages can only be delivered between 9Am to 9PM as per NCPR guidelines.
Transaction Messages are sent for Transaction purpose like sending OTP and sending Payment confirmation. These messages can be delivered to any number 24/7.
So the problem is you need to change your SMS type to Transaction SMS
Related
I am working on sending OTP messages for user login leveraging Amazon SNS. I am able to send Text message as suggesting here. For the email notification as well I would like to use a similar approach. But looks like for email notifications, a topic has to be created in SNS and a subscriber has to be created for each email id registered in the application.
Is it not possible to send email to mail-id dynamically as done for text messages without creating topics and subscribers? If not please suggest a way to set email id dynamically based on the user logged in.
Code for Text Messaging:
public static void main(String[] args) {
AmazonSNSClient snsClient = new AmazonSNSClient();
String message = "My SMS message";
String phoneNumber = "+1XXX5550100";
Map<String, MessageAttributeValue> smsAttributes =
new HashMap<String, MessageAttributeValue>();
//<set SMS attributes>
sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}
public static void sendSMSMessage(AmazonSNSClient snsClient, String message,
String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
PublishResult result = snsClient.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
System.out.println(result); // Prints the message ID.
}
Correct.
Amazon SNS normally uses a Public/Subscribe model for messages.
The one exception is the ability to send an SMS message to a specific recipient.
If you wish to send an email to a single recipient, you will need to use your own SMTP server, or use Amazon Simple Email Service (Amazon SES).
I Am using AWS Cognito for authentication in a web application.
If a user Forgets their password I have a Lambda Function Trigger that sends a custom email with a link that has the users email address and the verification code in so they can click the link and just enter their new password.
So There is a new requirement that should allow the user to receive an sms, but currently it looks like if you enable MFA for sending the SMS, you can only customise the message in the console area and in the Lambda function,
this event.response.smsMessage does not seem to do anything.
So if the user has phone_number_verified true, it sends the sms without triggering the Lambda function.
exports.handler = function(event, context) {
if(event.triggerSource === "CustomMessage_ForgotPassword") {
var url = 'http://myurl.com/forgotpassword/'+ event.request.userAttributes.email+'/'+event.request.codeParameter
event.response.smsMessage = 'Password Reset: Click the link ' + url + ' or enter Verification Code: ' + event.request.codeParameter
event.response.emailSubject = " Password reset";
event.response.emailMessage = "Custom HTML goes here";
}
context.done(null, event);
};
This is currently the Lambda function configure on custom message trigger in the cognito User pool.
After a long discussion with AWS support, They came to the resolution that my SMS text was more than 140 chars. Yes 140, And then the SMS would not be sent rather the email would. No errors in logs or anything whatsoever.
So they said they will look into adding something in the logs that at least tells you that the SMS was not sent because the character limit was exceeded.
The following code works to send a message but when it arrives, it displays the text 'VERIFY' for a sender id. How do I specific a sender ID? I think it's done with the message attributes but I cannot figure out the syntax.
session = boto3.session.Session(profile_name='Credentials',region_name='us-east-1')
theMessage='Now is the time for all good people to come to the aid of their party'
senderID='Godzilla'
snsclient = session.client('sns')
response = snsclient.publish(PhoneNumber='+84932575571', Message=theMessage)
pp = pprint.PrettyPrinter(indent=4)
print(pp.pprint(response))
Add a third parameter MessageAttributes to the publish method.
snsclient.publish(PhoneNumber='+84932575571', Message=theMessage,MessageAttributes={
'AWS.SNS.SMS.SenderID': {
'DataType': 'String',
'StringValue': 'Godzilla'
}})
The sender id is not supported in many countries. see AWS SNS SMS SenderId Supported Countries
I'm using the AWS to send SMS via my app.
I can send SMS via AWS web console, however, sending SMS via SDK is not successfull, without any exception information, the status of sending request is 200 and still receiving the returned value of AWS_REQUEST_ID.
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(accessKey, secretKey));
Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
.withStringValue("mySenderID") //The sender ID shown on the device.
.withDataType("String"));
smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
.withStringValue("1") //Sets the max price to 0.50 USD.
.withDataType("Number"));
// Sets the type to promotional
smsAttributes.put("AWS.SNS.SMS.SMSType",
new MessageAttributeValue().withStringValue("Promotional").withDataType("String"));
JSONObject smsService = (JSONObject) new JSONObject(json).get("smsService");
String phoneNumber = smsService.getString("phoneNumber");
String message = smsService.getString("message");
PublishResult result = snsClient.publish(new PublishRequest().withMessage(message).withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
LOGGER.info("Message ID: {}", result); // Prints the message ID.
After for a while of researching, I detect that the root cause is the budget is over, so I can not send SMS anymore (I track the log to detect it).
A user will create an account on my web app. The app will need to authenticate the user by sending a text message to the mobile phone number that the user provides. The text message is a short unique code, which the user will need to type in to the web browser in order for the app to authenticate the user.
How can I configure Amazon AWS SNS for this use case?
From what I have read, SNS works by the webmaster selecting a topic and then each user subscribes to that topic. Then the webmaster sends messages broadcast style to all the subscribers to the topic. This would not meet my requirements of sending a unique message to each user. Alternatively, creating a separate topic for every phone number would be too cumbersome, not to mention creating security issues with respect to protecting the privacy of all the phone numbers.
Can Amazon AWS SNS be configured for this use case? If so, how?
I am using Java.
SNS topics are only nedded when you need to send a SMS message to multiple mobile numbers. If you want to send a SMS message to a single number you can use the Amazon AWS SDK.
Docs are available at http://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk.
Example code:
public static void main(String[] args) {
AWSCredentials awsCredentials = new BasicAWSCredentials("YOUR_Client_ID", "YOUR_Client_secret");
final AmazonSNSClient client = new AmazonSNSClient(awsCredentials);
client.setRegion(Region.getRegion(Regions.REGION_YOU_WANT_TO_USE));
AmazonSNSClient snsClient = new AmazonSNSClient(awsCredentials);
String message = "My SMS message";
String phoneNumber = "+1XXX5550100";
Map<String, MessageAttributeValue> smsAttributes =
new HashMap<String, MessageAttributeValue>();
//<set SMS attributes>
sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}
public static void sendSMSMessage(AmazonSNSClient snsClient, String message,
String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
PublishResult result = snsClient.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
System.out.println(result); // Prints the message ID.
}
Remember to create an user on IAM console adding a permission to access the SNS Service. You need to add AmazonSNSFullAccess role.
Replace YOUR_Client_ID and YOUR_Client_secret with user credentials you have created.
Some methods have been deprecated in AWS SDK.
BasicAWSCredentials awsCredentials = new BasicAWSCredentials("CLIENT-ID", "SECRET-KEY");
AmazonSNS snsClient = AmazonSNSClientBuilder.standard()
.withRegion(Regions.fromName("YOUR_REGION"))
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
smsAttributes.put("AWS.SNS.SMS.SenderID",new MessageAttributeValue().withStringValue("SENDER-ID").withDataType("String"));
smsAttributes.put("AWS.SNS.SMS.SMSType",new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));
PublishRequest request = new PublishRequest();
request.withMessage("YOUR MESSAGE")
.withPhoneNumber("E.164-PhoneNumber")
.withMessageAttributes(smsAttributes);
PublishResult result=snsClient.publish(request);
Sending an SMS using the AWS SDK for Java:
public static void main(String[] args) {
AmazonSNSClient snsClient = new AmazonSNSClient();
String message = "My SMS message";
String phoneNumber = "+1XXX5550100";
Map<String, MessageAttributeValue> smsAttributes =
new HashMap<String, MessageAttributeValue>();
//<set SMS attributes>
sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}
public static void sendSMSMessage(AmazonSNSClient snsClient, String message,
String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
PublishResult result = snsClient.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
System.out.println(result); // Prints the message ID.
}
For this particular use case you should consider the AWS Customer Engagement Service Amazon Pinpoint. It allows sending SMS, emails and push notifications, with tools for marketing campaigns as well as analytics:
Amazon Pinpoint Developer Guide
The documentation includes a tutorial for your specific use case:
Setting Up an SMS Registration System Using Amazon Pinpoint
For what you described, while it could be done using SNS with the AWS SDK for Java, Pinpoint is designed to interact with application users, so it's probably a better alternative
Individual SMS/MMS messaging is outside the apparent use cases of AWS SNS.
If you need to send proper SMS/MMS text messages to arbitrary phone numbers, you'll need to turn to other platforms, such as Twilio or Bandwidth.