How does one send sms directly to the mobile number via AWS SNS using boto (or other python|perl library) ?
Constraints:
My use case: sending SMS alerts from Nagios using AWS SNS using AWS SMS as the endpoint protocol.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With