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

Implemented get_output_channel() fn for SocketIO Channel #5578

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions rasa/core/channels/socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ async def send_text_message(
"""Send a message through this channel."""

for message_part in text.strip().split("\n\n"):
await self._send_message(self.sid, {"text": message_part})
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved
await self._send_message(recipient_id, {"text": message_part})

async def send_image_url(
self, recipient_id: Text, image: Text, **kwargs: Any
) -> None:
"""Sends an image to the output"""

message = {"attachment": {"type": "image", "payload": {"src": image}}}
await self._send_message(self.sid, message)
await self._send_message(recipient_id, message)

async def send_text_with_buttons(
self,
Expand Down Expand Up @@ -80,7 +80,7 @@ async def send_text_with_buttons(
)

for message in messages:
await self._send_message(self.sid, message)
await self._send_message(recipient_id, message)

async def send_elements(
self, recipient_id: Text, elements: Iterable[Dict[Text, Any]], **kwargs: Any
Expand All @@ -95,27 +95,29 @@ async def send_elements(
}
}

await self._send_message(self.sid, message)
await self._send_message(recipient_id, message)

async def send_custom_json(
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any
) -> None:
"""Sends custom json to the output"""

json_message.setdefault("room", self.sid)
json_message.setdefault("room", recipient_id)

await self.sio.emit(self.bot_message_evt, **json_message)

async def send_attachment(
self, recipient_id: Text, attachment: Dict[Text, Any], **kwargs: Any
) -> None:
"""Sends an attachment to the user."""
await self._send_message(self.sid, {"attachment": attachment})
await self._send_message(recipient_id, {"attachment": attachment})
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved


class SocketIOInput(InputChannel):
"""A socket.io input channel."""

sio = None
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def name(cls) -> Text:
return "socketio"
Expand Down Expand Up @@ -145,6 +147,9 @@ def __init__(
self.namespace = namespace
self.socketio_path = socketio_path

def get_output_channel(self) -> Optional["OutputChannel"]:
return SocketIOOutput(SocketIOInput.sio , None, self.bot_message_evt)
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved

def blueprint(
self, on_new_message: Callable[[UserMessage], Awaitable[Any]]
) -> Blueprint:
Expand All @@ -155,6 +160,9 @@ def blueprint(
sio, self.socketio_path, "socketio_webhook", __name__
)

# make sio object static to use in get_output_channel
SocketIOInput.sio = sio
alfredfrancis marked this conversation as resolved.
Show resolved Hide resolved

@socketio_webhook.route("/", methods=["GET"])
async def health(_: Request) -> HTTPResponse:
return response.json({"status": "ok"})
Expand Down