(Ryan Veitch, Silas Sundar, and Babatunde Idahor, technical support engineers, and Sri Venkatesan, a software engineer, also contributed to this project.)

During the Eng-Curious Holiday Hackathon at New Relic, in which members of our global technical support and engineering teams got together and hacked away at some fun projects over two days, a few of us wrote an application that allows you to use Google Assistant to query New Relic Insights with your voice.

The app uses Dialogflow, a cloud-based service for creating voice-recognition tools. The service interprets what the user says, processes the important parts of what’s been said, and sends that information via a POST request through the Amazon API Gateway connected to an AWS Lambda function. The Lambda function constructs an Insights query (via the Insights API) and then returns the results to Dialogflow, which then speaks the results to the user.

Dialogflow app workflow

We think it’s a very cool little app, and in fact, it’s not too different from the Alexa skill a fellow Relic created last year to read Insights from an Alexa device.

Let’s walk through the technical details.

Defining the Dialogflow intents and entities

When a user issues a command, Dialogflow interprets what the user has said and what action to take—this is known as the the intent. It also collects the entities of the intent—the descriptors that tell Dialogflow what to act on. An Insights query has three entities: attribute, event, and timeline: "SELECT <attribute> from <event> since <timeline>".

So in our app, we set up a query to ask, “Since yesterday how many browser views have there been?”

Dialogflow creating phrases

As you can see, Dialogflow picks the entities out of the sentence. It has selected the attribute as “how many,” the event as “Browser views,” and the timeline as “since yesterday.”

At this point, Dialogflow translates these entities into a JSON data structure and sends them as a POST request, as shown in the following code sample. (Note that the three entities are contained in the parameters section.)

{
 "id": "46cd4e36-44e2-408f-aed2-b95f2cdaf9ff",
 "timestamp": "2017-12-15T09:09:41.95Z",
 "lang": "en",
 "result": {
 "source": "agent",
 "resolvedQuery": "since yesterday how many Browser views have there been?",
 "action": "getRequiredFields",
 "actionIncomplete": false,
 "parameters": {
 "attributes": "count(*)",
 "event": "PageView",
 "timeline": "%20SINCE%20yesterday"
},
 "contexts": [],
...
}

Getting the request to New Relic Insights

We use the Amazon API Gateway to send our POST request to a Lambda function.

Amazon API gateway

The Lambda function receives the POST request and sorts the data into a GET request for the Insights API.

Note that for the Lambda function to be able to execute this query through the Insights API, the query has to be in URL form:

https://insights-api.newrelic.com/v1/accounts/<MYNEWRELICACCOUNTNUMBER>/query?nrql="SELECT%20<attribute>%20from%20<event>%20since%20<timeline

Here’s what the Lambda function looks like:

import requests
import json

auth_out = "<MYNEWRELICKEY>"

def handler(event, context):
    print event
    try:
        result = event["result"]
        parameters = result["parameters"]
        attributes = parameters["attributes"]
        event = parameters["event"]
        timeline = parameters["timeline"]
    except KeyError:
        raise ValueError("Unrecognized payload" + json.dumps(event, indent=2))
    url = "SELECT%20" + attributes + "%20from%20" + event + "%20since%20" + timeline
    print url
    r = requests.get("https://insights-api.newrelic.com/v1/accounts/<MYNEWRELICACCOUNTNUMBER>/query?nrql=" + url, headers = {"Accept": "application/json", "X-Query-Key": auth_out})
    insightResult = json.loads(r.text)
    operation = attributes.split('(')[0]
    result = insightResult["results"][0][operation]

    response_out = {'speech': result,'displayText': result,'source': 'New Relic'}
    return

The response returned to the Dialogflow agent takes the form of

response_out = {'speech': result,'displayText': result,'source': 'New Relic'}

The speech and displayText for response_out is what the Dialogflow agent says and displays to the customer. In this case, it’s the number of browser views that is returned from the Insights GET request.

That’s pretty much it!

Now let’s see it in action:

Building this app during the hackathon was a great experience. In fact, the team that worked on the app has resolved to keep working on it—our plans are to improve everything!

Sean Winters is a Technical Support Engineer in New Relic's Dublin office. When not helping customers harness the power of New Relic, he's doing everything it takes to win his fantasy football league. View posts by .

Interested in writing for New Relic Blog? Send us a pitch!