Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example script to Send SMS via AWS SNS using boto

How does one send sms directly to the mobile number via AWS SNS using boto (or other python|perl library) ?

Constraints:

  • without using AWS Lambda functions
  • without using SNS topics to subscribe mobile numbers directly

My use case: sending SMS alerts from Nagios using AWS SNS using AWS SMS as the endpoint protocol.

like image 548
Traiano Welcome Avatar asked Nov 25 '16 09:11

Traiano Welcome


1 Answers

Here's the code to publish directly to a phone number via SNS using boto3. If you get an error regarding the PhoneNumber parameter, you'll need to upgrade your version boto. It's important to remember that SNS currently supports direct publish to a phone number (PhoneNumber) or push notifications endpoint (targetArn). Also note that TopicArn, PhoneNumber, and TargetArn are all mutually exclusive and therefore you can only specify one of these per publish.

import boto3

sns_client = boto3.client('sns')

response = sns_client.publish(
    PhoneNumber='+12065551212', 
    Message='This is a test SMS message',
    #TopicArn='string', (Optional - can't be used with PhoneNumer)
    #TargetArn='string', (Optional - can't be used with PhoneNumer)
    #Subject='string', (Optional - not used with PhoneNumer)
    #MessageStructure='string' (Optional)
)

print(response)
like image 99
Dennis H Avatar answered Nov 14 '22 22:11

Dennis H