SMS with SNS

Recently, my friend Wishah started a Design course and needed a way to send SMS to the participants. I have previously meddled with Amazon SNS and it was pretty good. Hence, I recommended it to him and suggested I made a small script for him.

Mission: Make a script to send SMS via Amazon SNS

Prerequisites:
1. Amazon Web Service Account (Region set to Asia Pacific: Singapore)
2. Basic python knowledge
3. Writing decent SMS for Dummies Edition 2

As usual we are using our favorite programming language, Python. First we will install the package need.

pip install boto3

Next we will write the script. Technically, I write the script and you just copy it.

import boto3
import csv
import sys

#Create an SNS Client
client = boto3.client(
    "sns",
    aws_access_key_id="XXXXXXXXXX",
    aws_secret_access_key="XXXXXXX/XXXXX/XXXXX",
    region_name="ap-southeast-1"
)

with open('numbers.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        name = row[0]
        number = row[1]
        client.publish (
            PhoneNumber = number,
            Message="Hey " + name + ", Hey, Sup bro?",
            MessageAttributes={
                'AWS.SNS.SMS.SenderID': 
				{
                    'DataType':'String',
                    'StringValue':'AruhamDev'
                }
            }
        )
        success = 'Message sent to ' + name
        print success

Here is what happens in the script.
1. We create an SNS client using our AWS access and secret access keys.
2. We open a file called numbers.csv which has the names and numbers saved separated by commas.
3. We publish the sms using client.
4. We print the success message

Yes, the communism is strong with this one. WE!

Note: The SenderID -> StringValue is the custom sender name you would want to set.
The numbers.csv file should have a name and number in each row separated by comma( , ). The number should include the country code (example: +9607777777) .

Pew Onii-chan sent me a message ^__* Kawaii