Skip to content

Commit

Permalink
Merge branch 'main' into ATO-1301
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchariGr committed Aug 4, 2023
2 parents bb89906 + b2d3d9c commit f9685ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog/12704.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Passed request headers from REST channel.
38 changes: 17 additions & 21 deletions rasa/core/channels/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
class UserMessage:
"""Represents an incoming message.
Includes the channel the responses should be sent to."""
Includes the channel the responses should be sent to.
"""

def __init__(
self,
Expand All @@ -43,6 +44,7 @@ def __init__(
input_channel: Optional[Text] = None,
message_id: Optional[Text] = None,
metadata: Optional[Dict] = None,
**kwargs: Any,
) -> None:
"""Creates a ``UserMessage`` object.
Expand Down Expand Up @@ -78,6 +80,7 @@ def __init__(

self.parse_data = parse_data
self.metadata = metadata
self.headers = kwargs.get("headers", None)


def register(
Expand Down Expand Up @@ -119,7 +122,8 @@ def blueprint(
"""Defines a Sanic blueprint.
The blueprint will be attached to a running sanic server and handle
incoming routes it registered for."""
incoming routes it registered for.
"""
raise NotImplementedError("Component listener needs to provide blueprint.")

@classmethod
Expand Down Expand Up @@ -217,7 +221,6 @@ def name(cls) -> Text:

async def send_response(self, recipient_id: Text, message: Dict[Text, Any]) -> None:
"""Send a message to the client."""

if message.get("quick_replies"):
await self.send_quick_replies(
recipient_id,
Expand Down Expand Up @@ -251,7 +254,6 @@ async def send_text_message(
self, recipient_id: Text, text: Text, **kwargs: Any
) -> None:
"""Send a message through this channel."""

raise NotImplementedError(
"Output channel needs to implement a send message for simple texts."
)
Expand All @@ -260,14 +262,12 @@ async def send_image_url(
self, recipient_id: Text, image: Text, **kwargs: Any
) -> None:
"""Sends an image. Default will just post the url as a string."""

await self.send_text_message(recipient_id, f"Image: {image}")

async def send_attachment(
self, recipient_id: Text, attachment: Text, **kwargs: Any
) -> None:
"""Sends an attachment. Default will just post as a string."""

await self.send_text_message(recipient_id, f"Attachment: {attachment}")

async def send_text_with_buttons(
Expand All @@ -279,8 +279,8 @@ async def send_text_with_buttons(
) -> None:
"""Sends buttons to the output.
Default implementation will just post the buttons as a string."""

Default implementation will just post the buttons as a string.
"""
await self.send_text_message(recipient_id, text)
for idx, button in enumerate(buttons):
button_msg = cli_utils.button_to_string(button, idx)
Expand All @@ -295,17 +295,16 @@ async def send_quick_replies(
) -> None:
"""Sends quick replies to the output.
Default implementation will just send as buttons."""

Default implementation will just send as buttons.
"""
await self.send_text_with_buttons(recipient_id, text, quick_replies)

async def send_elements(
self, recipient_id: Text, elements: Iterable[Dict[Text, Any]], **kwargs: Any
) -> None:
"""Sends elements to the output.
Default implementation will just post the elements as a string."""

Default implementation will just post the elements as a string.
"""
for element in elements:
element_msg = "{title} : {subtitle}".format(
title=element.get("title", ""), subtitle=element.get("subtitle", "")
Expand All @@ -318,16 +317,16 @@ async def send_custom_json(
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any
) -> None:
"""Sends json dict to the output channel.
Default implementation will just post the json contents as a string."""

Default implementation will just post the json contents as a string.
"""
await self.send_text_message(recipient_id, json.dumps(json_message))


class CollectingOutputChannel(OutputChannel):
"""Output channel that collects send messages in a list
"""Output channel that collects send messages in a list.
(doesn't send them anywhere, just collects them)."""
(doesn't send them anywhere, just collects them).
"""

def __init__(self) -> None:
"""Initialise list to collect messages."""
Expand All @@ -348,7 +347,6 @@ def _message(
custom: Dict[Text, Any] = None,
) -> Dict:
"""Create a message object that will be stored."""

obj = {
"recipient_id": recipient_id,
"text": text,
Expand Down Expand Up @@ -380,14 +378,12 @@ async def send_image_url(
self, recipient_id: Text, image: Text, **kwargs: Any
) -> None:
"""Sends an image. Default will just post the url as a string."""

await self._persist_message(self._message(recipient_id, image=image))

async def send_attachment(
self, recipient_id: Text, attachment: Text, **kwargs: Any
) -> None:
"""Sends an attachment. Default will just post as a string."""

await self._persist_message(self._message(recipient_id, attachment=attachment))

async def send_text_with_buttons(
Expand Down
1 change: 1 addition & 0 deletions rasa/core/channels/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ async def receive(request: Request) -> Union[ResponseStream, HTTPResponse]:
sender_id,
input_channel=input_channel,
metadata=metadata,
headers=request.headers,
)
)
except CancelledError:
Expand Down

0 comments on commit f9685ee

Please sign in to comment.