SKํ ๋ ์ฝค AI ์คํผ์ปค NUGU์ play๋ฅผ ๋ง๋๋ ๊ณผ์ ์์ ์ง์ ์ ์ํ ํ๋ผ๋ฏธํฐ์ Backend proxy API Reference์์ ์ ๊ณตํ๋ ๊ท๊ฒฉ์ ๋ง๊ฒ HTTP Request/Response์ JSON ํ์ฑ๊ณผ ์๋ต์ ์์ฝ๊ฒ ํ๋๋ก ๋์์ค๋๋ค.
์๋์ ๋ช ๋ น์ด๋ฅผ ํตํด nugu-kit๋ฅผ ์ค์นํฉ๋๋ค.
npm install nugu-kit
{
"version": "2.0",
"action": {
"actionName": "FoodFighter",
"parameters": {
"food": { "type": "korean", "value": "๊น์น" },
"price": { "type": "won", "value": "1000" }
}
},
"event": {
"token": "select_token",
"type": "AudioPlayer.PlaybackStarted",
"offset_in_milli_seconds": 2517
},
"context": {
"session": {
"accessToken": "abc123",
"id": "def456",
"isNew": true,
"isPlayBuilderRequest": false
},
"device": {
"type": "speaker.nugu.nu200",
"state": null
},
"supportedInterfaces": {
"AudioPlayer": {
"playerActivity": "PLAYING",
"token": "select_token",
"offsetInMilliseconds": 100000
}
}
}
}
const Nugu = require('nugu-kit');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/answer.food',(req,res)=>{
const nugu = new Nugu(req);
// apikey
console.log(nugu.authorization) // TEST-API-KEY
// version
console.log(nugu.version); // 2.0
// action
console.log(nugu.actionName); // FoodFighter
console.log(nugu.parameters); // { 'food': { type: 'korean', value: '๊น์น' }, 'price':{ type: 'won', value: '1000'} }
// event
console.log(nugu.event); // { token: 'select_token', type: 'AudioPlayer.PlaybackStarted', offset_in_milli_seconds: 2517 }}
// session
console.log(nugu.accessToken); // abc123
console.log(nugu.sessionId); // def456
console.log(nugu.isNew); // true
console.log(nugu.isPlayBuilderRequest); // false
// device
console.log(nugu.deviceType); // speaker.nugu.nu200
console.log(nugu.deviceState); // null
// supportedInterfaces
console.log(nugu.audioPlayer) // { 'playerActivity': 'PLAYING', 'token': 'select_token', 'offsetInMilliseconds': 100000 }
console.log(nugu.audioPlayerActivity); // PLAYING
console.log(nugu.audioToken); // select_token
console.log(nugu.audioOffset); // 100000
// response
console.log(nugu.response); // { version: '2.0', resultCode: 'OK', output: {}, directives: [] }
})
console.log(nugu.parameters); // { 'food': { type: 'korean', value: '๊น์น' }, 'price':{ type: 'won', value: '1000'} }
const food = nugu.getValue('food'); // ๊น์น
const price = nugu.getValue('price'); // 1000
const foodType = nugu.getValueType('food'); // korean
const priceType = nugu.getValueType('price'); // won
app.post('/answer.food',(req,res)=>{
const nugu = new Nugu(req);
const food = nugu.getValue('food');
const price = nugu.getValue('price');
const prompt = `${food}์ ๊ฐ๊ฒฉ์ ${price}์ ์ด์์`;
nugu.output = {'ment':prompt};
return res.json(nugu.response);
})
{
"version": "2.0",
"resultCode": "OK",
"output": {
"ment": "๊น์น์ ๊ฐ๊ฒฉ์ 1000์ ์ด์์."
},
"directives": []
}
app.post('/answer.food',(req,res)=>{
const nugu = new Nugu(req);
const food = nugu.getValue('food');
const price = parseInt(nugu.getValue('price'));
const prompt = `${food}์ ๊ฐ๊ฒฉ์ ${price}์ ์ด์์`;
// ์์ธ ์๋ต
if(price > 700){
nugu.resultCode = 'priceExceed';
return res.json(nugu.response);
}
nugu.output = {'ment':prompt};
return res.json(nugu.response);
})
{
"version": "2.0",
"resultCode": "priceExceed",
"output": {},
"directives": []
}
app.post('/answer.food',(req,res)=>{
const nugu = new Nugu(req);
const food = nugu.getValue("food");
const price = nugu.getValue("price");
const prompt = `${food}์ ๊ฐ๊ฒฉ์ ${price}์ ์ด์์`;
// default directive ์ถ๊ฐ
nugu.addDirective();
nugu.directiveType = 'AudioPlayer.Stop';
nugu.directiveUrl = 'https://www.food.kr/stop.mp3';
nugu.directiveOffset = 10000;
nugu.directiveDelay = 20000;
nugu.directiveInterval = 30000;
nugu.directiveToken = 'search_token';
nugu.directivePreviousToken = 'select_token';
nugu.output = {'ment':prompt};
return res.json(nugu.response);
})
{
"version": "2.0",
"resultCode": "OK",
"output": {
"ment": "๊น์น์ ๊ฐ๊ฒฉ์ 1000์ ์ด์์"
},
"directives": [
{
"type": "AudioPlayer.Stop",
"audioItem": {
"stream": {
"url": "https://www.food.kr/food.mp3",
"offsetInMilliseconds": 10000,
"progressReport": {
"progressReportDelayInMilliseconds": 20000,
"progressReportIntervalInMilliseconds": 30000
},
"token": "search_token",
"expectedPreviousToken": "select_token"
},
"metadata": {}
}
}
]
}
nugu.addDirective();
console.log(nugu.response.directives);
/*
{
"type":"AudioPlayer.Play",
"audioItem":{
"stream":{
"url": "",
"offsetInMilliseconds":0,
"progressReport":{
"progressReportDelayInMilliseconds": 0,
"progressReportIntervalInMilliseconds": 0
},
"token":"",
"expectedPreviousToken":""
},
"metadata":{}
}
}
*/
Copyright (c) 2020 REVOICE:
MIT ๋ผ์ด์ผ์ค, ์ธ๋ถ์ฌํญ