Dialogflow

Dialogflow Authentication

Dialogflow is an NLP API for matching strings to intents or actions. Intents are created on the Dialogflow website.

With version 2 of Dialogflow, you will need a service account - a Google account associated with your Google Cloud project. You will need to log into your Google Cloud Platform account and head to Create service account key, you can follow the steps in the official documentation - Getting Started with Authentication.

Once you have your JSON file, you will need to place this file somewhere safe and add its path to an environment variable named GOOGLE_APPLICATION_CREDENTIALS (the official documentation explains how to do this as well). Once this is done you can start using the Dialogflow parser.

Localization

If you use opsdroid in a language other than English, this parser will automatically grab the language code that you have set on your configuration.

If you want to run this parser in a different language you can overwrite the language configuration by adding the lang parameter on this parser configuration.

Note: You need to add additional languages to be used with dialogflow, to do this you need to log into your console and under your bot name you can add additional languages.

Configuring opsdroid

To use Dialogflow with opsdroid, you need to add it to your parsers list along with your project-id and set the correct authentication method as mentioned above.

You can also set a min-score option to tell opsdroid to ignore any matches which score less than a given number between 0 and 1. The default for this is 0 which will match all messages.

parsers:
  dialogflow:
    project-id: <your project id>  # Required
    min-score: 0.6 # optional

Using the parser with a skill

Let’s have a look at how you can use this parser and the match_dialogflow_action and match_dialogflow_intent decorators on a skill.

These decorator take one parameter (the name of the intent/action to match), any skill (function or class method) decorated with this matcher, will trigger that skill.

prsers:
  dialogflow:
    project-id: <your project id>  # Required
    lang: 'pt'

This will make the parser to use the Portuguese language when matching the string to an intent or an action.

opsdroid.matchers.match_dialogflow_action(action)

Return Dialogflow action match decorator.

Decorator that calls a function on any Dialogflow intent with a specified action.

Parameters

action (str) – Dialogflow action

Returns

Decorated Function

opsdroid.matchers.match_dialogflow_intent(intent)

Return Dialogflow intent match decorator.

Decorator that calls a function on specific Diaglogflow API intents.

Parameters

intent (str) – Dialogflow intent name

Returns

Decorated Function

Example 1

from opsdroid.skill import Skill
from opsdroid.matchers import match_dialogflow_action

class MySkill(Skill):
  @match_dialogflow_action('mydomain.myaction')
  async def my_skill(self, message):
      await message.respond('An appropriate response!')

The above skill would be called on any intent which has an action of 'mydomain.myaction'.

Example 2

from opsdroid.skill import Skill
from opsdroid.matchers import match_dialogflow_intent

class MySkill(Skill):
  @match_dialogflow_intent('myIntent')
  async def my_skill(self, message):
      await message.respond('An appropriate response!')

The above skill would be called on any intent which has a name of 'myIntent'.

Creating a Dialogflow bot

You can find a quick getting started with Dialogflow guide here and you can also follow the quickstart guides.

If you want to use Dialogflow in a different language other than English, all you need to do is specify the lang parameter in opsdroid’s configuration. Then change/add another language to your Dialogflow agent in the Language tab of the agent settings.

Useful Links: Languages Reference, Multi-language Agents Reference

Message object additional parameters

message.dialogflow

Since version 2 Dialogflow will return a google.cloud.dialogflow_v2.types.DetectIntentResponse object when the API is called. This is a special object isn’t iterable and subscriptable, so you will have to access each attribute by using dot notation like message.dialogflow.query_text to get the text that you just sent to dialogflow.

This object allows you to access any information from the matched intent including the queried text, action, reply, intent, confidence and language.

from opsdroid.skill import Skill
from opsdroid.matchers import match_dialogflow_action

class MySkill(Skill):
  @match_dialogflow_action('smalltalk.greetings')
  async def dump_response(self, message):
      print(message.dialogflow)

This example skill will print the following on the message “what’s up?”.

query_text: "what is up"
action: "smalltalk.greetings.whatsup"
parameters {
}
all_required_params_present: true
fulfillment_text: "Not much. What\'s new with you?"
fulfillment_messages {
  text {
    text: "Not much. What\'s new with you?"
  }
}
intent {
}
intent_detection_confidence: 1.0
language_code: "en"

If you want to access the text reply from dialogflow you would have to do it like this message.dialogflow.fulfillment_text which would give you Not much. What\'s new with you?

You can build your bot to get different replies and access different things depending on your actions and intents - this is the example of what you would get if you used the contact info action and asked opsdroid for the contact info:

query_text: "contact info"
action: "support.contacts"
parameters {
}
all_required_params_present: true
fulfillment_text: "Email us at support@example.com for any questions regarding our products and services."
fulfillment_messages {
text {
  text: "Email us at support@example.com for any questions regarding our products and services."
}
platform: SLACK
}
fulfillment_messages {
text {
  text: "Email us at support@example.com for any questions regarding our products and services."
}
}
intent {
name: "projects/test-ddd33/agent/intents/1bcf91d7-acdb-4583-abca-927d6efb0730"
display_name: "Contact us"
}
intent_detection_confidence: 1.0
language_code: "en"