Last active
May 25, 2019 22:18
-
-
Save tbass134/273219ec5bd436d23bbecf8b76109b2d to your computer and use it in GitHub Desktop.
Dialogflow webhook to transfer user to live agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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