Amazon Lex postContent 错误 "Failed to decode Session attributes."

Amazon Lex postContent error "Failed to decode Session attributes."

我正在尝试使用 PHP 的 AWS SDK 中的 postContent 将我的网页连接到我的 Lex 机器人。

我设置了凭据和参数,然后尝试发布内容。这是相关代码:

$credentials = new \Aws\Credentials\Credentials('XXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXX');

$args = array(
    'region' => 'us-east-1',
    'version' => 'latest',
    'debug' => true,
    'credentials' => $credentials
);

$lex_client = new Aws\LexRuntimeService\LexRuntimeServiceClient($args);

$lex_response = $lex_client->postContent([
    'accept' => 'text/plain; charset=utf-8',
    'botAlias' => 'XXXX',
    'botName' => 'XXXX',
    'contentType' => 'text/plain; charset=utf-8',
    'inputStream' => $userInput,
    'requestAttributes' => "{}",
    'sessionAttributes' => "{}",
    'userId' => 'XXXXXXXXXXXX',
]);

此错误为:

'Error executing "PostContent" on "https://runtime.lex.us-east-1.amazonaws.com/bot/XXXX/alias/XXXX/user/XXXXXXXXXX/content";

AWS HTTP error: Client error: POST https://runtime.lex.us-east-1.amazonaws.com/bot/XXXX/alias/XXXX/user/XXXXXXXXXX/content resulted in a 400 Bad Request response: {"message":"Invalid Request: Failed to decode Session attributes. Session Attributes should be a Base64 encoded json map of String to String"}' (length=142)

我尝试在 sessionAttributes 中使用各种 JSON 字符串、JSON 编码字符串和 Base64 编码字符串,但我仍然遇到同样的错误。

AWS SDK 中的 LexRuntimeService 自动 JSON 编码 Base64 编码 postContent 数组。通过向它传递一个 JSON 字符串,SDK 中的 json 编码将在 {} 周围放置双引号,使其成为 "{}" 并导致错误。

所以只需将 sessionAttributesrequestAttributes 作为 PHP 数组传递即可。

$lex_response = $lex_client->postContent([
    'accept' => 'text/plain; charset=utf-8',
    'botAlias' => 'XXXX',
    'botName' => 'XXXX',
    'contentType' => 'text/plain; charset=utf-8',
    'inputStream' => $userInput,
    'requestAttributes' => array(),
    'sessionAttributes' => array(),            // <---- PHP Array not JSON
    'userId' => 'XXXXXXXXXXXX',
]);