Search code examples
phpdialogflow-es

DialogFlow - How to Batch Update Intents via API?


I'm looking for a basic example on how to utilize DialogFlow's batchUpdate, and how to utilize batchUpdateResponse to show an actual response once complete.

Have found no examples for DialogFlow V1 or V2 (at this point either would be helpful), the below is all I've managed to setup - looking for the missing arguments to be added:

$intentsClient->batchUpdateIntents($formattedParent, $languageCode, $test_3);

Currently using PHP https://github.com/googleapis/google-cloud-php/tree/83ae284c025f6e93b9ce835b987932c425b5a9de/Dialogflow but any language is fine here.


Solution

  • Ended up figuring this out through the use of https://developers.google.com/apis-explorer/ and the Google Client Library for PHP (https://github.com/googleapis/google-api-php-client).

    Below is a basic example for updating the text on two intents at once, via PHP. Hopefully this helps someone in the future, am somewhat surprised at the general lack of helpful documentation and/or examples for using DialogFlow's API V2 (or even V1 for that matter). So many awesome things can be done by using this rather than their Dashboard to train your bot!

    // Global variable pointing to the .json file downloaded with private key from DialogFlow
    putenv('GOOGLE_APPLICATION_CREDENTIALS=directory-of-file/google-service-acount-key.json');
    
    // Setup Google Client
    require __DIR__.'/vendor/autoload.php';
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');
    $httpClient = $client->authorize();
    
    // Setup array to update intent (minified)
    $update_intent = array('intentBatchInline'=>array('intents'=>array(
    0=>array('name'=>'projects/YOUR-PROJECT-NAME/agent/intents/FIRST-INTENT-ID','displayName'=>'FIRST-INTENT-NAME','messages'=>array(0=>array('text'=>array('text'=>array(0=>'FIRST-INTENT-TEXT-TO-UPDATE',),),),),),
    1=>array('name'=>'projects/YOUR-PROJECT-NAME/agent/intents/SECOND-INTENT-ID','displayName'=>'SECOND-INTENT-NAME','messages'=>array(0=>array('text'=>array('text'=>array(0=>'SECOND-INTENT-TEXT-TO-UPDATE',),),),),),),),
    );
    
    // Post to DialogFlow API
    $response = $httpClient->post('https://dialogflow.googleapis.com/v2/projects/PROJECT-NAME-HERE/agent/intents:batchUpdate', [
        GuzzleHttp\RequestOptions::JSON => $test_batch_intent_1
    ]);
    
    // Print out response for troubleshooting
    print_r($response->getBody()->getContents());
    echo "<br /><br />Here's to getting past DialogFlow API's hurdles! :)";
    exit;