2018-01-12 13:49:11 +01:00
|
|
|
# Webhooks for external integrations.
|
2018-05-10 19:34:01 +02:00
|
|
|
from typing import Any, Dict
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-01-12 13:49:11 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-01-12 13:49:11 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.actions import check_send_private_message
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.models import UserProfile, get_user_profile_by_email
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-01-12 13:49:11 +01:00
|
|
|
@api_key_only_webhook_view("dialogflow")
|
|
|
|
@has_request_variables
|
|
|
|
def api_dialogflow_webhook(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body'),
|
|
|
|
email: str=REQ(default='foo')) -> HttpResponse:
|
|
|
|
status = payload["status"]["code"]
|
|
|
|
|
|
|
|
if status == 200:
|
|
|
|
result = payload["result"]["fulfillment"]["speech"]
|
|
|
|
if not result:
|
|
|
|
alternate_result = payload["alternateResult"]["fulfillment"]["speech"]
|
|
|
|
if not alternate_result:
|
2020-04-09 21:51:58 +02:00
|
|
|
body = "DialogFlow couldn't process your query."
|
2018-01-12 13:49:11 +01:00
|
|
|
else:
|
|
|
|
body = alternate_result
|
|
|
|
else:
|
|
|
|
body = result
|
|
|
|
else:
|
|
|
|
error_status = payload["status"]["errorDetails"]
|
2020-04-09 21:51:58 +02:00
|
|
|
body = "{} - {}".format(status, error_status)
|
2018-01-12 13:49:11 +01:00
|
|
|
|
|
|
|
profile = get_user_profile_by_email(email)
|
|
|
|
check_send_private_message(user_profile, request.client, profile, body)
|
|
|
|
return json_success()
|