Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Created June 17, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeefe/c82af4be2454dc829719a59bbe98a282 to your computer and use it in GitHub Desktop.
Save jkeefe/c82af4be2454dc829719a59bbe98a282 to your computer and use it in GitHub Desktop.
Code that actually works as a bridge between Twilio & Dialogflow on AWS Lambda
// 1. This uses claudiajs & its API Gateway to deploy to lambda & handle api post
// 2. Assumes an existing dialogflow project, google cloud project, and google cloud billing
// 3. Got credentials .json file using steps 1-5 (ONLY) here:
// https://cloud.google.com/docs/authentication/getting-started
// 4. Put that json file in a directory called "sekrets' which I added to .gitignore
// 5. Other helpful options for Google authentication are here:
// https://stackoverflow.com/questions/50355556/how-authenticate-with-gcloud-credentials-an-dialogflow-api
// 6. Requires npm installation of @google-cloud/dialogflow & twilio
// 7. When deployed, appended the word "endpoint" to the lambda URL I gave to Twilio as a POST webhook
// for incoming SMS messages
// Dialogflow setup
const dialogflow = require('@google-cloud/dialogflow')
const projectId = '<google-cloud-project-id>'
const credentials_file = '/sekrets/<credentials-json-file-name>.json'
const lambda_runtime_directory = '/var/task'
const credentials_file_path = lambda_runtime_directory + credentials_file
const sessionClient = new dialogflow.SessionsClient({
projectId,
keyFilename: credentials_file_path,
});
// Twilio setup
const MessagingResponse = require('twilio').twiml.MessagingResponse;
// API builder setup
var ApiBuilder = require('claudia-api-builder');
var api = new ApiBuilder();
module.exports = api;
// code executed for each call to the lambda function
api.post('/endpoint', async function(req, res){
// from Twilio
const data = req.post
const text = data.Body;
const twilio_id = data.From;
const sessionId = twilio_id;
const languageCode = 'en-US';
const sessionPath = `projects/${projectId}/agent/sessions/${sessionId}`
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: text,
// The language used by the client (en-US)
languageCode: languageCode,
},
},
};
// Send sent text to Dialogflow and log result
const responses = await sessionClient.detectIntent(request);
const result = responses[0].queryResult;
const sendback = result.fulfillmentText
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
// Reply to texter via Twilio
const twiml = new MessagingResponse()
const message = twiml.message(sendback)
return twiml.toString()
}, {
// this optional 3rd argument changes the format of the response, for twilio
success: { contentType: 'application/xml' },
error: { contentType: 'application/xml' }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment