Integrate Rasa with Live Chat

Hi,

I’ve developed a chatbot with rasa stack and Iwould like to integrate the it with Live Chat. I want to accomplish two major features.

  1. Add a button named “Chat with a Live Agent” - when the user clicks on this button he/she should be redirected to a live agent from Live Chat.
  2. Redirect to a live agent from live chat when the bot fails to answer.

I am unable to find documentations or examples pertaining to this. It will be a lot helpful for me to move my chatbot to live environment if you could provide me with an idea on how to approach this.

Use TwoStageFallbackPolicy Fallback Actions and try to overwrite action_default_fallback for those scenarios. For buttons : Domains . For more detailed solution refer : Open source Two Stage Fallback Policy with Rasa NLU and Rasa Core

Hi @himanshus97,

Thank you for the reply. To be more clear on my question, I would like to know about hand-off to a human agent by integrating Rasa chatbot with Live Chat. Can I switch from bot to human agent using rasa in terms of Live Chat api. If it is possible can you let me know how it could be done?

Immediate response is highly appreciated.

Hey i"m also looking for that only.Please let me know if you get any clue

Hi @SudheshSolomon,

There are multiple ways to do so:

  • your frontend can route messages to a different backend when the bot sends some specific message
  • you can use custom actions to forward messages to a human

This second way is what @himanshus97 already suggested. You would want to implement a custom action that hands over to a human agent and use that as fallback action. If you add a button “Chat with a Live Agent”, have this button trigger the same custom action.

Hello @chkoss,

I’m able to post messages to Live Chat System (FreshChat) using the second method. Now the live agent system needs to push responses back to rasa instance (and forward it to user). The live agent system supports webhooks and it can push messages (including sender_id) to rasa it.

I added a webhook HTTP API to the rasa server (rasa/server.py). I am able to receive messages from the live agent system. Also, I’m able to reproduce the tracker obejct and Input_channel object.

image

I managed to make this work for the Input Channels who are providing get_output_channel() fn. But issue is, for the Socketio Input channel, get_output_channel() only returns CollectingDisptacher object. So, I’m not able to push messages to the socket channel.

How can i approch this case ? I’m stuck here since past 2 days. Any help is appreciated :innocent:

PS: If this is something in your roadmap, once the issues are sorted out, I’d love to create a pull request to support live chat handover :sunglasses:

2 Likes

@amn41 @tmbo Anybody? :innocent:

@alfredfrancis bro help me with this when u r done :joy:. Can you share the file or the screen shots

Hi @alfredfrancis,

Cool that you solved it for some channels already! To make it work for the SocketIO channel, too, maybe you could implement a get_output_channel() function for the class SocketIOInput in rasa/core/channels/socketio. If you do this, feel free to create a pull request, then we can include it in a release so everyone can benefit from it.

About your PS: Thanks for the offer! I will bring this up in our next planning meeting.

1 Like

Hello @chkoss
Thank you for your response

I’ve implemented get_output_channel() for SocketIO as follows

def get_output_channel(self) -> Optional["OutputChannel"]:
    return SocketIOOutput(SocketIOInput.sio , None, self.bot_message_evt)

I had to make sio object static in order to access it. Also i had to set sid to None. Please take a look at follwing code

def blueprint(
    self, on_new_message: Callable[[UserMessage], Awaitable[Any]]
) -> Blueprint:
    # Workaround so that socketio works with requests from other origins.
    # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
    sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])

    socketio_webhook = SocketBlueprint(
        sio, self.socketio_path, "socketio_webhook", __name__
    )

    SocketIOInput.sio = sio

Also, I had to change the output channel message handlers to following

async def send_text_message(
    self, recipient_id: Text, text: Text, **kwargs: Any
) -> None:
    """Send a message through this channel."""
    # await self._send_message(self.sid, {"text": text})     <-------- old way
    await self._send_message(recipient_id, {"text": text})

Even though everything is working fine, I’m not sure whether the above changes are the right approch or not. Please share your suggestions.

Thanks @alfredfrancis! I don’t know this part of the codebase well enough to judge your changes, but you will get feedback about it in the PR.

1 Like

Ok.

Check my pull request

@chkoss. We are looking at same. Could you elaborate a bit more on how we put on the bot ‘on rails’ to always send messages to this custom action when in ‘talking with agent’ mode.
@himanshus97 I didn’t follow the fallback policy as the bot may still classify the intent, we just it to send all inputs to the action when the switch/slot is on. Similiar to how the FormPolicy takes over the normal predictive next action until. Also which Rasa version has this socket.io fix has been implemented in? https://github.com/RasaHQ/rasa/pull/5578 Status says merged, but not closed. @alfredfrancis Did you manage to finish this end to end? Any suggestions on the above? thanks

Hi, the custom action shouldn’t handle all the messages in talking with agent mode. Rather, you should have a client UI which can switch to a different backend, and the job of the custom action would be to prompt this switch and pause the conversation in Rasa. See this reply for further explanation.

To see an example of how such a handoff can be done, check out our recent addition to the chatroom Rasa widget. There is also an example bot that uses this feature.

edit: Also, the SocketIO addition is included in Rasa Open Source since version 1.10.0, which was released on 28th April.