Skip to content

Commit

Permalink
Merge pull request #912 from RasaHQ/eng-123-action-compression
Browse files Browse the repository at this point in the history
handling deflate encoded data in web hook
  • Loading branch information
radovanZRasa authored Dec 22, 2022
2 parents 9660044 + 35e6e86 commit a05f152
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog/912.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added the ability to decompress deflate encoded
json payloads for the webhook endpoint of the
action server.
10 changes: 9 additions & 1 deletion rasa_sdk/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
import os
import types
import zlib
import json
from typing import List, Text, Union, Optional, Any
from ssl import SSLContext

Expand Down Expand Up @@ -90,7 +92,13 @@ async def health(_) -> HTTPResponse:
@app.post("/webhook")
async def webhook(request: Request) -> HTTPResponse:
"""Webhook to retrieve action calls."""
action_call = request.json
if request.headers.get("Content-Encoding") == "deflate":
# Decompress the request data using zlib
decompressed_data = zlib.decompress(request.body)
# Load the JSON data from the decompressed request data
action_call = json.loads(decompressed_data)
else:
action_call = request.json
if action_call is None:
body = {"error": "Invalid body request"}
return response.json(body, status=400)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import json
import zlib

import rasa_sdk.endpoint as ep
from rasa_sdk.events import SlotSet
Expand Down Expand Up @@ -90,3 +91,21 @@ def test_arg_parser_actions_params_module_style():
args = ["--actions", "actions.act"]
cmdline_args = parser.parse_args(args)
assert cmdline_args.actions == "actions.act"


def test_server_webhook_custom_action_encoded_data_returns_200():
data = {
"next_action": "custom_action",
"tracker": {"sender_id": "1", "conversation_id": "default"},
"domain": {"intents": ["greet", "goodbye"]},
}

request, response = app.test_client.post(
"/webhook",
data=zlib.compress(json.dumps(data).encode()),
headers={"Content-encoding": "deflate"},
)
events = response.json.get("events")

assert events == [SlotSet("test", "bar")]
assert response.status == 200

0 comments on commit a05f152

Please sign in to comment.