diff --git a/static/images/integrations/logos/delighted.png b/static/images/integrations/logos/delighted.png new file mode 100644 index 0000000000..234362b185 Binary files /dev/null and b/static/images/integrations/logos/delighted.png differ diff --git a/zerver/fixtures/delighted/delighted_survey_response_updated_non_promoter.json b/zerver/fixtures/delighted/delighted_survey_response_updated_non_promoter.json new file mode 100644 index 0000000000..18c0241dc1 --- /dev/null +++ b/zerver/fixtures/delighted/delighted_survey_response_updated_non_promoter.json @@ -0,0 +1,21 @@ +{ + "event_type": "survey_response.updated", + "event_id": "b8d057c59327541d7ec2104c0a9a255ad1997fb00831b9c6bbf09561e6d5cbd0", + "event_data": { + "id": "5435", + "person": { + "id": "5975", + "email": "paul_gravis@example.com", + "name": "Paul Gravis", + "created_at": 1482589349 + }, + "score": 5, + "comment": "Your service is slow, but nearly flawless! Keep up the good work!", + "permalink": "https://delighted.com/r/5pFDpmlyC8GUc5oxU6USto5VonSKAqOa", + "created_at": 1482589409, + "updated_at": 1482590009, + "person_properties": null, + "notes": [], + "tags": [] + } +} diff --git a/zerver/fixtures/delighted/delighted_survey_response_updated_promoter.json b/zerver/fixtures/delighted/delighted_survey_response_updated_promoter.json new file mode 100644 index 0000000000..dccbb9ba8b --- /dev/null +++ b/zerver/fixtures/delighted/delighted_survey_response_updated_promoter.json @@ -0,0 +1,21 @@ +{ + "event_type": "survey_response.updated", + "event_id": "b8d057c59327541d7ec2104c0a9a255ad1997fb00831b9c6bbf09561e6d5cbd0", + "event_data": { + "id": "5435", + "person": { + "id": "5975", + "email": "charlie_gravis@example.com", + "name": "Charlie Gravis", + "created_at": 1482589349 + }, + "score": 9, + "comment": "Your service is fast and flawless!", + "permalink": "https://delighted.com/r/5pFDpmlyC8GUc5oxU6USto5VonSKAqOa", + "created_at": 1482589409, + "updated_at": 1482590009, + "person_properties": null, + "notes": [], + "tags": [] + } +} diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py index 001ac8f604..5c5ef26596 100644 --- a/zerver/lib/integrations.py +++ b/zerver/lib/integrations.py @@ -109,6 +109,7 @@ WEBHOOK_INTEGRATIONS = [ WebhookIntegration('circleci', display_name='CircleCI'), WebhookIntegration('codeship'), WebhookIntegration('crashlytics'), + WebhookIntegration('delighted', display_name='Delighted'), WebhookIntegration('deskdotcom', logo='static/images/integrations/logos/deskcom.png', display_name='Desk.com'), WebhookIntegration('freshdesk'), GithubIntegration( diff --git a/zerver/tests/webhooks/test_delighted.py b/zerver/tests/webhooks/test_delighted.py new file mode 100644 index 0000000000..dce3242874 --- /dev/null +++ b/zerver/tests/webhooks/test_delighted.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +from typing import Text +from zerver.lib.test_classes import WebhookTestCase + +class DelightedHookTests(WebhookTestCase): + STREAM_NAME = 'delighted' + URL_TEMPLATE = "/api/v1/external/delighted?stream={stream}&api_key={api_key}" + FIXTURE_DIR_NAME = 'delighted' + + def test_feedback_message_promoter(self): + # type: () -> None + expected_subject = "Survey Response" + expected_message = ("Kudos! You have a new promoter.\n" + ">Score of 9/10 from charlie_gravis@example.com" + "\n>Your service is fast and flawless!") + + self.send_and_test_stream_message('survey_response_updated_promoter', + expected_subject, + expected_message, + content_type="application/x-www-form-urlencoded") + + def test_feedback_message_non_promoter(self): + # type: () -> None + expected_subject = "Survey Response" + expected_message = ("Great! You have new feedback.\n" + ">Score of 5/10 from paul_gravis@example.com" + "\n>Your service is slow, but nearly flawless! " + "Keep up the good work!") + + self.send_and_test_stream_message('survey_response_updated_non_promoter', + expected_subject, + expected_message, + content_type="application/x-www-form-urlencoded") + + def get_body(self, fixture_name): + # type: (Text) -> Text + return self.fixture_data("delighted", fixture_name, file_type="json") diff --git a/zerver/views/webhooks/delighted.py b/zerver/views/webhooks/delighted.py new file mode 100644 index 0000000000..167bfd4a25 --- /dev/null +++ b/zerver/views/webhooks/delighted.py @@ -0,0 +1,40 @@ +from __future__ import absolute_import +from django.utils.translation import ugettext as _ +from zerver.lib.actions import check_send_message +from zerver.lib.response import json_success, json_error +from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view + +from zerver.models import Client, UserProfile + +from django.http import HttpRequest, HttpResponse +from six import text_type +from typing import Dict, Any, Optional + +def body_template(score): + # type: (int) -> str + if score >= 7: + return 'Kudos! You have a new promoter.\n>Score of {score}/10 from {email}\n>{comment}' + else: + return 'Great! You have new feedback.\n>Score of {score}/10 from {email}\n>{comment}' + +@api_key_only_webhook_view("Delighted") +@has_request_variables +def api_delighted_webhook(request, user_profile, client, + payload=REQ(argument_type='body'), + stream=REQ(default='delighted'), + topic=REQ(default='Survey Response')): + # type: (HttpRequest, UserProfile, Client, Dict[str, Dict[str, Any]], text_type, text_type) -> HttpResponse + try: + person = payload['event_data']['person'] + selected_payload = {'email': person['email']} + selected_payload['score'] = payload['event_data']['score'] + selected_payload['comment'] = payload['event_data']['comment'] + except KeyError as e: + return json_error(_("Missing key {} in JSON").format(str(e))) + + BODY_TEMPLATE = body_template(selected_payload['score']) + body = BODY_TEMPLATE.format(**selected_payload) + + check_send_message(user_profile, client, 'stream', [stream], + topic, body) + return json_success()