Dialogflow API, support for versions and environments

Even if currently it’s a beta feature, you can create multiple versions of your agent and publish them to separate environments.

The problem is that the REST URI bindings do not appear to have been updated in the API definition for this feature.

I wrote an issue in the cloud-php Github project, and John Pedrie, working for the project, asked me to try the gRPC way.

Never did it before, so here’s a guide to help you just in case you are in my situation…hopefully it will be integrated soon!

Assuming you are working with PHP7.3 (and a Debian-based Linux server):

Based on the Install gRPC for PHP doc, on our server (running Ubuntu server 19.10, PHP7.3) I had to install some packages:

sudo apt-get install autoconf libz-dev php-dev php-pear
sudo pecl install grpc
sudo pecl install protobuf

Let’s modify the /etc/php/7.3/fpm/php.ini file adding the following lines:

extension=grpc.so
extension=protobuf.so

Restarting php7.3-fpm:

sudo systemctl restart php7.3-fpm.service

And adding the "grpc/grpc": "^v1.1.0" line to the project composer.json.

Now it’s time to change the code. The usual way is something like:

$sessionClient = new SessionsClient();
$session = $sessionClient->sessionName($projectId, $sessionId);
$response = $sessionsClient->detectIntent($session, $queryInput);

And it has to be changed with something like:

$session = $this->_getSessionName($sessionId);
$sessionsClient = new SessionsClient();
$response = $sessionsClient->detectIntent($session, $queryInput);

// WHERE _getSessionName() is:
private function _getSessionName($sessionId)
{
    $projectId = $this->_getProjectId();
    $environment = $this->_getEnvironment();
    return "projects/{$projectId}/agent/environments/{$environment}/users/-/sessions/{$sessionId}";
}

And that’s it.

In the Dialogflow History tab there is still no way to filter conversations by Environment, but there is an Environment indicator in the conversation Detail.

Dialogflow environemnts

Leave a Comment

Your email address will not be published. Required fields are marked *