Created
December 13, 2018 19:21
-
-
Save craigderington/c2b99ddff3af5dfbb207447a805998fb to your computer and use it in GitHub Desktop.
Flask REST Mailgun Webhook v3
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
try: | |
lead = db.session.query(Lead).filter( | |
Lead.email_addr == mg_recipient | |
).first() | |
if lead: | |
email = lead.email | |
lead_id = lead.id | |
event = form_data['event'] | |
# set the delivered flags in the database | |
lead.followup_email_delivered = 1 | |
lead.followup_email_status = event | |
lead.webhook_last_updated = datetime.now() | |
db.session.commit() | |
# return a successful response | |
return jsonify({ | |
"l_id": lead_id, | |
"email": email, | |
"event": event, | |
"status": 'success'}), 202 | |
# return 404: no email for recipient email address | |
else: | |
resp = {"Error": "Unable to resolve the recipient email address..."} | |
data = json.dumps(resp) | |
return Response(data, status=404, mimetype='application/json') | |
# database exception | |
except exc.SQLAlchemyError as db_err: | |
resp = {"Database Error": str(db_err)} | |
data = json.dumps(resp) | |
return Response(data, status=500, mimetype='application/json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment