Skip to content

Instantly share code, notes, and snippets.

@mariuz
Created April 6, 2021 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariuz/51bd2dc82e8981891d62d44dc2376b52 to your computer and use it in GitHub Desktop.
Save mariuz/51bd2dc82e8981891d62d44dc2376b52 to your computer and use it in GitHub Desktop.
Test sns phone text sending using python sample from AWS https://docs.aws.amazon.com/code-samples/latest/catalog/python-sns-sns_basics.py.html
import json
import logging
import time
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
sns_resource = boto3.resource('sns')
def publish_text_message(phone_number, message):
"""
Publishes a text message directly to a phone number without need for a
subscription.
:param phone_number: The phone number that receives the message. This must be
in E.164 format. For example, a United States phone
number might be +12065550101.
:param message: The message to send.
:return: The ID of the message.
"""
try:
response = sns_resource.meta.client.publish(
PhoneNumber=phone_number, Message=message)
message_id = response['MessageId']
logger.info("Published message to %s.", phone_number)
except ClientError:
logger.exception("Couldn't publish message to %s.", phone_number)
raise
else:
return message_id
publish_text_message('+12025550113','text')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment