Skip to content

Commit 248ebdb

Browse files
committed
transactional sms
1 parent bfc6707 commit 248ebdb

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

customerio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
from customerio.client_base import CustomerIOException
44
from customerio.track import CustomerIO
5-
from customerio.api import APIClient, SendEmailRequest, SendPushRequest
5+
from customerio.api import APIClient, SendEmailRequest, SendPushRequest, SendSMSRequest
66
from customerio.regions import Regions

customerio/api.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def send_push(self, request):
2727
request = request._to_dict()
2828
resp = self.send_request('POST', self.url + "/v1/send/push", request)
2929
return json.loads(resp)
30+
31+
def send_sms(self, request):
32+
if isinstance(request, SendSMSRequest):
33+
request = request._to_dict()
34+
resp = self.send_request('POST', self.url + "/v1/send/sms", request)
35+
return json.loads(resp)
3036

3137
# builds the session.
3238
def _build_session(self):
@@ -211,3 +217,50 @@ def _to_dict(self):
211217
data[name] = value
212218

213219
return data
220+
221+
class SendSMSRequest(object):
222+
'''An object with all the options avaiable for triggering a transactional push message'''
223+
def __init__(self,
224+
transactional_message_id=None,
225+
to=None,
226+
identifiers=None,
227+
disable_message_retention=None,
228+
send_to_unsubscribed=None,
229+
queue_draft=None,
230+
message_data=None,
231+
send_at=None,
232+
language=None,
233+
):
234+
235+
self.transactional_message_id = transactional_message_id
236+
self.to = to
237+
self.identifiers = identifiers
238+
self.disable_message_retention = disable_message_retention
239+
self.send_to_unsubscribed = send_to_unsubscribed
240+
self.queue_draft = queue_draft
241+
self.message_data = message_data
242+
self.send_at = send_at
243+
self.language = language
244+
245+
def _to_dict(self):
246+
'''Build a request payload from the object'''
247+
field_map = dict(
248+
# field name is the same as the payload field name
249+
transactional_message_id="transactional_message_id",
250+
to="to",
251+
identifiers="identifiers",
252+
disable_message_retention="disable_message_retention",
253+
send_to_unsubscribed="send_to_unsubscribed",
254+
queue_draft="queue_draft",
255+
message_data="message_data",
256+
send_at="send_at",
257+
language="language",
258+
)
259+
260+
data = {}
261+
for field, name in field_map.items():
262+
value = getattr(self, field, None)
263+
if value is not None:
264+
data[name] = value
265+
266+
return data

tests/test_api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import unittest
77

8-
from customerio import APIClient, SendEmailRequest, SendPushRequest, Regions, CustomerIOException
8+
from customerio import APIClient, SendEmailRequest, SendPushRequest, SendSMSRequest, Regions, CustomerIOException
99
from customerio.__version__ import __version__ as ClientVersion
1010
from tests.server import HTTPSTestCase
1111

@@ -95,5 +95,21 @@ def test_send_push(self):
9595

9696
self.client.send_push(push)
9797

98+
def test_send_sms(self):
99+
self.client.http.hooks = dict(response=partial(self._check_request, rq={
100+
'method': 'POST',
101+
'authorization': "Bearer app_api_key",
102+
'content_type': 'application/json',
103+
'url_suffix': '/v1/send/sms',
104+
'body': {"identifiers": {"id":"customer_1"}, "transactional_message_id": 100}
105+
}))
106+
107+
push = SendSMSRequest(
108+
identifiers={"id":"customer_1"},
109+
transactional_message_id=100,
110+
)
111+
112+
self.client.send_sms(push)
113+
98114
if __name__ == '__main__':
99115
unittest.main()

0 commit comments

Comments
 (0)