Here, you will modify your code to use Cosmos DB to dynamically query data. You will remove the constant variable quotes that contain the author quotes.
- Open Command Prompt and go to the directory where the server.js file is.
- Type npm install documentdb --save. This will install the Cosmos DB component that you can use in the Node.js code to access Cosmos DB and query data. Also --save will save documentdb as dependencies in package.json.
- Open server.js in Visual Studio Code.
- Declare the Cosmos DB client using var documentClient = require('documentdb').DocumentClient.
- Declare the Cosmos DB endpoint and primary key to create a Cosmos DB client: var dbClient = new documentClient(endpoint, { "masterKey": primaryKey }).
- Declare colLink , which will be used to query Cosmos DB.
The following code shows the declarations for querying CosmoDB:
var documentClient = require('documentdb').DocumentClient
var endpoint = "https://henrydb.documents.azure.com:443/";
var primaryKey = "ZJBcmZppBpZB5nIfjUOllvSmoLrvo6uzYGMoRaAcAayVZG0LG9HXTdjZhkvjE6OWFZvowxFiNeRxBpZBWm1uLg==";
var dbClient = new documentClient(endpoint, { "masterKey": primaryKey });
var dbLink = 'dbs/myDb';
var collLink = dbLink + '/colls/myCollection';
- Remove const quotes. We will replace this by querying the quotes from Cosmos DB.
- Create a sendQuoteResponse function. There are three functions that you need to incorporate into the Cosmos DB code: sendQuote, sendQuoteWithFeeling, and sendAuthorQuote. You will be refactoring the code, as all three of these functions will use Cosmos DB in very similar ways. You will create new function called sendQuoteResponse , which will basically receive the query, the index and the functionName. The query contains the SQL-like query syntax that will be sent to Cosmos DB to pull the result sets, the index will be used to select a specific result from the array of quotes, and finally, the functionName will be used for logging to make sure that, if something fails, you will know which function caused the error. Using dbClient.queryDocuments(collLink, query).toArray(function (err, quotes), you will send the query request to Cosmos DB and then receive the result. When the query receives the result, it will send the response back to appropriate Dialogflow quote.
The following code shows the sendQuoteResponse:
function sendQuoteResponse(query, index, functionName) {
dbClient.queryDocuments(collLink, query).toArray(function (err, quotes) {
if(err)
return response.status(400).end('Error while calling cosmoDB in ' + functionName);
else {
let responseJson = { fulfillmentText: quotes[index].quote };
console.log(functionName + ":" + JSON.stringify(responseJson));
response.json(responseJson);
}
});
}
- Modify sendQuote, sendQuoteWithFeeling, and sendAuthorQuote to use sendQuoteResponse. Previously, fulfilmentTexts were set with static quotes. In the new code, they are replaced with sendQuoteResponse but the query is built independently in each of the functions and sent to the sendQuoteResponse. The sendQuote function and the sendQuoteWithFeeling function creates the query let query = "SELECT * FROM c", where the query selects all the quotes stored in Cosmos DB. On the other hand, sendAuthorQuote creates let query = "SELECT * FROM c where contains(lower(c.author), '" + parameters.Author.toLowerCase() + "')", where the query filters the quotes by author name.
The following code shows the modified sendQuote, sendQuoteWithFeeling, and sendAuthorQuote:
function sendQuoteWithFeeling () {
let responseJson, randomNumber;
if(parameters.Feeling === "happy"){
randomNumber = Math.floor(Math.random() * 5);
}
else {
randomNumber = Math.floor(Math.random() * 4) + 5;
}
let query = "SELECT * FROM c";
sendQuoteResponse(query, randomNumber, "sendQuoteWithFeeling");
}
function sendQuote () {
let randomNumber = Math.floor(Math.random() * 9);
let query = "SELECT * FROM c";
sendQuoteResponse(query, randomNumber, "sendQuote");
}
function sendAuthorQuote () {
let query = "SELECT * FROM c where contains(lower(c.author), '" + parameters.Author.toLowerCase() + "')";
sendQuoteResponse(query, 0, "sendAuthorQuote");
}
- Open Postman and test this locally, then deploy it to the Microsoft Azure Node.js server using FTP.