Handing off to human on Slack

Hi everyone!

I saw this documentation about Handing off to a human agent, however it is not very clear. I wonder if anyone knows how to custom this action so that it sends a Slack message to a “live agent”, in this case, me?

Thanks in advance!

BR,

Mia

Hi @mia.le0711! I actually implemented something like that last week :slight_smile:

I created a custom action that:

  • dispatch a message to the user that it is struggling and will handover to a human
  • sends a message in a Slack channel with some information about the issue
  • pause the bot by returning the ConversationPaused() event

On Slack, you need to create an App and get your credentials. I gave it permission to post in any channel but that’s up to you.

Using the Slack v1 API (it will be different if you use v2), I did something like:

response = client_slack.api_call(
    "chat.postMessage", 
    channel="#support", 
    text=f"@botsupport user {tracker.sender_id}" is struggling! Please take over", 
    link_names=1 
)

Note that the link_ǹames is here because I am tagging a group of users in the text with @botsupport (this does not work with direct mention of a user, only groups)

Finally, as mentioned in the doc you linked, I added a trigger on a specific intent to trigger this handover action. I also do something like that if the bot struggles several time in a row.

Hope this helps!! Nicolas

Hi Nicolas!

Thank you so much for your answer, after taking a look at some other documents, I made it to work! One small problem though, I cannot get the User ID who needed help :pensive::pensive: As I understqand, tracker.sender_id will help? However it cannot seem to work for me. Can you help me with this? :innocent:

Again, I really appreciate your help!

@nbeuchat I made it work, hehe… But now it is in UserID and not Username:speak_no_evil: Do you know how to turn it into name instead? :blush:

Great you could make it work! How do you normally know the name of the user in your conversation? We do that through slots so in your action, you can simply do something like:

tracjer.get_slot("user_name") or whatever slot you stored your name in!

My chatbot is an educational one (FAQ) so it does not ask for names during conversation. I have been googling around for a solution to “translate” Slack ID to Slack username. But haven’t found any :confounded:

I guess I will have to think of the other way haha. Thank you so much again for your kind help. :innocent:

Got it! No idea how to do that, maybe open another question in this forum to get more attention on that specific issue?

Cheers Nicolas

I figured it out, even though it’s a bit too manual hehe but considering the small team I have now, I think it’s fine. I made a dictionary with user ID and Username, then assign the value to the user ID in the message sent. I type here so that if others have the same problem they can have a solution right away! :slight_smile:

Thanks again @nbeuchat :blush:

1 Like

This sound like a great solution to do human handoff.

Now it will just pause the conversation and send a message to slack, so that’s why i’m asking if @nbeuchat or @mia.le0711 created a feature to:

  1. Receive users messages to slack after conversation is paused
  2. Send a message to user from slack

And i guess the conversation will end when human sends a keyword from slack and then bot continues?

Hi Kim,

I’m sorry I don’t quite understand your question. Can you please specify a bit more? :slight_smile:

hey @mia.le0711 and @nbeuchat

can you show me your custom actions and how it sends a slack message to a human ?

Thanks in advance

Hey @KhalidBentaleb, sure, have a look! I simplified it a bit as we have quite some code specific to our use case just to generate the right message for our customer support team.

I am using the slack client 1.3 (slackclient~=1.3) as I had issues with version 2. link_names is set to 1 so as to have the mention with @customersupport work (only works with groups, not individual users).

class ActionHandoverToSupport(Action):
    def name(self):
        return "action_handover_to_support"

    def run(
        self,
        dispatcher,
        tracker,
        domain,
        reason: Text = None,
    ):
        events = []
        text = f"@customersupport Help needed for user {tracker.sender_id}"
        response = client_slack.api_call(
            "chat.postMessage", channel=SLACK_SUPPORT_CHANNEL, text=text, link_names=1
        )

        if response.get("ok"):
            dispatcher.utter_message(template="utter_handover_to_support")
            events.append(ConversationPaused())
        else:
            dispatcher.utter_message(
                template="utter_technical_issue",
                extra_message="I could not send a message to my humans 😢",
            )

        return events

Hope that helps!! Nicolas

1 Like

Hey @nbeuchat

Thank you so much for your answer, I didn’t understand the working mechanism handoff to human because I had some errors. and Tell me if I’ll add another codes

Great, hopefully this helps! Let me know if you have any other issue in implementing this :slight_smile:

hii @nbeuchat

always I get this message

Can you share your logs? Try to set a breakpoint within the action to see where it fails. And what response do you get from slack

hey @nbeuchat, Still not working if you have any idea ?

Could you share the logs of the action server itself? These are the logs for the core server. Your action is correctly called but it’s probably failing when trying to send a message to slack.

Also, can you try to send a message to slack from a normal python console (ie: outside of the action server), that way you can check if you are able to send any message at all.

I can’t create channel bot for groups.

Not sure I understand, what do you mean? Also, the channel name is not the one you see on slack (for us, it looks something like CRNK58KX3, you can see it in the URL)

thank you @nbeuchat, now I can send a message to slack but I don’t get any responses from slack channel

Sounds like some async issue. Is your run function async? You might need to put an await as well. What version of the slack client are you using in the action server?