mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 01:16:19 +00:00
This converts most webhook integration views to use @typed_endpoint instead of @has_request_variables, rewriting REQ parameters. For these webhooks, it simply requires switching the decorator, rewriting the type annotation of payload/message to WebhookPayload[WildValue], and removing the REQ default that defines the to_wild_value converter.
33 lines
985 B
Python
33 lines
985 B
Python
from django.http import HttpRequest, HttpResponse
|
|
|
|
from zerver.decorator import webhook_view
|
|
from zerver.lib.response import json_success
|
|
from zerver.lib.typed_endpoint import WebhookPayload, typed_endpoint
|
|
from zerver.lib.validator import WildValue, check_string
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
@webhook_view("HomeAssistant")
|
|
@typed_endpoint
|
|
def api_homeassistant_webhook(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
*,
|
|
payload: WebhookPayload[WildValue],
|
|
) -> HttpResponse:
|
|
# construct the body of the message
|
|
body = payload["message"].tame(check_string)
|
|
|
|
# set the topic to the topic parameter, if given
|
|
if "topic" in payload:
|
|
topic = payload["topic"].tame(check_string)
|
|
else:
|
|
topic = "homeassistant"
|
|
|
|
# send the message
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
|
|
|
# return json result
|
|
return json_success(request)
|