Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscriber SMS functionality #90

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/createsend/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get(self, list_id=None, email_address=None, include_tracking_preference=Fals
(list_id or self.list_id), params=params)
return json_to_py(response)

def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False, mobile_number=False, consent_to_track_sms="Unchanged"):
"""Adds a subscriber to a subscriber list."""
validate_consent_to_track(consent_to_track)
body = {
Expand All @@ -32,11 +32,17 @@ def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_
"Resubscribe": resubscribe,
"ConsentToTrack": consent_to_track,
"RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders}

if mobile_number:
body["MobileNumber"] = mobile_number
validate_consent_to_track(consent_to_track_sms)
body["ConsentToSendSms"] = consent_to_track_sms

response = self._post("/subscribers/%s.json" %
list_id, json.dumps(body))
return json_to_py(response)

def update(self, new_email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
def update(self, new_email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False, mobile_number=False, consent_to_track_sms="Unchanged"):
"""Updates any aspect of a subscriber, including email address, name, and
custom field data if supplied."""
validate_consent_to_track(consent_to_track)
Expand All @@ -48,6 +54,12 @@ def update(self, new_email_address, name, custom_fields, resubscribe, consent_to
"Resubscribe": resubscribe,
"ConsentToTrack": consent_to_track,
"RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders}

if mobile_number:
body["MobileNumber"] = mobile_number
validate_consent_to_track(consent_to_track_sms)
body["ConsentToSendSms"] = consent_to_track_sms

response = self._put("/subscribers/%s.json" % self.list_id,
body=json.dumps(body), params=params)
# Update self.email_address, so this object can continue to be used
Expand Down Expand Up @@ -93,4 +105,4 @@ def delete(self):
"""Moves this subscriber to the deleted state in the associated list."""
params = {"email": self.email_address}
response = self._delete("/subscribers/%s.json" %
self.list_id, params=params)
self.list_id, params=params)
20 changes: 20 additions & 0 deletions samples/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
listId = 'YOUR_LIST_ID'
emailAddress = 'YOUR_SUBSCRIBER_EMAIL_ADDRESS'

subscriberName = "YOUR_SUBSCRIBER_NAME"
subscriberCustomFields = []
subscriberResubscribed = False
subscriberConsentToTrack = 'Unchanged'
subscriberMobileNumber = "+61491570006" # This is a reserved mobile number by the Australian Communications and Media Authority
subscriberConsentToSendSms = "Yes"

subscriber = Subscriber(auth, listId, emailAddress)

# Get the details for a subscriber
subscriberDetail = subscriber.get()
for property, value in vars(subscriberDetail).items():
print(property, ":", value)

# Adding a subscriber
#This implemntation defaults the value of 'restart_subscription_based_autoresponders' to false
subscriber.add(listId, emailAddress, subscriberName, subscriberCustomFields, subscriberResubscribed, subscriberConsentToTrack)

# Adding a subscriber with a mobile number
#This implemntation defaults the value of 'restart_subscription_based_autoresponders' to false
#This also sets the default value of 'consent_to_track_sms' to 'unchanged', meaning new users will not receive SMS communications by default."
subscriber.add(listId, emailAddress, subscriberName, subscriberCustomFields, subscriberConsentToTrack, mobile_Number=subscriberMobileNumber)

#Alternative to set SMS tracking permissions
# This implemntation defaults the value of 'restart_subscription_based_autoresponders' to false
subscriber.add(listId, emailAddress, subscriberName, subscriberCustomFields, subscriberConsentToTrack, mobile_Number=subscriberMobileNumber, consent_to_track_sms=subscriberConsentToSendSms)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="createsend",
version='9.0.2',
version='9.1.2',
description="A library which implements the complete functionality of the Campaign Monitor API.",
author='Campaign Monitor',
author_email='[email protected]',
Expand Down