Odyssey
sendAwsSms.py
1 # ##############
2 # sendAwsSms.py
3 #
4 # This will send an sms message using the AWS SNS services
5 #
6 # Arguments
7 # -p - (required) This is the phone number that will receive the sms
8 # -s - (required) This is the subject that is sent on the message
9 # -m - (required) This is the message to be sent. Messages longer than AWS max will automatically
10 # be split to more messages
11 # -t - (optional) For Transactional -- Critical Messages (** Otherwise the message is sent as promotional)
12 # -r - (optional) us-west-2 (default)
13 
14 import sys
15 import getopt
16 import boto3
17 import os
18 import json
19 from botocore.exceptions import ClientError
20 
21 # ###########
22 # GLOBAL SETTINGS
23 HOMECU_SCRIPT_HELP = 'sendAwsSms.py -p <phone number> -s <subject> -m <message> [-t] [-r <region>]'
24 
25 # SENDER ID -- NOT SUPPORTED IN US
26 
27 # Always use us-west-2.. This should work for local development, alpha stack(us-east-2) and prod
28 AWS_REGION = 'us-west-2'
29 
30 
31 def main(argv):
32 
33  main_return = {
34  'status': '000',
35  'error': '',
36  'response': {}
37  }
38 
39  # ### PARSE ARGUMENTS
40  try:
41  opts, args = getopt.getopt(argv, "p:s:m:r:t")
42  except getopt.GetoptError:
43  main_return['status'] = '999'
44  main_return['error'] = HOMECU_SCRIPT_HELP
45  print(json.dumps(main_return))
46  sys.exit(2)
47 
48  missing_phone = True
49  missing_subject = True
50  missing_message = True
51  arg_transaction = False
52 
53  for opt, arg in opts:
54  if opt == '-p':
55  arg_phone = arg
56  missing_phone = False
57  elif opt == '-s':
58  sms_subject = arg
59  missing_subject = False
60  elif opt == '-m':
61  sms_message = arg
62  missing_message = False
63  elif opt == '-t':
64  arg_transaction = True
65  elif opt == '-r':
66  arg_region = arg
67 
68 
69  if missing_phone or missing_message or missing_subject:
70  main_return['status'] = '999'
71  main_return['error'] = HOMECU_SCRIPT_HELP
72  print (json.dumps(main_return))
73  sys.exit(2)
74 
75 
76  # ########
77  # SANITIZE
78 
79  # PHONE NUMBER
80  if len(arg_phone) != 11 or not arg_phone.isdigit():
81  main_return['status'] = '999'
82  main_return['error'] = "Unsupported phone number"
83  print (json.dumps(main_return))
84  sys.exit(2)
85  else:
86  sms_phone_nbr = arg_phone
87 
88 
89  # CONTINUE
90 
91  # SET SMS TYPE
92  if arg_transaction:
93  homecu_sms_type = 'Transactional'
94  else:
95  homecu_sms_type = 'Promotional'
96 
97  # SETTINGS
98  # ###########
99 
100  try:
101  # OPEN boto3 client
102  client = boto3.client('sns', region_name=AWS_REGION)
103 
104  # DefaultSMSType
105  # Transactional (Critical for MFA)
106  # Promotional (Default)
107  response = client.set_sms_attributes(
108  attributes={
109  'DefaultSMSType': homecu_sms_type
110  }
111  )
112 
113  # ###############
114  # PUBLISH MESSAGE
115  response = client.publish(
116  PhoneNumber = sms_phone_nbr,
117  Message = sms_message,
118  Subject = sms_subject
119  )
120  except Exception as e:
121  main_return['status'] = '999'
122  main_return['error'] = e
123  print (json.dumps(main_return))
124  sys.exit(2)
125 
126  # SUCCESS
127  main_return['response'] = response
128  print (json.dumps(main_return));
129 
130 if __name__ == "__main__":
131  main(sys.argv[1:])