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?
Hi @mia.le0711! I actually implemented something like that last week
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.
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 As I understqand, tracker.sender_id will help? However it cannot seem to work for me. Can you help me with this?
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
I guess I will have to think of the other way haha. Thank you so much again for your kind help.
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!
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
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
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)
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?