Skip to content

Commit f6f86e0

Browse files
committed
add support for sending transactional in-app messages
Add SendInAppRequest class and send_in_app method to APIClient for the POST /v1/send/in_app endpoint. Follows the same pattern as the existing inbox message support.
1 parent 47e0bc6 commit f6f86e0

3 files changed

Lines changed: 66 additions & 3 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, SendSMSRequest, SendInboxMessageRequest
5+
from customerio.api import APIClient, SendEmailRequest, SendPushRequest, SendSMSRequest, SendInboxMessageRequest, SendInAppRequest
66
from customerio.regions import Regions

customerio/api.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def send_inbox_message(self, request):
4040
resp = self.send_request('POST', self.url + "/v1/send/inbox_message", request)
4141
return json.loads(resp)
4242

43+
def send_in_app(self, request):
44+
if isinstance(request, SendInAppRequest):
45+
request = request._to_dict()
46+
resp = self.send_request('POST', self.url + "/v1/send/in_app", request)
47+
return json.loads(resp)
48+
4349
# builds the session.
4450
def _build_session(self):
4551
session = super()._build_session()
@@ -311,3 +317,44 @@ def _to_dict(self):
311317
data[name] = value
312318

313319
return data
320+
321+
class SendInAppRequest(object):
322+
'''An object with all the options available for triggering a transactional in-app message'''
323+
def __init__(self,
324+
transactional_message_id=None,
325+
identifiers=None,
326+
disable_message_retention=None,
327+
queue_draft=None,
328+
message_data=None,
329+
send_at=None,
330+
language=None,
331+
):
332+
333+
self.transactional_message_id = transactional_message_id
334+
self.identifiers = identifiers
335+
self.disable_message_retention = disable_message_retention
336+
self.queue_draft = queue_draft
337+
self.message_data = message_data
338+
self.send_at = send_at
339+
self.language = language
340+
341+
def _to_dict(self):
342+
'''Build a request payload from the object'''
343+
field_map = dict(
344+
# field name is the same as the payload field name
345+
transactional_message_id="transactional_message_id",
346+
identifiers="identifiers",
347+
disable_message_retention="disable_message_retention",
348+
queue_draft="queue_draft",
349+
message_data="message_data",
350+
send_at="send_at",
351+
language="language",
352+
)
353+
354+
data = {}
355+
for field, name in field_map.items():
356+
value = getattr(self, field, None)
357+
if value is not None:
358+
data[name] = value
359+
360+
return data

tests/test_api.py

Lines changed: 18 additions & 2 deletions
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, SendSMSRequest, SendInboxMessageRequest, Regions, CustomerIOException
8+
from customerio import APIClient, SendEmailRequest, SendPushRequest, SendSMSRequest, SendInboxMessageRequest, SendInAppRequest, Regions, CustomerIOException
99
from customerio.__version__ import __version__ as ClientVersion
1010
from tests.server import HTTPSTestCase
1111

@@ -126,6 +126,22 @@ def test_send_inbox_message(self):
126126
)
127127

128128
self.client.send_inbox_message(inbox_message)
129-
129+
130+
def test_send_in_app(self):
131+
self.client.http.hooks = dict(response=partial(self._check_request, rq={
132+
'method': 'POST',
133+
'authorization': "Bearer app_api_key",
134+
'content_type': 'application/json',
135+
'url_suffix': '/v1/send/in_app',
136+
'body': {"identifiers": {"id":"customer_1"}, "transactional_message_id": 100}
137+
}))
138+
139+
in_app = SendInAppRequest(
140+
identifiers={"id":"customer_1"},
141+
transactional_message_id=100,
142+
)
143+
144+
self.client.send_in_app(in_app)
145+
130146
if __name__ == '__main__':
131147
unittest.main()

0 commit comments

Comments
 (0)