Last active
April 12, 2019 10:27
-
-
Save jinyu121/24e56fb3ad7be349800b44449d7f0bdf to your computer and use it in GitHub Desktop.
亚马逊发送短信示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import boto3 | |
import argparse | |
AWS_ACCESS_KEY = "AWS_ACCESS_KEY_HERE" | |
AWS_SECRET_KEY = "AWS_SECRET_KEY_HERE" | |
AWS_REGION_NAME = "AWS_REGION_NAME_HERE" | |
def send_message(sms_list, message="Test", topic_name="notifications"): | |
# Create an SNS client | |
client = boto3.client( | |
"sns", | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_KEY, | |
region_name=AWS_REGION_NAME | |
) | |
# Create the topic if it doesn't exist (this is idempotent) | |
topic = client.create_topic(Name=topic_name) | |
topic_arn = topic['TopicArn'] | |
# Add SMS Subscribers | |
for number in sms_list: | |
client.subscribe( | |
TopicArn=topic_arn, | |
Protocol='sms', | |
Endpoint=number | |
) | |
# Publish a message | |
client.publish(Message=message, TopicArn=topic_arn) | |
def main(args): | |
sms_list = args.number | |
message = " ".join(args.message) | |
send_message(sms_list=sms_list, message=message) | |
if "__main__" == __name__: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('message', type=str, nargs='+', default="测试消息", help="短信内容") | |
parser.add_argument('--number', '-n', type=str, nargs='+', help="接收号码") | |
args = parser.parse_args() | |
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python sms.py This is a test message --number +8617000000000 +8613000000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment