简体   繁体   English

如何为aws-sns主题订阅电话号码列表

[英]how to subscribe a list of phone numbers to a aws-sns topic

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. 我需要发送200条短信,然后查看亚马逊文档,找到了如何通过订阅一个主题(但只能一个一个地订阅)来做到这一点的方法。

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? 有什么方法可以将电话号码列表发送到端点,或者我可以订阅SubscribeRequest列表?

Currently, you cannot pass a list of phone numbers as an endpoint when you create subscription for a SNS Topic. 当前,在创建SNS主题的订阅时,您无法list of phone numbers作为端点传递。 Each subscription can have only ONE phone number as an endpoint. 每个订阅只能有ONE电话号码作为端点。

For emails, we can just provide the group email-id, and the email server will handle the distribution list. 对于电子邮件,我们只提供组电子邮件ID,电子邮件服务器将处理通讯组列表。 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) . 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 . 您可以遍历该列表,并每次使用same topic ARN but different phone number调用subscribeToTopic方法。 But I am sure you would have thought about this yourself. 但是我敢肯定,您自己会考虑过这一点。

We can pass a list of phone numbers to an endpoint using AWS SDKs. 我们可以使用AWS开发工具包将电话号码列表传递给端点。

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. 如果您需要向多个收件人发送消息,则值得阅读亚马逊的文档:( https://docs.amazonaws.cn/en_us/sns/latest/dg/sms_publish-to-topic.html )在发送至多部电话时数字。

The SNS service implements the Publish-Subscribe pattern, and you can use it to send messages to a topic. SNS服务实现了“发布-订阅”模式,您可以使用它将消息发送到主题。 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: python代码看起来像这样:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM