Skip to content

Instantly share code, notes, and snippets.

@bpodgursky
Created January 27, 2020 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpodgursky/ccfdea81578bff076d2128b2a8a8199e to your computer and use it in GitHub Desktop.
Save bpodgursky/ccfdea81578bff076d2128b2a8a8199e to your computer and use it in GitHub Desktop.
CCPA report generator
import base64
import json
import sqlalchemy
import requests
# This application handles data export approvals and rejections, as
# forwarded from the Slack approval bot.
# - on approval, the requested email is sent a formatted data export
# - on rejection, the requested email is alerted about the rejection
# Data is fetched from "Example Users DB", keyed on email
db = CloudWright.get_module("mysql")
# Email the user a generated report
emailer = CloudWright.get_module("gmail")
# The slack module validates that the request was valid
slack = CloudWright.get_module("slack")
def build_email(action):
# If the user rejected the request in Slack, send a polite reply
if action is 'Reject':
return 'Export rejected. Send a better picture.'
else:
# Look up the user's data and format a reply. In practice, this logic will be more
# complex, likely searching logs or several databases.
query = sqlalchemy.text("SELECT name,favorite_food FROM users WHERE email=:email")
record = db.execute(query, email=email).fetchone()
if record:
return f"Here's what we know:<br/><br/>Your name: {record[0]}<br/>Your favorite food: {record[1]}"
else:
return "We don't have any data about you."
if slack.cloudwright_validate_request():
# Pull the user-generated action from the Slack message payload
payload = CloudWright.inputs.get('payload')
parsed_payload = json.loads(payload)
action = parsed_payload.get('actions')[0]
approve = action.get('text').get('text').strip()
# Extract the email and name of the requester
data = json.loads(base64.b64decode(action.get('value')))
email = data.get('email')
name = data.get('name')
# Generate our reply message and send it using the attached gmail module
message = build_email(approve)
emailer.send_email('Your CCPA data export', message, email)
# Mark the message as processed in Slack
res = requests.post(parsed_payload.get('response_url'), json={
"replace_original": "true",
"text": name +": " +approve+" :white_check_mark:"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment