Skip to content

revoiceStudio/nugu-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

32 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Nugu Kit for Node.js

SKํ…”๋ ˆ์ฝค AI ์Šคํ”ผ์ปค NUGU์˜ play๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์ง์ ‘ ์ •์˜ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ Backend proxy API Reference์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ทœ๊ฒฉ์— ๋งž๊ฒŒ HTTP Request/Response์˜ JSON ํŒŒ์‹ฑ๊ณผ ์‘๋‹ต์„ ์†์‰ฝ๊ฒŒ ํ•˜๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.

์„ค์น˜

์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด nugu-kit๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

npm install nugu-kit

์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์˜ˆ์‹œ

Request sample

{
    "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
            }
        }
    }
}

Request ํŒŒ์‹ฑ

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: [] }

})

ํŠน์ • Utterance parameter ์–ป๊ธฐ

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

Response

๊ธฐ๋ณธ ์‘๋‹ต

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);
})

๊ธฐ๋ณธ ์‘๋‹ต output

{
    "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);
})

์˜ˆ์™ธ ์ƒํ™ฉ ์‘๋‹ต output

{
    "version": "2.0",
    "resultCode": "priceExceed",
    "output": {},
    "directives": []
}

AudioPlayer Interface๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‘๋‹ต

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);
})

AudioPlayer Interface๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‘๋‹ต output

{
  "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": {}
      }
    }
  ]
}

Default directive

nugu.addDirective();
console.log(nugu.response.directives);
/*   
    {
        "type":"AudioPlayer.Play",
        "audioItem":{
            "stream":{
                "url": "",
                "offsetInMilliseconds":0,
                "progressReport":{
                    "progressReportDelayInMilliseconds": 0,
                    "progressReportIntervalInMilliseconds": 0
                },
                "token":"",
                "expectedPreviousToken":""
            },
            "metadata":{}
        }
    }
*/

์ฐธ๊ณ 

play builder document

์ด์Šˆ

github

๋ผ์ด์„ผ์Šค

Copyright (c) 2020 REVOICE:
MIT ๋ผ์ด์„ผ์Šค, ์„ธ๋ถ€์‚ฌํ•ญ

Releases

No releases published

Packages

No packages published