Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get metadata from REST channel #11311

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions changelog/11311.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-instates the REST channel metadata feature. Metadata can be provided on the `metadata` key.
5 changes: 4 additions & 1 deletion docs/docs/connectors/your-own-website.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ After making the `callback` input available, you can `POST` messages to
```json
{
"sender": "test_user", // sender ID of the user sending the message
"message": "Hi there!"
"message": "Hi there!",
"metadata": { // optional metadata
"key1": "value1"
}
}
```

Expand Down
30 changes: 30 additions & 0 deletions rasa/core/channels/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ def _extract_message(self, req: Request) -> Optional[Text]:
def _extract_input_channel(self, req: Request) -> Text:
return req.json.get("input_channel") or self.name()

def get_metadata(self, request: Request) -> Optional[Dict[Text, Any]]:
"""Extracts additional information from the incoming request.

Implementing this function is not required. However, it can be used to extract
metadata from the request. The return value is passed on to the
``UserMessage`` object and stored in the conversation tracker.

Args:
request: incoming request with the message of the user

Returns:
Metadata which was extracted from the request.
"""
return request.json.get("metadata", None)

def stream_response(
rgstephens marked this conversation as resolved.
Show resolved Hide resolved
self,
on_new_message: Callable[[UserMessage], Awaitable[None]],
Expand All @@ -66,6 +81,21 @@ def stream_response(
input_channel: Text,
metadata: Optional[Dict[Text, Any]],
) -> Callable[[Any], Awaitable[None]]:
"""Streams response to the client

If the stream option is enabled, this method will be called to
stream the response to the client

Args:
on_new_message: sanic event
text: message text
sender_id: message sender_id
input_channel: input channel name
metadata: optional metadata sent with the message

Returns:
Sanic stream
"""
async def stream(resp: Any) -> None:
q: Queue = Queue()
task = asyncio.ensure_future(
Expand Down