Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import boto3 

2import requests 

3from gramex.http import OK 

4from gramex.config import app_log 

5 

6 

7class Notifier(object): 

8 def send(self, to, subject, sender): 

9 ''' 

10 Send an SMS to the ``to`` mobile with ``subject`` as the contents. ``sender`` optional. 

11 

12 Return API-specific response object. 

13 Raise Exception if API access fails. (This does not guarantee SMS delivery.) 

14 ''' 

15 raise NotImplementedError() 

16 

17 def status(self, result): 

18 ''' 

19 Returns the delivery status of the SMS. The ``result`` is the output from ``.send()``. 

20 ''' 

21 raise NotImplementedError() 

22 

23 

24class AmazonSNS(Notifier): 

25 ''' 

26 Send messages via AmazonSNS:: 

27 

28 >>> notifier = AmazonSNS( 

29 ... aws_access_key_id='...', 

30 ... aws_secret_access_key='...', 

31 ... region_name='ap-southeast-1', 

32 ... smstype='Transactional') 

33 >>> notifier.send( 

34 ... to='+919741552552', 

35 ... subject='This is the content of the message', 

36 ... sender='gramex') 

37 ''' 

38 

39 def __init__(self, aws_access_key_id, aws_secret_access_key, 

40 smstype='Transactional', **kwargs): 

41 self.client = boto3.client( 

42 'sns', 

43 aws_access_key_id=aws_access_key_id, 

44 aws_secret_access_key=aws_secret_access_key, 

45 **kwargs) 

46 self.smstype = smstype 

47 

48 def send(self, to, subject, sender): 

49 result = self.client.publish( 

50 PhoneNumber=to, 

51 Message=subject, 

52 MessageAttributes={ 

53 'AWS.SNS.SMS.SenderID': { 

54 'DataType': 'String', 

55 'StringValue': sender, 

56 }, 

57 'AWS.SNS.SMS.SMSType': { 

58 'DataType': 'String', 

59 'StringValue': self.smstype, 

60 } 

61 } 

62 ) 

63 app_log.info('SMS sent. SNS MessageId: %s', result['MessageId']) 

64 return result 

65 

66 

67class Exotel(Notifier): 

68 ''' 

69 Send messages via Exotel:: 

70 

71 >>> notifier = Exotel( 

72 ... sid='...', 

73 ... token='...', 

74 ... priority='high', 

75 ... ) 

76 >>> notifier.send( 

77 ... to='+919741552552', 

78 ... subject='This is the content of the message', 

79 ... sender='gramex') 

80 ''' 

81 def __init__(self, sid, token, priority='high'): 

82 self.sid = sid 

83 self.token = token 

84 self.priority = priority 

85 self.send_url = 'https://{}:{}@api.exotel.com/v1/Accounts/{}/Sms/send.json'.format( 

86 self.sid, self.token, self.sid) 

87 self.stat_url = 'https://{}:{}@api.exotel.com/v1/Accounts/{}/SMS/Messages/%s.json'.format( 

88 self.sid, self.token, self.sid) 

89 

90 def _handle_response(self, r): 

91 if r.status_code != OK: 

92 raise RuntimeError('Exotel API failed: %s' % r.text) 

93 result = r.json() 

94 return result['SMSMessage'] 

95 

96 def send(self, to, subject, sender=None): 

97 r = requests.post(self.send_url, { 

98 'From': sender or self.sid, 

99 'To': to, 

100 'Body': subject, 

101 'Priority': self.priority, 

102 }) 

103 return self._handle_response(r) 

104 

105 def status(self, result): 

106 r = requests.get(self.stat_url % result['Sid']) 

107 return self._handle_response(r) 

108 

109 

110class Twilio(Notifier): 

111 def __init__(self, account_sid, auth_token, **kwargs): 

112 raise NotImplementedError()