I said there was more to come, right?

First we did the Watson NLU middleware. Then we did the AWS Comprehend middleware. Now we're adding the Google Cloud Platform's Natural Language API. Each of these middleware packages performs text analysis that allows you to gather better meta data on chatbot messages, and each is essentially swappable--following a similar pattern. This makes switching between them easy.

So let's follow the pattern of the previous blog posts, and dive in a bit.

Start by installing the package:

npm install @botbuildercommunity/middleware-google-language --save  

Once you have that installed, you can include the Google Natural Language API middleware in your Bot Framework code just like you do with any other middleware:

import { SentimentAnalysis } from '@botbuildercommunity/middleware-google-cloud';

adapter.use(new SentimentAnalysis());  

You'll notice in this example (much like with the AWS Comprehend package), that we aren't passing anything into the constructor--not even credentials. Google assumes an environment variable (GOOGLE_APPLICATION_CREDENTIALS) that is set to the path where you are storing the Google Cloud Platform credential JSON file.

See the Google Cloud Platform documentation for more about credentials and set up.

In the above code, we're importing the SentimentAnalysis middleware. Once you have the middleware set up, you can check the sentiment of a particular message off of the turn state with something like the following:

context.turnState.get("sentimentScore");  

This very much follows the same convention as the Text Analytics API package and the Watson NLU package (differing slightly from the AWS Comprehend package).

Here's a quick code snippet from the example that's inside the repository for the package:

adapter.use(new SentimentAnalysis());

server.post("/api/messages", (req, res) => {  
    adapter.processActivity(req, res, async (context) => {
        if (context.activity.type === "message") {
            await context.sendActivity(`You said "${context.activity.text} with a sentiment of ${context.turnState.get("sentimentScore")}"`);
        }
    });
});

You can find more information in the GitHub repository. In addition, this is just bare bones functionality. More advanced functionality will be built in soon.

(Photo by Trending Topics 2019)