Hi @ricwo, As per your info, I have changed my Input Channel as follows.
class DesktopInput(InputChannel):
@classmethod
def name(cls):
# print('Inside the desktop Channel')
return "desktop"
@staticmethod
async def on_message_wrapper(
on_new_message: Callable[[UserMessage], Awaitable[None]],
text: Text,
queue: Queue,
sender_id: Text,
input_channel: Text,
metadata: Dict,
) -> None:
collector = QueueOutputChannel(queue)
output_channel = DesktopOutputChannel()
# print('Input meta data is: ', metadata)
message = UserMessage(text, sender_id = sender_id, input_channel=input_channel, output_channel=collector, metadata=metadata,)
await on_new_message(message)
await queue.put("DONE") # pytype: disable=bad-return-type
async def _extract_sender(self, req: Request) -> Optional[Text]:
return req.json.get("sender", None)
# noinspection PyMethodMayBeStatic
def _extract_message(self, req: Request) -> Optional[Text]:
return req.json.get("message", None)
def _extract_input_channel(self, req: Request) -> Text:
return req.json.get("input_channel") or self.name()
def _extract_metadata(self, req: Request) -> Text:
return req.json.get("metadata",None)
def stream_response(
self,
on_new_message: Callable[[UserMessage], Awaitable[None]],
text: Text,
sender_id: Text,
input_channel: Text,
metadata: Dict,
) -> Callable[[Any], Awaitable[None]]:
async def stream(resp: Any) -> None:
q = Queue()
output = DesktopOutputChannel()
task = asyncio.ensure_future(
self.on_message_wrapper(
on_new_message, text, q, sender_id, input_channel, metadata
)
)
result = None # declare variable up front to avoid pytype error
while True:
result = await q.get()
if result == "DONE":
break
else:
await resp.write(json.dumps(result) + "\n")
await task
return stream # pytype: disable=bad-return-type
def blueprint(self, on_new_message: Callable[[UserMessage], Awaitable[None]]):
custom_webhook = Blueprint(
"custom_webhook_{}".format(type(self).__name__),
inspect.getmodule(self).__name__,
)
# noinspection PyUnusedLocal
@custom_webhook.route("/", methods=["GET"])
async def health(request: Request):
return response.json({"status": "ok"})
@custom_webhook.route("/webhook", methods=["POST"])
async def receive(request: Request):
sender_id = await self._extract_sender(request)
text = self._extract_message(request)
metadata = self._extract_metadata(request)
should_use_stream = rasa.utils.endpoints.bool_arg(
request, "stream", default=False
)
input_channel = self._extract_input_channel(request)
if should_use_stream:
# print("Inside the should stream of Input Channel")
return response.stream(
self.stream_response(
on_new_message, text, sender_id, input_channel, metadata
),
content_type="text/event-stream",
)
else:
# print("Inside the else of stream of Input Channel")
collector = DesktopOutputChannel()
# noinspection PyBroadException
try:
await on_new_message(
UserMessage(
text, sender_id=sender_id, input_channel=input_channel, output_channel=collector,metadata=metadata
)
)
except CancelledError:
logger.error(
"Message handling timed out for "
"user message '{}'.".format(text)
)
except Exception:
logger.exception(
"An exception occured while handling "
"user message '{}'.".format(text)
)
# print('Final Receive Endpoint result is: ', collector.messages)
return response.json(collector.messages)
return custom_webhook
def get_output_channel(self) -> OutputChannel:
# print('Getting the Output channel ')
return DesktopOutputChannel()
In the above code , under on_message_wrapper function, should I send the collector or output_channel to UserMessage output_channel attribuite? …I couldnt understand the same with stream_response where , I am sending q instead of output variable.
My desktop output Channel is:
class DesktopOutputChannel(CollectingOutputChannel):
"""Output channel base class.
Provides sane implementation of the send methods
for text only output channels."""
@classmethod
def name(cls):
"""Every output channel needs a name to identify it."""
# print('Inside the desktop Output Channel')
return "desktop"
After specifying the above changes, when i specify channel as desktop in domain, I am able to get the specific desktop response.
utter_greet:
- text: 'Hey there, welcome
- text: 'Hey there desktop user, welcome
channel: "desktop"
Am I making any mistake in the above channel creation. Just want to check the efficiency of the code.