mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
Using get_user_profile_by_email is invalid, as it omits the realm, and also fetches via .delivery_email - our convention is that .email is supposed to be used for user-facing purposes like this.
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
# Webhooks for external integrations.
|
|
from typing import Any, Dict
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from zerver.decorator import 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
|
|
|
|
|
|
@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(),
|
|
) -> 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:
|
|
body = "Dialogflow couldn't process your query."
|
|
else:
|
|
body = alternate_result
|
|
else:
|
|
body = result
|
|
else:
|
|
error_status = payload["status"]["errorDetails"]
|
|
body = f"{status} - {error_status}"
|
|
|
|
receiving_user = get_user(email, user_profile.realm)
|
|
check_send_private_message(user_profile, request.client, receiving_user, body)
|
|
return json_success()
|