mirror of
https://github.com/zulip/zulip.git
synced 2025-11-14 19:06:09 +00:00
integrations: Add webhook code, API endpoint, and tests for Delighted.
This commit is contained in:
BIN
static/images/integrations/logos/delighted.png
Normal file
BIN
static/images/integrations/logos/delighted.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
@@ -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": []
|
||||
}
|
||||
}
|
||||
@@ -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": []
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
37
zerver/tests/webhooks/test_delighted.py
Normal file
37
zerver/tests/webhooks/test_delighted.py
Normal file
@@ -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")
|
||||
40
zerver/views/webhooks/delighted.py
Normal file
40
zerver/views/webhooks/delighted.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user