Skip to content

Instantly share code, notes, and snippets.

@tbass134
Last active May 25, 2019 22:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbass134/273219ec5bd436d23bbecf8b76109b2d to your computer and use it in GitHub Desktop.
Save tbass134/273219ec5bd436d23bbecf8b76109b2d to your computer and use it in GitHub Desktop.
Dialogflow webhook to transfer user to live agent
function getInfo(contexts) {
var product = contexts[contexts.length - 1]['parameters']['Product']
var state = contexts[contexts.length - 1]['parameters']['geo-state-us']
for (var i = 0; i < contexts.length; i++) {
var context = contexts[i]['parameters']
if (typeof context['Product'] != "undefined" && typeof context['geo-state-us'] != "undefined") {
return context
}
}
return null
}
app.post('/', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Content-Type", "application/json");
console.log("req.body", JSON.stringify(req.body, null, 2));
var json = null
if (intent == "transfer - ask name - ask product - ask state") {
var contexts = req.body['queryResult']["outputContexts"]
var data = getInfo(contexts)
var text = "Great. I’m going to hand you over to a Customer Service Representative now."
var parameters = { "text": text }
var json = {
'followup_event_input': { name: 'transfer_event', 'language_code': 'en', "parameters": parameters },
"outputContexts": [
{
"name": req.body.session+"/contexts/setCRMData",
"parameters": data,
"lifespanCount": 5
}
]
}
return res.json(json)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment