-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add newsletter sign up serverless function.
- Loading branch information
Showing
6 changed files
with
236 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,11 @@ | |
|
||
CONTACT_FORM_FROM_EMAIL = '[email protected]' | ||
CONTACT_FORM_TO_EMAIL = '[email protected]' | ||
|
||
# WARNING: Crisp Development Token is connected to the PRODUCTION environment, | ||
# since Crisp has no concept of test environment, so be careful. | ||
# | ||
# Development Token is limited to 500/requests day. | ||
# If needed you can reset the limit here (see login credentials in Notion): | ||
# https://marketplace.crisp.chat/plugins/plugin/421c7588-7ed5-464a-a1f7-8917ae9bafe7/tokens/ | ||
CRISP_WEBSITE_ID = '6de2ec98-888b-4ff4-b9be-dab91a460ee6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import logging | ||
import os | ||
from json import JSONDecodeError | ||
|
||
import boto3 | ||
import pipedrive | ||
import sentry_sdk | ||
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | ||
from crisp_api import Crisp | ||
|
||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
sentry_sdk.init( | ||
dsn="https://[email protected]" | ||
+ "/4506218347954176", | ||
integrations=[AwsLambdaIntegration()], | ||
traces_sample_rate=1.0, | ||
profiles_sample_rate=1.0, | ||
) | ||
|
||
|
||
def add_crisp_contact(event_body): | ||
logger.info('add_crisp_contact(): start') | ||
crisp = Crisp() | ||
crisp.set_tier('plugin') | ||
website_id = os.environ['CRISP_WEBSITE_ID'] | ||
token_id = os.environ['CRISP_TOKEN_ID'] | ||
token_key = os.environ['CRISP_TOKEN_KEY'] | ||
|
||
if not (website_id and token_id and token_key): | ||
warnings.warn('Crisp credentials are missing!') | ||
return | ||
|
||
crisp.authenticate(token_id, token_key) | ||
|
||
try: | ||
contact_data = crisp.website.add_new_people_profile( | ||
website_id=website_id, | ||
data={ | ||
'email': event_body['email'], | ||
'person': {'nickname': event_body['email']}, | ||
'segments': 'newsletter', | ||
}, | ||
) | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
logger.exception('add_crisp_contact(): add_crisp_contact failed') | ||
return | ||
|
||
logger.info('add_crisp_contact(): success') | ||
|
||
|
||
def lambda_handler(event): | ||
assert event.get('email') is not None | ||
|
||
logger.info('lambda_handler(): invoked. event={}'.format(event)) | ||
try: | ||
add_crisp_contact(event) | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
logger.exception('lambda_handler(): add_crisp_contact failed') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ dependencies = [ | |
"boto3==1.29.1", | ||
"requests==2.31.0", | ||
"sentry-sdk==1.35.0", | ||
"crisp-api==1.1.19", | ||
] |
Oops, something went wrong.