Created
December 31, 2021 19:56
-
-
Save kun432/08ec5eacfc19585f43b5fd90458daddc to your computer and use it in GitHub Desktop.
Voiceflow Dialog Management APIサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require("axios"); | |
| const { cli } = require("cli-ux"); | |
| const API_KEY = "YOUR_API_KEY_HERE"; | |
| const VERSION_ID = "YOUR_VERSION_ID_HERE"; | |
| async function interact(userID, request) { | |
| console.log("..."); | |
| const response = await axios({ | |
| method: "POST", | |
| url: `https://general-runtime.voiceflow.com/state/${VERSION_ID}/user/${userID}/interact`, | |
| headers: { Authorization: API_KEY }, | |
| data: { request }, | |
| }); | |
| for (const trace of response.data) { | |
| switch (trace.type) { | |
| case "text": | |
| case "speak": { | |
| console.log(trace.payload.message); | |
| break; | |
| } | |
| case "end": { | |
| // an end trace means the the Voiceflow dialog has ended | |
| return false; | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| async function main() { | |
| console.log("セッションを開始します。"); | |
| const userID = await cli.prompt("> ユーザーIDを入力してください。"); | |
| let isRunning = await interact(userID, { type: "launch" }); | |
| while (isRunning) { | |
| const nextInput = await cli.prompt("> 入力"); | |
| isRunning = await interact(userID, { type: "text", payload: nextInput }); | |
| } | |
| console.log("セッションを終了します。"); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment