diff --git a/server/.env.example b/server/.env.example index 225550f92..0609fc1af 100644 --- a/server/.env.example +++ b/server/.env.example @@ -39,6 +39,9 @@ GITHUB_SHA=DEVELOPMENT SLACK_WEBHOOK_URL= SLACK_ERROR_CODES=[400, 500] +# Sendgrid +SENDGRID_API_KEY= + ######################### DOCKER-COMPOSE SETTINGS ######################## COMPOSE_PROJECT_NAME=311_data diff --git a/server/api/src/control/route_handlers.py b/server/api/src/control/route_handlers.py index e1b4c44cd..dc9224083 100644 --- a/server/api/src/control/route_handlers.py +++ b/server/api/src/control/route_handlers.py @@ -6,7 +6,8 @@ comparison as comp_svc, github as github_svc, map as map_svc, - status as status_svc) + status as status_svc, + email as email_svc) async def index(request): @@ -92,7 +93,8 @@ async def feedback(request): 'title': to.req.STR, 'body': to.req.STR}) - issue_id = await github_svc.create_issue(args['title'], args['body']) - response = await github_svc.add_issue_to_project(issue_id) + id, number = await github_svc.create_issue(args['title'], args['body']) + await github_svc.add_issue_to_project(id) + await email_svc.respond_to_feedback(args['body'], number) - return json(response) + return json({'success': True}) diff --git a/server/api/src/services/email.py b/server/api/src/services/email.py new file mode 100644 index 000000000..173af17fe --- /dev/null +++ b/server/api/src/services/email.py @@ -0,0 +1,37 @@ +from sendgrid import SendGridAPIClient +from sendgrid.helpers.mail import Mail +from settings import Sendgrid + + +API_KEY = Sendgrid.API_KEY +FROM_ADDRESS = '311-data@hackforla.org' + + +def response(first_name, issue_number, feedback): + return f'''\ +{first_name}, + +Thank you for contacting 311-data.org. A ticket has been created (#{issue_number}) and an email has been sent to our support team. Please be patient with our response time since we are all volunteers. + +Below is the information that we received from you: +{feedback} + +Thanks again for your interest and we look forward to working with you. + +If you have any questions, feel free to contact us at 311-data@hackforla.org + +'''.replace('\n', '
') # noqa: E501 + + +async def respond_to_feedback(feedback, issue_number): + first_name, _, email, *__ = feedback.split('\n') + first_name = first_name.replace('First name: ', '') + email = email.replace('Email: ', '') + + message = Mail( + from_email=FROM_ADDRESS, + to_emails=[email, FROM_ADDRESS], + subject='Thanks for your feedback', + html_content=response(first_name, issue_number, feedback)) + + return SendGridAPIClient(API_KEY).send(message) diff --git a/server/api/src/services/github.py b/server/api/src/services/github.py index d6eeb872f..4bf03ac3a 100644 --- a/server/api/src/services/github.py +++ b/server/api/src/services/github.py @@ -41,7 +41,8 @@ async def create_issue(title, response.raise_for_status() response_content = loads(response.content) issue_id = response_content['id'] - return issue_id + issue_number = response_content['number'] + return issue_id, issue_number except requests.exceptions.HTTPError as errh: return errh except requests.exceptions.ConnectionError as errc: diff --git a/server/api/src/settings.py b/server/api/src/settings.py index ce6300fa2..2cf40ad09 100644 --- a/server/api/src/settings.py +++ b/server/api/src/settings.py @@ -65,3 +65,7 @@ class Socrata: class Slack: WEBHOOK_URL = env('SLACK_WEBHOOK_URL') ERROR_CODES = env('SLACK_ERROR_CODES', to.LIST_OF_INTS) + + +class Sendgrid: + API_KEY = env('SENDGRID_API_KEY')