Writing a new intent type¶
The Dialogflow extension comes with a set of basic intent types and associated fulfilments. It is also provides a framework to write new intent types.
If you have a generic intent, we would love to add it to the Dialogflow extension. If it is more specific, it probably makes more sense to put it in your own extension. Following this approach, our aim is to meet the most common needs through a set of easy to configure intents in a single extension, and to allow people to write their own extensions that extend Dialogflow for very specific needs.
Architecture of an intent type¶
An intent typically consists of three classes performing specific functions:
- define the form used to configure the intent
- send a request to Dialogflow to register the intent
- fulfil request received from Dialogflow
Define the configuration form¶
This class defines a form that will be used to configure intents. You'll also write your validations there.
Set up intent¶
This class takes care of creating and updating your intents in Dialogflow.
Fulfilment¶
This class is responsible for fulfilling the intent when a request is received from Dialogflow.
Registering an intent type¶
Once you've defined this classes, they need to be registered. To do so, you can use the DialogflowIntentType
API:
<?php // Example: Register your intent handler
civicrm_api3('DialogflowIntentType', 'create', array(
'name' => 'subscribe',
'display_name' => 'Subscribe to a newsletter',
'intent_class_name' => 'CRM_Dialogflow_IntentType_Subscribe_Remote',
'webhook_class_name' => 'CRM_Dialogflow_IntentType_Subscribe_Webhook',
'form_class_name' => 'CRM_Dialogflow_IntentType_Subscribe_Form',
));
Note that the name
assigned here must be unique, as it will be used to identify the intent type when calling DialogflowIntent.