如何在Dialogflow中更新会话实体?

时间:2019-01-19 01:36:48

标签: dialogflow

我在Dialogflow代理中定义了一个名为visible_objects的实体。

在Dialogflow控制台中,我放置了一个名为placeholder2的条目,并带有一个同义词占位符。

enter image description here

当用户开始与代理进行交互时,我正在通过projects.agent.sessions.entity补丁请求来更新此实体。以下是我发送的请求的内容。

`{
    "name": "projects\/{PROJECT_NAME}\/agent\/sessions\/{SESSION}\/entityTypes\/visible_objects",
    "entityOverrideMode": "ENTITY_OVERRIDE_MODE_OVERRIDE",
    "entities": [
        {
            "value": "door",
            "synonyms": [
                "door"
            ]
        },
        {
            "value": "desk",
            "synonyms": [
                "desk"
            ]
        },
        {
            "value": "test tubes",
            "synonyms": [
                "test tubes",
                "test",
                "tubes"
            ]
        }
    ]
}`

这是我用来生成它的代码:

<?php

$json = file_get_contents('php://input');
$request = json_decode($json, true);
$df_action = $request['queryResult']['action'];
$df_contexts = $request['queryResult']['outputContexts'];
$df_parameters = $request['queryResult']['parameters'];

// taken from private_key in my GCP authentication .json file
$CLIENT_ACCESS_TOKEN = "-----BEGIN PRIVATE KEY-----xxxxxxxxxxxxxx---END PRIVATE KEY-----\n";

// loop through the visible objects and prepare the output for dialogflow
foreach($_SESSION['STORY']['VISIBLE_OBJECTS'] as $visible_object)
{
    unset($synonyms);       // clear any existing synonyms
    $synonyms = array();
    $synonyms[] = $visible_object['name'];
    if (strpos($visible_object['name'], ' ', 1))
    {       // multiple words so split it as synonyms
        $sub_objects = explode(' ', $visible_object['name']);
        foreach($sub_objects as $sub_object)
            $synonyms[] = $sub_object;
    }

    // add the object to the user entity
    $user_ent_objects[] = array('value'=>$visible_object['name'], 'synonyms'=>$synonyms);
}

// prepare user_entity
$user_entity = array('name'=>$request['session'] . '/entityTypes/visible_objects', 'entityOverrideMode'=>'ENTITY_OVERRIDE_MODE_OVERRIDE', 'entities'=>$user_ent_objects);

// log for debugging
file_put_contents($SITE_DIR . '/webhook/user-entity-visible-objects.txt',  json_encode($user_entity,JSON_PRETTY_PRINT));

$ch = curl_init('https://dialogflow.googleapis.com/v2/' . $request['session'] . '/entityTypes/visible_objects');

curl_setopt($ch, CURLOPT_POST, true);        // tell it we're posting
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $CLIENT_ACCESS_TOKEN));      // set the headers
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($user_entity));     // load our data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);     // execute it

?>

我的意图是“看对象名称”,其中对象名称映射到visible_objects实体。这是我看到的一些示例短语:

  • 查看占位符->检测到“占位符”为对象名称
  • 查看测试->未检测到对象名称
  • 看管->未检测到对象名称
  • 在桌子上看->未检测到对象名称

基于这些响应,由于未检测到新值,并且未覆盖旧值占位符2,因此会话实体未更新。

我的webhook是用PHP编写的,并且我没有使用Google SDK(它仅适用于PHP的Alpha版)。我正在捕获request curl_exec的响应,但未返回任何内容。

使用curl_getinfo我看到以下内容:

`[url] => https://dialogflow.googleapis.com/v2/projects/escaperoom-447aa/agent/sessions/SESSION_ID_MASKED/entityTypes/visible_objects
[content_type] => text/html; charset=UTF-8
[http_code] => 400
[header_size] => 144
[request_size] => 2002
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.072034
[namelookup_time] => 0.023728
[connect_time] => 0.031063
[pretransfer_time] => 0.065598
[size_upload] => 394
[size_download] => 0
[speed_download] => 0
[speed_upload] => 5472
[download_content_length] => 1555
[upload_content_length] => 394
[starttransfer_time] => 0.065659
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 216.58.192.234
[certinfo] => Array
    (
    )

[primary_port] => 443
[local_ip] => 66.198.240.46
[local_port] => 41442

`

有人在这里看到我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

问题是您没有将$CLIENT_ACCESS_TOKEN设置为访问令牌。您正在将其设置为应与服务帐户关联的私钥。

您将需要使用私钥访问generate an access token-它不是访问令牌本身。由于存在Dialogflow服务的服务定义,因此您可以选择仅generate a signed JWT for the service