1

I need to send 200 sms messages, and looking in the amazon documentation I found how to do this by subscribing to a topic but only one by one.

public static void main(String[] args) {
  AmazonSNSClient snsClient = new AmazonSNSClient();
  String phoneNumber = "+1XXX5550100";
  String topicArn = createSNSTopic(snsClient);
  subscribeToTopic(snsClient, topicArn, "sms", phoneNumber);
}


public static void subscribeToTopic(AmazonSNSClient snsClient, String topicArn, 
        String protocol, String endpoint) { 
        SubscribeRequest subscribe = new SubscribeRequest(topicArn, protocol,
                                                          endpoint);
        SubscribeResult subscribeResult = snsClient.subscribe(subscribe);
}

Is there any way I send a list of phone numbers to the endpoint, or I subscribe a list of SubscribeRequest?

2 Answers2

2

Currently, you cannot pass a list of phone numbers as an endpoint when you create subscription for a SNS Topic. Each subscription can have only ONE phone number as an endpoint.

For emails, we can just provide the group email-id, and the email server will handle the distribution list. But something similar is not possible for phone numbers. As far as SNS is concerned, it needs a single endpoint for a selected protocol(SMS/EMAIL).

Just to simplify things, what you do is you can maintain a list for the phone numbers in your code. You can loop through the list and call the subscribeToTopic method each time with the same topic ARN but different phone number. But I am sure you would have thought about this yourself.

Madhukar Mohanraju
  • 2,793
  • 11
  • 28
  • thx man, i did the loop but i thought that should have a better way ... Could you give me an example of the e-mail? – Thiago Melin Oct 19 '17 at 22:38
0

We can pass a list of phone numbers to an endpoint using AWS SDKs.

If you need to send messages to multiple recipients, it's worthwhile to read though Amazon's docs: (https://docs.amazonaws.cn/en_us/sns/latest/dg/sms_publish-to-topic.html) on sending to multiple phone numbers.

The SNS service implements the Publish-Subscribe pattern, and you can use it to send messages to a topic. Here are the steps to make this work:

  1. Create a named topic. This is just a communication channel to which you can subscribe phone numbers.
  2. Subscribe your recipients to the topic.
  3. Publish a message on the topic.

The python code looks something like this:

import boto3

# Create an SNS client
client = boto3.client(
    "sns",
    aws_access_key_id="YOUR ACCES KEY",
    aws_secret_access_key="YOUR SECRET KEY",
    region_name=us-east-1
)

# Create the topic if it doesn't exist
topic = client.create_topic(Name="invites-for-push-notifications")
topic_arn = topic['TopicArn']  # get its Amazon Resource Name

#Get List of Contacts
list_of_contacts = ["+919*********", "+917********", "+918********"]

# Add SMS Subscribers
for number in some_list_of_contacts:
    client.subscribe(
        TopicArn=topic_arn,
        Protocol='sms',
        Endpoint=number  # <-- number who'll receive an SMS message.
    )

# Publish a message.
client.publish(Message="Good news everyone!", TopicArn=topic_arn)