How to redirect conversations to human - rasa (socket, channel))

I’m using rasa. I have the following scenario:

-user open webpage with chat(bot),
-user writes with both:
example:
u: hello,
b: hello, how can I help you?
u. I need something....
b. ...
u. I want talk to human
b-> redirect to human assistant (slot) 

I read about helpdesk assistant, financial assistant GitHub - RasaHQ/helpdesk-assistant I created story. I created websocket using Java + Spring boot ws://localhost:5006/socket. Still not receiving messages on 5006/socket.

import pathlib
from typing import Dict, Text, Any, List

import ruamel.yaml
from rasa_sdk import Tracker, Action
from rasa_sdk.events import EventType
from rasa_sdk.executor import CollectingDispatcher

here = pathlib.Path(__file__).parent.absolute()
handoff_config = (
    ruamel.yaml.safe_load(open(f"{here}/handoff_config.yml", "r")) or {}
).get("handoff_hosts", {})


class ActionHandoffOptions(Action):
    def name(self) -> Text:
        return "action_handoff_options"

    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[EventType]:

        if not any(
            [config.get("url") for bot, config in handoff_config.items()]
        ):
            dispatcher.utter_message(template="utter_no_handoff")
        else:
            buttons = [
                {
                    "title": config.get("title"),
                    "payload": f'/trigger_handoff{{"handoff_to":"{bot}"}}',
                }
                for bot, config in handoff_config.items()
            ]
            dispatcher.utter_message(
                text="I can't transfer you to a human, \
                     but I can transfer you to one of these bots",
                buttons=buttons,
            )
        return []


class ActionHandoff(Action):
    def name(self) -> Text:
        return "action_handoff"

    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[EventType]:

        dispatcher.utter_message(template="utter_handoff")
        handoff_to = tracker.get_slot("handoff_to")

        handoff_bot = handoff_config.get(handoff_to, {})
        url = handoff_bot.get("url")

        if url:
            if tracker.get_latest_input_channel() == "rest":
                dispatcher.utter_message(
                    json_message={
                        "handoff_host": url,
                        "title": handoff_bot.get("title"),
                    }
                )
            else:
                dispatcher.utter_message(
                    template="utter_wouldve_handed_off", handoffhost=url
                )
        else:
            dispatcher.utter_message(template="utter_no_handoff")

        return []
handoff_hosts:
    financial_demo:
      title: "Financial Assistant"
      url: "ws://localhost:5006/socket"
    ## you can add more handoff hosts to this list e.g.
    # moodbot:
    #   title: "MoodBot"
    #   url: "http://localhost:5007"

@mmuserdev are you able to manually send a message to "ws://localhost:5006/socket" and get the behavior you want? If so is it possible that the message being forwarded from rasa is not of the correct format?

@erohmensing

import pathlib
from typing import Dict, Text, Any, List

import ruamel.yaml
from rasa_sdk import Tracker, Action
from rasa_sdk.events import EventType
from rasa_sdk.executor import CollectingDispatcher

here = pathlib.Path(__file__).parent.absolute()
handoff_config = (
    ruamel.yaml.safe_load(open(f"{here}/handoff_config.yml", "r")) or {}
).get("handoff_hosts", {})


class ActionHandoffOptions(Action):
    def name(self) -> Text:
        return "action_handoff_options"

    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[EventType]:

        if not any(
            [config.get("url") for bot, config in handoff_config.items()]
        ):
            dispatcher.utter_message(template="utter_no_handoff")
        else:
            buttons = [
                {
                    "title": config.get("title"),
                    "payload": f'/trigger_handoff{{"handoff_to":"{bot}"}}',
                }
                for bot, config in handoff_config.items()
            ]
            dispatcher.utter_message(
                text="I can't transfer you to a human, \
                     but I can transfer you to one of these bots",
                buttons=buttons,
            )
        return []


class ActionHandoff(Action):
    def name(self) -> Text:
        return "action_handoff"

    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[EventType]:

        dispatcher.utter_message(template="utter_handoff")
        handoff_to = tracker.get_slot("handoff_to")

        handoff_bot = handoff_config.get(handoff_to, {})
        url = handoff_bot.get("url")

        if url:
            if tracker.get_latest_input_channel() == "rest":
                dispatcher.utter_message(
                    json_message={
                        "handoff_host": url,
                        "title": handoff_bot.get("title"),
                    }
                )
            else:
                dispatcher.utter_message(
                    template="utter_wouldve_handed_off", handoffhost=url
                )
        else:
            dispatcher.utter_message(template="utter_no_handoff")

        return []
handoff_hosts:
    financial_demo:
      title: "Financial Assistant"
      url: "ws://localhost:8080/socket"
      #url: "http://localhost:8001/api"
    ## you can add more handoff hosts to this list e.g.
    # moodbot:
    #   title: "MoodBot"
    #   url: "http://localhost:5007"

What is wrong?

Ah, I see. That’s because you can’t implement handoff directly in Rasa X – handoff has to happen on the front end and Rasa X is not configured for that. You’ll notice the message “If you were connected to me on chatroom I would have handed you off…”

You’ll need to configure the chatroom frontend as described in the repo and talk to it there to implement the handoff.

1 Like