# Integrate Rasa with Live Chat

**URL:** <https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314>\
**Category:** Rasa Open Source\
**Created:** [December 11, 2019, 3:36am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314 "2019-12-11T03:36:09Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SudheshSolomon](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@SudheshSolomon](https://forum.rasa.com/u/SudheshSolomon)\
**Post date:** [December 11, 2019, 3:36am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/1 "2019-12-11T03:36:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![himanshus97](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/himanshus97/32/5425_2.png) [@himanshus97](https://forum.rasa.com/u/himanshus97)\
**Post date:** [December 11, 2019, 6:27am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/2 "2019-12-11T06:27:33Z")

</div>

Use TwoStageFallbackPolicy [Fallback Actions](https://rasa.com/docs/rasa/core/fallback-actions/#two-stage-fallback-policy) and try to overwrite action\_default\_fallback for those scenarios. For buttons : [Domains](https://rasa.com/docs/rasa/core/domains/#images-and-buttons) . For more detailed solution refer : [Open source Two Stage Fallback Policy with Rasa NLU and Rasa Core](https://blog.rasa.com/failing-gracefully-with-rasa-core/)

---

<div class="post-metadata">

**Author:** ![SudheshSolomon](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@SudheshSolomon](https://forum.rasa.com/u/SudheshSolomon)\
**Post date:** [March 2, 2020, 8:04am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/3 "2020-03-02T08:04:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nitz](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nitz](https://forum.rasa.com/u/nitz)\
**Post date:** [March 6, 2020, 11:27am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/4 "2020-03-06T11:27:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chkoss](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chkoss/32/7186_2.png) [@chkoss](https://forum.rasa.com/u/chkoss)\
**Post date:** [March 9, 2020, 12:52pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/5 "2020-03-09T12:52:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alfredfrancis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alfredfrancis/32/7973_2.png) [@alfredfrancis](https://forum.rasa.com/u/alfredfrancis)\
**Post date:** [March 28, 2020, 8:39pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/6 "2020-03-28T20:39:12Z")

</div>

Hello @chkoss,

I’m able to post messages to Live Chat System ([FreshChat](http://freshchat.com)) 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](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/7/7a006a953c84ab63f05bcb3fb98dd3d122a06f79.png)

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 😇

_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_ 🕶

---

<div class="post-metadata">

**Author:** ![alfredfrancis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alfredfrancis/32/7973_2.png) [@alfredfrancis](https://forum.rasa.com/u/alfredfrancis)\
**Post date:** [March 29, 2020, 12:49pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/7 "2020-03-29T12:49:42Z")

</div>

@amn41 @tmbo Anybody? 😇

---

<div class="post-metadata">

**Author:** ![nitz](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nitz](https://forum.rasa.com/u/nitz)\
**Post date:** [March 30, 2020, 7:56am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/8 "2020-03-30T07:56:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chkoss](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chkoss/32/7186_2.png) [@chkoss](https://forum.rasa.com/u/chkoss)\
**Post date:** [March 30, 2020, 10:56am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/9 "2020-03-30T10:56:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alfredfrancis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alfredfrancis/32/7973_2.png) [@alfredfrancis](https://forum.rasa.com/u/alfredfrancis)\
**Post date:** [April 4, 2020, 2:04pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/10 "2020-04-04T14:04:01Z")

</div>

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.

> <https://github.com/RasaHQ/rasa/pull/5578>
>
> \*\*Proposed changes\*\*:
> \- Implemented get\_output\_channel() fn for SocketIO Channe…l
> \- Changed Socketio Output message handlers to use recepient\_id instead of self.sid 
> 
> \*\*Status (please check what you already did)\*\*:
> \- \[\] added some tests for the functionality
> \- \[\] updated the documentation
> \- \[\] updated the changelog (please check \[changelog\](https://github.com/RasaHQ/rasa/tree/master/changelog) for instructions)
> \- \[\] reformat files using \`black\` (please check \[Readme\](https://github.com/RasaHQ/rasa#code-style) for instructions)

---

<div class="post-metadata">

**Author:** ![chkoss](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chkoss/32/7186_2.png) [@chkoss](https://forum.rasa.com/u/chkoss)\
**Post date:** [April 8, 2020, 10:18am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/11 "2020-04-08T10:18:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alfredfrancis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alfredfrancis/32/7973_2.png) [@alfredfrancis](https://forum.rasa.com/u/alfredfrancis)\
**Post date:** [April 8, 2020, 1:56pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/12 "2020-04-08T13:56:09Z")

</div>

Ok.

---

<div class="post-metadata">

**Author:** ![alfredfrancis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alfredfrancis/32/7973_2.png) [@alfredfrancis](https://forum.rasa.com/u/alfredfrancis)\
**Post date:** [April 8, 2020, 1:57pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/13 "2020-04-08T13:57:05Z")

</div>

Check my pull request

---

<div class="post-metadata">

**Author:** ![Serge](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/serge/32/10491_2.png) [@Serge](https://forum.rasa.com/u/Serge)\
**Post date:** [August 16, 2020, 10:03pm UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/14 "2020-08-16T22:03:17Z")

</div>

@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](http://socket.io) fix has been implemented in? [https://github.com/RasaHQ/rasa/pull/5578](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

---

<div class="post-metadata">

**Author:** ![chkoss](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chkoss/32/7186_2.png) [@chkoss](https://forum.rasa.com/u/chkoss)\
**Post date:** [August 24, 2020, 11:22am UTC](https://forum.rasa.com/t/integrate-rasa-with-live-chat/22314/15 "2020-08-24T11:22:46Z")

</div>

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](http://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/10) 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](https://github.com/RasaHQ/chatroom#setting-up-handoff-capability). There is also an [example bot](https://github.com/RasaHQ/helpdesk-assistant#handoff) 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.
