Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: Include a Braze campaign id in code/offer emails.
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Jan 4, 2022
1 parent a37ae9c commit c0eef8e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ecommerce_worker/configuration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
'CAMPAIGN_SEND_ENDPOINT': '/campaigns/trigger/send',
'FROM_EMAIL': '<[email protected]>',
'ENTERPRISE_CAMPAIGN_ID': '',
'ENTERPRISE_CODE_ASSIGNMENT_CAMPAIGN_ID': '',
'ENTERPRISE_CODE_UPDATE_CAMPAIGN_ID': '',
'ENTERPRISE_CODE_USAGE_CAMPAIGN_ID': '',
'ENTERPRISE_CODE_NUDGE_CAMPAIGN_ID': '',
# Retry settings for Braze celery tasks
'BRAZE_RETRY_SECONDS': 3600,
'BRAZE_RETRY_ATTEMPTS': 6,
Expand Down
4 changes: 4 additions & 0 deletions ecommerce_worker/email/v1/braze/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def send_message( # pylint: disable=dangerous-default-value
sender_alias='EdX Support Team',
reply_to='',
attachments=[],
campaign_id='',
):
"""
Sends the message via Braze Rest API /messages/send
Expand All @@ -306,13 +307,15 @@ def send_message( # pylint: disable=dangerous-default-value
sender_alias (str): sender alias for email e.g. edX Support Team
reply_to (str): Enterprise Customer reply to address for email reply
attachments (list): list of dicts with filename and url keys
campaign_id (str): The id of the campaign this email is associated with
Request message format:
Content-Type: application/json
Authorization: Bearer {{api_key}}
Body:
{
"external_user_ids": [ "[email protected]", "[email protected]" ],
"campaign_id": "some-campaign-identifier",
"messages": {
"email": {
"app_id": "99999999-9999-9999-9999-999999999999",
Expand Down Expand Up @@ -366,6 +369,7 @@ def send_message( # pylint: disable=dangerous-default-value
message = {
'user_aliases': user_aliases,
'external_user_ids': external_ids,
'campaign_id': campaign_id,
'recipient_subscription_state': 'all',
'messages': {
'email': email
Expand Down
34 changes: 26 additions & 8 deletions ecommerce_worker/email/v1/braze/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def send_offer_assignment_email_via_braze(self, user_email, offer_assignment_id, subject, email_body, sender_alias,
reply_to, attachments, site_code): # pylint: disable=invalid-name
reply_to, attachments, site_code):
"""
Sends the offer assignment email via Braze.
Expand All @@ -31,13 +31,15 @@ def send_offer_assignment_email_via_braze(self, user_email, offer_assignment_id,
try:
user_emails = [user_email]
braze_client = get_braze_client(site_code)
response = braze_client.send_message(
response = _send_braze_message(
braze_client,
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias,
reply_to=reply_to,
attachments=attachments
attachments=attachments,
campaign_id=config.get('ENTERPRISE_CODE_ASSIGNMENT_CAMPAIGN_ID'),
)
if response and response['success']:
dispatch_id = response['dispatch_id']
Expand Down Expand Up @@ -81,13 +83,15 @@ def send_offer_update_email_via_braze(self, user_email, subject, email_body, sen
try:
user_emails = [user_email]
braze_client = get_braze_client(site_code)
braze_client.send_message(
_send_braze_message(
braze_client,
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias,
reply_to=reply_to,
attachments=attachments
attachments=attachments,
campaign_id=config.get('ENTERPRISE_CODE_UPDATE_CAMPAIGN_ID'),
)
except (BrazeRateLimitError, BrazeInternalServerError) as exc:
raise self.retry(countdown=config.get('BRAZE_RETRY_SECONDS'),
Expand Down Expand Up @@ -116,12 +120,14 @@ def send_offer_usage_email_via_braze(self, emails, subject, email_body, reply_to
try:
user_emails = list(emails.strip().split(","))
braze_client = get_braze_client(site_code)
braze_client.send_message(
_send_braze_message(
braze_client,
email_ids=user_emails,
subject=subject,
body=email_body,
reply_to=reply_to,
attachments=attachments
attachments=attachments,
campaign_id=config.get('ENTERPRISE_CODE_USAGE_CAMPAIGN_ID'),
)
except (BrazeRateLimitError, BrazeInternalServerError) as exc:
raise self.retry(countdown=config.get('BRAZE_RETRY_SECONDS'),
Expand Down Expand Up @@ -152,13 +158,15 @@ def send_code_assignment_nudge_email_via_braze(self, email, subject, email_body,
try:
user_emails = [email]
braze_client = get_braze_client(site_code)
braze_client.send_message(
_send_braze_message(
braze_client,
email_ids=user_emails,
subject=subject,
body=email_body,
sender_alias=sender_alias,
reply_to=reply_to,
attachments=attachments,
campaign_id=config.get('ENTERPRISE_CODE_NUDGE_CAMPAIGN_ID'),
)
except (BrazeRateLimitError, BrazeInternalServerError) as exc:
raise self.retry(countdown=config.get('BRAZE_RETRY_SECONDS'),
Expand All @@ -168,3 +176,13 @@ def send_code_assignment_nudge_email_via_braze(self, email, subject, email_body,
'[Code Assignment Nudge Email] Error in offer nudge notification with message --- '
'{message}'.format(message=email_body)
)


def _send_braze_message(braze_client, **kwargs):
"""
Helper to send braze messages. Pops any falsey `campaign_id`
argument before sending.
"""
if 'campaign_id' in kwargs and not kwargs['campaign_id']:
kwargs.pop('campaign_id')
return braze_client.send_message(**kwargs)
4 changes: 4 additions & 0 deletions ecommerce_worker/email/v1/braze/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class SendEmailsViaBrazeTests(TestCase):
'EXPORT_ID_ENDPOINT': '/users/export/ids',
'CAMPAIGN_SEND_ENDPOINT': '/campaigns/trigger/send',
'ENTERPRISE_CAMPAIGN_ID': '',
'ENTERPRISE_CODE_ASSIGNMENT_CAMPAIGN_ID': 'the-code-assignment-campaign-id',
'ENTERPRISE_CODE_UPDATE_CAMPAIGN_ID': 'the-code-update-campaign-id',
'ENTERPRISE_CODE_USAGE_CAMPAIGN_ID': 'the-code-usage-campaign-id',
'ENTERPRISE_CODE_NUDGE_CAMPAIGN_ID': '', # cover case where a `campaign_id` kwarg is falsey.
'FROM_EMAIL': '<[email protected]>',
'BRAZE_RETRY_SECONDS': 3600,
'BRAZE_RETRY_ATTEMPTS': 6,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def is_requirement(line):

setup(
name='edx-ecommerce-worker',
version='3.0.0',
version='3.1.0',
description='Celery tasks supporting the operations of edX\'s ecommerce service',
long_description=long_description,
classifiers=[
Expand Down

0 comments on commit c0eef8e

Please sign in to comment.