Dialogflow:如何从Symfony控制器调用detectIntent?

时间:2019-07-14 11:13:17

标签: php symfony dialogflow

我正在尝试从我的Symfony控制器调用detectIntent方法,并且得到ERR_EMPTY_RESPONSE。

如果我创建一个PHP文件并从控制台运行该文件,则相同的代码可以正常运行。在那种情况下,我可以毫无问题地完成代理商的履行。

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;

class MainController extends AbstractController
{
    /**
     * @Route("/main", name="main")
     */
    public function index()
    {

        $this->detect_intent_texts('project_id', ["hello"], "session");

        die("STOP");

        return $this->render('main/index.html.twig', [
            'controller_name' => 'MainController',
        ]);
    }

    /**
     * Returns the result of detect intent with texts as inputs.
     * Using the same `session_id` between requests allows continuation
     * of the conversation.
     */
    function detect_intent_texts($projectId, $texts, $sessionId, $languageCode = 'en-US')
    {
        // new session
        $path_to_json = __DIR__.'/../../config/api-project.json';
        $test = array('credentials' => $path_to_json);
        $sessionsClient = new SessionsClient($test);

        // query for each string in array
        foreach ($texts as $text) {

            // create text input
            $textInput = new TextInput();
            $textInput->setText($text);
            $textInput->setLanguageCode($languageCode);

            // get response and relevant info
            try {
                $session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());

                // create query input
                $queryInput = new QueryInput();
                $queryInput->setText($textInput);

                $response = $sessionsClient->detectIntent($session, $queryInput);
                die();
            } catch(ApiException $error) {
                die("ERROR!");
            } finally {
                $sessionsClient->close();
            }

            $queryResult = $response->getQueryResult();
            $queryText = $queryResult->getQueryText();
            $intent = $queryResult->getIntent();
            $displayName = $intent->getDisplayName();
            $confidence = $queryResult->getIntentDetectionConfidence();
            $fulfilmentText = $queryResult->getFulfillmentText();

            // output relevant info
            print(str_repeat("=", 20) . PHP_EOL);
            printf('Query text: %s' . PHP_EOL, $queryText);
            printf('Detected intent: %s (confidence: %f)' . PHP_EOL, $displayName,
                $confidence);
            print(PHP_EOL);
            printf('Fulfilment text: %s' . PHP_EOL, $fulfilmentText);
        }

    }
}

我想直接在我的控制器中访问“实现”文本,以便稍后使用该答案。 谢谢!

0 个答案:

没有答案