Skip to content

Instantly share code, notes, and snippets.

@kun432
Created December 31, 2021 19:56
Show Gist options
  • Save kun432/08ec5eacfc19585f43b5fd90458daddc to your computer and use it in GitHub Desktop.
Save kun432/08ec5eacfc19585f43b5fd90458daddc to your computer and use it in GitHub Desktop.
Voiceflow Dialog Management APIサンプル
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