Tutorial 5 - Gracefully handling operating hours during handoff

Gracefully handling operating hours in bot-to-humans handoff

NOTE: this tutorial uses Dialogflow just as an example for a programmatic external chatbot integration. If you instead need to easily embed your Dialogflow Agent in your conversations, please use the Tiledesk Dialogflow Connector, the out-of-the-box Dialogflow integration that doesn't require programming skills.

Fork the tutorial code

We'll start from Dialogflow Tutorial 1, just adding a small library to our original endpoint.

You must use the code in Tutorial 1. The code is available on Github here.

Fork the tutorial code using the Fork button. Now you have a copy of the tutorial on your own repo.

Introduction

What happens if, while you switch to the human operator, your support team is outside of operating hours? The request will be placed in the unserved queue and will became served as soon as your team became available again. Meanwhile you should notice to the user that the request will be taken in some hours, or probably the day after. So, telling "I'm putting you in touch with an operator" is not enough.

How can you write an handoff message that depends by operating hours? You can mix Dialogflow messages fulfillment and Tiledesk APIs to reach your goal.

Enable Agent fulfillment

Open Dialogflow dashboard on your agent, the in the menu fulfillment option enable "Webhook" option. In the URL field insert your heroku app url where you pubblished your app followed by /dfwebhook and your project_id:

This "webhook" endpoint is already provided in the script.

// Tutorial 4.1 - Webhook for Bot-to-Agent handoff message based on opening hours
app.post('/dfwebhook/:project_id', (req, res) => {
  const fulfillmentText = req.body.queryResult.fulfillmentText
  console.log("fulfillmentText:", fulfillmentText)
  const languageCode = req.body.queryResult.languageCode
  console.log("languageCode:", languageCode)
  // replace the following with your prject id
  const project_id = req.params.project_id
  const intent = req.body.queryResult.intent.displayName.toUpperCase()
  if (intent === "TALK TO AGENT") {
    if (resbody && resbody.token) {
      const tdclient = new TiledeskClient()
      tdclient.openNow(function(isopen) {
        var df_res = {}
        if (isopen) {
          df_res['fulfillmentText'] = "We are open! Switching to agent\\agent"
        }
        else {
          df_res['fulfillmentText'] = "I'm sorry but we are closed right now."
        }
        res.status(200).send(JSON.stringify(df_res));
      })
    }
  }
});

This endpoint uses a projeect_id in the webhook URL that you must have care to replace with your own.

Re-train the 'talk to agent' intent to use fulfillment

Fulfillment is not automatically actvive on the intents. You must activate it for each intent that you want to reply dynamically. We will re-train the 'talk to agent' intent to use our webhook to reply, instead of the static response.

Switch to the intent UI from Dialogflow dashboard, the go on the form bottom and switch to "on" the fulfillment button, as in the following picture:

Now go on your project in Tiledesk dashboard and activate operating hours, taking care to put offline the interval when you ask the bot to switch to human agents. If you will try to switch to human operators during offline hours you will get the following message:

Do you have feedback on this article? Please send us your feedback writing an email to info@tiledesk.com

Enjoy Tiledesk!

Last updated