Created
May 11, 2020 09:52
-
-
Save vernig/f208647d35a91f64d09c51df39d90764 to your computer and use it in GitHub Desktop.
Incoming Message function
This file contains hidden or 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
const fetch = require('node-fetch'); | |
const dialogflow = require('dialogflow'); | |
const projectId = '<Dialogflow JSON project_id>'; | |
const dfConfig = { | |
credentials: { | |
private_key: '<Dialogflow JSON private_key>', | |
client_email: '<Dialogflow JSON client_email>' | |
} | |
} | |
const autopilotWebhook = 'https://channels.autopilot.twilio.com/v1/<account_sid>/<autopilot_sid>/twilio-messaging/whatsapp' | |
async function detectIntent(query) { | |
// New session client | |
const sessionClient = new dialogflow.SessionsClient(dfConfig); | |
// The path to identify the agent that owns the created intent. | |
const sessionPath = sessionClient.sessionPath(projectId, '123456'); | |
// The text query request. | |
const request = { | |
session: sessionPath, | |
queryInput: { | |
text: { | |
text: query, | |
languageCode: 'it', | |
}, | |
}, | |
}; | |
const responses = await sessionClient.detectIntent(request); | |
return responses[0]; | |
} | |
async function executeQuery(query) { | |
let queryResult = {} | |
try { | |
console.log(`Sending Query: ${query}`); | |
intentResponse = await detectIntent(query); | |
console.log('Detected intent'); | |
console.log( | |
`Intent Name: ${intentResponse.queryResult.intent.displayName}` | |
); | |
console | |
// Use the context from this response for next queries | |
queryResult.success = true | |
queryResult.intent = intentResponse.queryResult.intent.displayName | |
queryResult.parameters = intentResponse.queryResult.parameters; | |
} catch (error) { | |
console.log('executeQuery() error') | |
console.log(error); | |
queryResult.success = false | |
} | |
return queryResult | |
} | |
function detectLanguage(text) { | |
body = { | |
q: text | |
} | |
return fetch('https://translation.googleapis.com/language/translate/v2/detect?key=' + process.env.TRANSLATE_API_KEY, { | |
method: 'POST', | |
body: JSON.stringify(body), | |
headers: { 'Content-Type': 'application/json' }, | |
}) | |
.then(response => response.json()) | |
.then(resJson => Promise.resolve(resJson.data.detections[0][0].language)); | |
} | |
exports.handler = function(context, event, callback) { | |
detectLanguage(event.Body) | |
.then(language => { | |
console.log(language) | |
let response = '' | |
switch (language) { | |
case 'it': | |
// Gestisci richieste in italiano | |
executeQuery(event.Body) | |
.then(result => { | |
if (result.intent === 'ordine') { | |
response = `Grazie! Stiamo preparando ${result.parameters.fields.number.numberValue} ${result.parameters.fields.any.stringValue}`; | |
} else { | |
response = 'Non ho capito. Puoi provare di nuovo?'; | |
} | |
callback(null, response); | |
}) | |
break; | |
case 'en': | |
// Handle requests in english | |
response = new Twilio.twiml.MessagingResponse(); | |
response.redirect(autopilotWebhook) | |
callback(null, response); | |
break; | |
} | |
}) | |
.catch(error => { | |
callback(error, null); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment