# Forwarding messages to Slack and vice versa for human hand-off

**URL:** <https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447>\
**Category:** Rasa Open Source\
**Created:** [May 5, 2020, 1:10pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447 "2020-05-05T13:10:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arneverster](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/arneverster/32/9376_2.png) [@arneverster](https://forum.rasa.com/u/arneverster)\
**Post date:** [May 5, 2020, 1:10pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/1 "2020-05-05T13:10:01Z")

</div>

This [thread](http://forum.rasa.com/t/handing-off-to-human-on-slack/26203) was very helpful to get started with a human hand-off in RASA, but there’s still some way to go before we have a working example.

In this particular thread, code is provided that will allow us to send a message to a Slack channel when a user requests to talk to an agent.

It is not discussed how the agent can forward messages back to the user and how the user can continue to engage with the agent until it is indicated that the bot can take over again.

The main issue to solve is the following: how can we extend the code in order to send user messages to Slack and Slack messages to the user so long as the hand-off is required?

The custom action provided in the thread looks as follows:

```
import os
from slack import WebClient
from slack.errors import SlackApiError
 
client_slack = WebClient(token=os.environ["SLACK_API_TOKEN"], run_async=True)
SLACK_SUPPORT_CHANNEL = os.environ["SLACK_SUPPORT_CHANNEL"]
 
 
class ActionHumanHandoff(Action):
   def name(self):
       return "action_human_handoff"
 
   async def run(
       self, dispatcher, tracker, domain, reason: Text = None,
   ):
       events = []
       text = f"Help needed for user {tracker.sender_id}"
       try:
           response = await client_slack.chat_postMessage(
               channel=SLACK_SUPPORT_CHANNEL, text=text
           )
           if response.get("ok"):
               dispatcher.utter_message(template="utter_handover_to_support")
               events.append(ConversationPaused())
       except SlackApiError as e:
           dispatcher.utter_message(
               template="utter_technical_issue", extra_message=e.response["error"],
           )
       return events
```

---

<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:** [May 7, 2020, 4:12pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/2 "2020-05-07T16:12:27Z")

</div>

Hi, one possible way to accomplish that would be the following:

> [@Handing off to Human agent](http://forum.rasa.com/t/handing-off-to-human-agent/21528/13):
>
> A reliable way of accomplishing without switching the frontend for routing messages (which is a good way of going about it) would be defining your own policy in addition to a custom action to facilitate the communication. Give the policy the highest priority in your configuration and let it predict the action when a certain slot is triggered like handoff\_active. After you are done talking with the human agent you can unset the slot to finish the story and optionally predict some follow-up actio…

So this means you

- have one action `action_human_handoff` like you suggest above. In addition to what it does already, have it set a slot like `handoff_active`.
- add a policy like described in the post I linked

And then for sending messages back and forth between the user and the human support, you could implement another custom action like `action_talk_to_human` which your policy always predicts while the `handoff_active` slot is set. The action `action_talk_to_human` could e.g. use the [Slack API for sending messages](https://api.slack.com/messaging/sending#threading) to send the last message from the user as a reply to the thread that was started by `action_human_handoff`, use the [Slack API for retrieving messages](https://api.slack.com/messaging/retrieving#pulling_threads) to get the answer that is sent by the human support, and then forward this answer to the user.

Once the hand-off is no longer required, the human support should send a specific message, e.g. `/handback`. The action `action_talk_to_human` needs to recognise this and unset the slot `handoff_active`.

Hope that helps, let me know if you have more questions.

---

<div class="post-metadata">

**Author:** ![arneverster](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/arneverster/32/9376_2.png) [@arneverster](https://forum.rasa.com/u/arneverster)\
**Post date:** [May 7, 2020, 5:15pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/3 "2020-05-07T17:15:02Z")

</div>

Awesome stuff!

I’ll work in this for a while and provide feeback when I get it working 🙂

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [May 8, 2020, 3:19am UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/4 "2020-05-08T03:19:55Z")

</div>

Thanks for the detailed instructions, this is the best approach to handle the human hand-off so far. I was exploring the **looped FollowupAction** path and quickly realized it would be overridden with new user input.

I will work on this too and will share my implementation once done.

My case: I have connected my bot with MS Teams and when the user request for human agents, the chat will be queued up in [service now chat dashboard](https://docs.servicenow.com/bundle/orlando-customer-service-management/page/product/customer-service-management/concept/c_ChatFeature.html) for the agents to pick it up.

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [June 1, 2020, 1:56pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/5 "2020-06-01T13:56:07Z")

</div>

For future readers, this is the path I have taken so far:

1. User types ‘talk to live agent’ or something similar.
2. The custom action `action_live_agent` creates a live support chat in service now and sets the `handoff_active` to true. It also sets slots like `live_chat_id` (live chat id), `live_chat_user` (user\_id to differentiate messages between agent and user), and `live_chat_last_updated` (timestamp of the last sent message to compare and only get the new messages later).
3. The custom handoff policy `HandoffPolicy` with the highest priority checks `handoff_active` slot and predicts action `action_live_agent_chat` until `handoff_active` slot is set to false.
4. `action_live_agent_chat` will post user messages to the live chat using `live_chat_id`. It will then do the following in order:

- Check user message `if message == "/restart" or message == '/end_chat'`. If true, it will end the live chat by calling leave chat API and set slots `handoff_active`, `live_chat_id`, and `live_chat_last_updated` to None. `HandoffPolicy` will then stop predicting the custom action and bot will start handling responses. Ref. Screenshot 1.
- If user message is not ‘/restart’ or ‘/end\_chat’, the custom action will post the latest message to live agent chat thread and will retrieve the message thread.
- It will loop through the message thread. If the message creator is not `live_chat_user` and message timestamp is greater than `live_chat_last_updated`, it will utter the messages to the end-user. After uttering the messages to the end-user, it will set `live_chat_last_updated` to the timestamp of the latest message in the thread. Ref. Screenshot 2.
- It will then check if the received message contains the text `has closed the support session` (this is a part of the message that is sent once the live agent ends the chat session). If true, it will end the live chat by calling leave chat API and set slots `handoff_active`, `live_chat_id`, and `live_chat_last_updated` to None. Ref. Screenshot 3.
- If any of the API calls (posting/retrieving messages) throws an error, it will end the live chat by calling leave chat API and set slots `handoff_active`, `live_chat_id`, and `live_chat_last_updated` to None. It will utter message with details like (chat summary, chat id, last updated). If there is an error in calling leave chat API, it will set slots `handoff_active`, `live_chat_id`, and `live_chat_last_updated` to None and utter generic message like “The chat session is closed/could not be located. Please try reconnecting to live agent if you still need help!”

**Screenshot 1: User initiated live chat end:**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/0/0fd82030f5848946f9ea7e1447d31ca1cee19c68.png)

**Screenshot 2: Live agent conversation:**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/c/c249f4ad48d1a52422c240b64de2741e9331b52e.png)

**Screenshot 3: Live agent closed the chat session:**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/3/389b5602df34329a47135acd4b4c7bea352c50a7.png)

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [June 1, 2020, 2:13pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/6 "2020-06-01T14:13:08Z")

</div>

Thank you so much for your detailed instruction, I have posted my implementation so far.

> [@chkoss](#):
>
> use the [Slack API for retrieving messages](https://api.slack.com/messaging/retrieving#pulling_threads) to get the answer that is sent by the human support, and then forward this answer to the user.

I have a question in the quoted part. I have a service now API to get the message thread, should I be polling the API to get the updated messages or is there a better implementation?

---

<div class="post-metadata">

**Author:** ![arneverster](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/arneverster/32/9376_2.png) [@arneverster](https://forum.rasa.com/u/arneverster)\
**Post date:** [June 1, 2020, 2:31pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/7 "2020-06-01T14:31:29Z")

</div>

Really good stuff man. I’ve yet to start on my implementation, just got back from leave. Will post solution soon 🙂

---

<div class="post-metadata">

**Author:** ![pcoutu](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pcoutu](https://forum.rasa.com/u/pcoutu)\
**Post date:** [June 10, 2020, 7:30pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/8 "2020-06-10T19:30:04Z")

</div>

Hi Niraj!

First, I just want to say thank you for posting a detailed description of your implementation. It was a great reference for the development of my own solution.

As far as your API Polling question. I’m currently using Slack for my implementation, which offers an Events API with a configurable Callback URL.

I set that endpoint up in the Rasa Server, which is essentially a modified version of the execute\_action() function associated with the “/conversations/\<conversation\_id\>/execute” endpoint.

It checks to make sure that the message isn’t from the bot, then parses the sender\_id (associated with the parent message of the Slack thread), and fetches the tracker. It then calls for a custom action to execute, that I setup to retrieve the most recent message and emit it to the tracker’s output channel.

This way, there is no polling. The endpoint receives a request when a new message is posted in the Slack channel, and then the action executes.

I’m not familiar with the Teams APIs, but it looks like this might offer some similar functionality:

[https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-outgoing-webhook](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-outgoing-webhook)

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [June 10, 2020, 10:54pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/9 "2020-06-10T22:54:33Z")

</div>

Thanks Patrick! Your explanation seems to be the right way of doing it. I will work on the implementation in couple of days. I will share it for future implementors of servicenow and MS Teams API.

---

<div class="post-metadata">

**Author:** ![stephens](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/stephens/32/1632_2.png) [@stephens](https://forum.rasa.com/u/stephens)\
**Post date:** [July 1, 2020, 10:56pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/10 "2020-07-01T22:56:28Z")

</div>

I think it’s not a good approach to use Rasa as the centralized messaging router. It’s not designed to play this role. Even if you implement the suggested solution, your tracker store will contain conversations that are unrelated to the domain of the bot.

Instead, there should be a client UI that switches between Rasa and other services (Slack, another Rasa bot, etc). The action would send a message that the client UI recognizes as a transfer (via slot metadata) and pauses the conversation. The UI then connects to Slack or some other service.

---

<div class="post-metadata">

**Author:** ![basil-chatha](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/basil-chatha/32/9260_2.png) [@basil-chatha](https://forum.rasa.com/u/basil-chatha)\
**Post date:** [July 25, 2020, 6:09pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/11 "2020-07-25T18:09:16Z")

</div>

@nchaurasHey Niraj, thanks for this post. It’s very helpful. Could you share the code for your policy? I’ve been trying to implement a policy that predicts an action when a slot is set but haven’t been having much luck

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [July 27, 2020, 9:38pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/12 "2020-07-27T21:38:52Z")

</div>

In your custom policy ` __init__ ` method, set the priority to highest int value (depends on your config and the priority for other policies). e.g. `priority: int = <set to highest int value>` and `handoff_action_name: Text = "action_live_agent_chat"`.

In `predict_action_probabilities` method, predict the handoff\_action if `tracker.get_slot('handoff_active')` is True.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![basil-chatha](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/basil-chatha/32/9260_2.png) [@basil-chatha](https://forum.rasa.com/u/basil-chatha)\
**Post date:** [July 28, 2020, 10:19pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/13 "2020-07-28T22:19:36Z")

</div>

@nchaurasI put the priority in `config.yml` like so:

```auto
...
  - name: custom_policies.testpolicy
    priority: 6

```

the ` __init__.py` file in the directory housing my custom action is empty.

For some reason it seems that the policy is not actually running the action, though. Here’s my code in `predict_action_probabilities`:

```auto
 is_triggered = tracker.get_slot('my_slot')

if is_triggered:
    logger.info('if statement ' + str(is_triggered) + ' ' + str(type(is_triggered)))
    return confidence_scores_for("action_my_action", 1.0, domain)
else:
    logger.info('in else statement ' + str(is_triggered) + ' ' + str(type(is_triggered)))
    return self._default_predictions(domain)

```

I also made sure to mount the file in `docker-compose.override.yml` so that shouldn’t be an issue

---

<div class="post-metadata">

**Author:** ![flore](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/flore/32/9959_2.png) [@flore](https://forum.rasa.com/u/flore)\
**Post date:** [August 4, 2020, 5:58pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/14 "2020-08-04T17:58:42Z")

</div>

@stephens @nchauras @pcoutu Hi!

Here we are trying to do human-handoff with Rasa bot. The hard part is retrieve the message from the human agent and send it to the user, but I do not want to do polling whitin an action to do that.

Maybe something like this can be implemented:

 ![Rasa handoff architecture](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/0/0efcb4dfffe4d898f4aa9dc464cc808985f0a2b4.jpeg)

Can somebody help me with how to implement this? I’m thinking about rabbit or something like this but I’m not sure.

---

<div class="post-metadata">

**Author:** ![stephens](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/stephens/32/1632_2.png) [@stephens](https://forum.rasa.com/u/stephens)\
**Post date:** [August 4, 2020, 6:23pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/15 "2020-08-04T18:23:05Z")

</div>

We recently added an enhancement to the chatroom Rasa widget [here](https://github.com/RasaHQ/chatroom) and we documented it’s use in the example helpdesk-assistant [here](https://github.com/RasaHQ/helpdesk-assistant#handoff).

It is an example of what you show above without the use of a Router or Handoff DB. When the user requests a transfer, the Rasa response can include slots or metadata that is passed to the UI component. In the example implementation, it passes the URL to another Rasa chatbot but you could add your own metadata.

---

<div class="post-metadata">

**Author:** ![flore](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/flore/32/9959_2.png) [@flore](https://forum.rasa.com/u/flore)\
**Post date:** [August 5, 2020, 6:08pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/16 "2020-08-05T18:08:46Z")

</div>

Thank you, but I’m using whatsapp not a personal UI

---

<div class="post-metadata">

**Author:** ![nchauras](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nchauras/32/16965_2.png) [@nchauras](https://forum.rasa.com/u/nchauras)\
**Post date:** [August 25, 2020, 2:49pm UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/17 "2020-08-25T14:49:43Z")

</div>

I ended up creating Webhook to push human messages to the bot using MS Teams API directly (as MS Teams is the front end for me). The messages from the user are pushed out to the human agent using rasa custom action & handoff policy and the human agent reply triggers webhook response directly to the front end.

I followed this route to record the user messages as to why the human agent was requested and we do a weekly review to include those use-cases in the bot itself. This has helped us with the CDD tremendously.

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [September 8, 2020, 11:46am UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/18 "2020-09-08T11:46:48Z")

</div>

Interesting! Does that mean that you review conversations containing both human chats and bot chats mixed together?

---

<div class="post-metadata">

**Author:** ![manoj\_kumar](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@manoj\_kumar](https://forum.rasa.com/u/manoj_kumar)\
**Post date:** [December 9, 2020, 6:47am UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/20 "2020-12-09T06:47:10Z")

</div>

Hi @nchauras,

Could you share the implementation code or repo to go through?

Thanks.

---

<div class="post-metadata">

**Author:** ![shubham12et1062](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@shubham12et1062](https://forum.rasa.com/u/shubham12et1062)\
**Post date:** [December 30, 2020, 6:35am UTC](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447/21 "2020-12-30T06:35:23Z")

</div>

Hi, were you able to implement this? Can you please share the steps you took

[Next page](https://forum.rasa.com/t/forwarding-messages-to-slack-and-vice-versa-for-human-hand-off/28447.md?page=2)
