LoginSignup
0
0

More than 5 years have passed since last update.

DialogflowAPIでIntent作成時に、intentのIDを取得する方法

Last updated at Posted at 2018-06-27

概要

Dialogflow V2 API使用時に、Intentを作成したのですが、戻り値にはIDは含まれていません。
そうすると、更新や削除のときに困ってしまい、APIとして機能しないので、IDの取得法の備忘録として書きました。

本題

以下、TypeScriptで書いてます。
accessTokenはすでに取得している前提で始めます。

index.ts
import axios from 'axios'

async function main() {

    const URL = 'https://dialogflow.googleapis.com/v2'
    const projectId = 'test'
    const accessToken = 'hogehoge'

    let http = axios.create({
        baseURL: `${URL}/projects/${projectId}/agent/intents`,
        // timeout: 1000,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${accessToken}`,
        },
    })

    const { data } = await http.post('', {
        displayName: 'Greeting',
        webhookState: 'WEBHOOK_STATE_UNSPECIFIED',
        priority: 5000000,
        isFallback: false,
        mlDisabled: false,
        trainingPhrases: [
            {
                'type': 'EXAMPLE',
                'parts': [
                    {
                        'text': 'Hello!',
                    },
                ],
            },
        ],
        'action': 'greeting',
        'messages': [
            {
                'text': {
                    'text': [
                        'Hello!',
                    ],
                },
            },
        ],
    },
    )

    const intentId = data.name.split('/').pop()
}

main()

main関数の最終行に注目してください。

const intentId = data.name.split('/').pop()

Dialogflow V2 APIの戻り値は以下のようになっています。

{
  "name": "projects/test/agent/intents/15933e2c-63f4-45c9-87c2-f69a4a19f71a",
  "displayName": "Greeting-again",
  "priority": 500000,
  "action": "greeting",
  "messages": [
    {
      "text": {
        "text": [
          "Hello!"
        ]
      }
    }
  ]
}

上記のnameの中のintents以降がintentのIDです。

そのため、/splitして、最後のものをpopしています。

APIのリファレンスを読まないと、IDが返ってくるものと勘違いしてしまいがちですが、返ってこないんですね。
思い込みでやったら危険ということを改めて認識しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0