Day29-介接 API(四)Laravel 实作 Dialogflow ES 之 API interactions

大家好~
今天来实作下图架构的简单版吧!

申请服务帐户


选择一个 Dialogflow 的 Agent,
先点击齿轮,
再点击 Project ID
会导向到 Google Cloud Platform

点击建立凭证。
建立一个服务帐户。

输入服务帐户名称。

选择角色 Dialogflow 服务代理人

点击完成建立服务帐户。

建立金钥

点击刚刚建立的服务帐号。

  1. 点击金钥
  2. 点击新增金钥
  3. 点击建立新的金钥

选择 JSON
点击建立下载金钥。

实作

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\SessionsClient;
public function test(Request $request)
{
    $textInput = new TextInput();
    $textInput->setLanguageCode('zh-TW')
        ->setText($request->text);

    $queryInput = new QueryInput();
    $queryInput->setText($textInput);

    $sessionsClient = new SessionsClient([
        'credentials' => env('GOOGLE_APPLICATION_CREDENTIALS')
    ]);

    $session = $sessionsClient::sessionName(
        env('DIALOGFLOW_PROJECT_ID'), uniqid()
    );

    $detectIntentResponse = $sessionsClient->detectIntent($session, $queryInput);
    $queryResult = $detectIntentResponse->getQueryResult();
    $fulfillmentText = $queryResult->getFulfillmentText();
    return $fulfillmentText;
}

env 介绍:

  • env('GOOGLE_APPLICATION_CREDENTIALS')
    • 服务帐户的 JSON 金钥档案路径。
  • env('DIALOGFLOW_PROJECT_ID')
    • Dialogflow 的 Project ID

测试环节

就用上次 Day27 做的 Intent 做测试吧!

#1

#2

#3

成功~

那麽今天先这样啦~
若文章有任何问题,
还请大家不吝赐教!
大家明天见!

参考资料:


<<:  大共享时代系列_028_云端串流游戏 ( Cloud Gaming )

>>:  来画一个视力检查表吧!

Day18. Blue Prism制造齿轮 -BP Object页 将成绩写入Excel中

以前年轻时,常说我只是一颗小小的螺丝钉没什麽用处, 接着就会听到家中长一辈的人正向鼓励着说:「每一个...

Day5 SR替代实境 让我们可以空手操纵物件的技术

简略介绍完VR、AR、MR後,把接下要介绍的是SR。 SR(替代实境):是一种裸眼3D,是一种把虚拟...

[NestJS 带你飞!] DAY15 - Dynamic Module

前面有介绍过 Module 的一些基本使用方式,然而有一项非常强大的功能没有被提及,就是 动态模组(...

Day 2 - A short introduction to gcc usage - 1

In this part, I will compile a simple program call...