Last active
December 19, 2018 00:30
-
-
Save zaoldyeck/e92797f36836199dae8945036066f6a4 to your computer and use it in GitHub Desktop.
This file contains 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 config = require('../../config') | |
const axios = require('axios') | |
const md5 = require('md5') | |
const KKBOX = require('../api/KKBOX') | |
class Olami { | |
constructor(appKey = config.olami.appKey, appSecret = config.olami.appSectet, inputType = 1) { | |
this.URL = 'https://tw.olami.ai/cloudservice/api' | |
this.appKey = appKey | |
this.appSecret = appSecret | |
this.inputType = inputType | |
} | |
nli(text, cusid = null) { | |
const timestamp = Date.now() | |
return axios.post(this.URL, {}, { | |
params: { | |
appkey: this.appKey, | |
api: 'nli', | |
timestamp: timestamp, | |
sign: md5(`${this.appSecret}api=nliappkey=${this.appKey}timestamp=${timestamp}${this.appSecret}`), | |
cusid: cusid, | |
rq: JSON.stringify({'data_type': 'stt', 'data': {'input_type': this.inputType, 'text': text}}) | |
} | |
}).then(response => { | |
const nli = response.data.data.nli[0]; | |
return this._intentDetection(nli) | |
}) | |
} | |
_intentDetection(nli) { | |
const type = nli.type | |
const desc = nli.desc_obj | |
const data = nli.data_obj | |
const semantic = nli.semantic | |
function handleSelectionType(desc) { | |
const descType = desc.type | |
switch (descType) { | |
case 'news': | |
return desc.result + '\n\n' + data.map((el, index) => index + 1 + '. ' + el.title).join('\n') | |
case 'poem': | |
return desc.result + '\n\n' + data.map((el, index) => index + 1 + '. ' + el.poem_name).join('\n') | |
case 'cooking': | |
return desc.result + '\n\n' + data.map((el, index) => index + 1 + '. ' + el.name).join('\n') | |
default: | |
return '對不起,你說的我還不懂,能換個說法嗎?' | |
} | |
} | |
async function handleMusicKKBOXType(semantic) { | |
function getKeyWord(semantic, dataType) { | |
function getSlotValueByName(slotName) { | |
return semantic.slots.filter(slot => slot.name === slotName)[0].value | |
} | |
switch (dataType) { | |
case 'artist': | |
return getSlotValueByName('artist_name') | |
case 'album': | |
return getSlotValueByName('album_name') | |
case 'track': | |
return getSlotValueByName('track_name') | |
case 'playlist': | |
return getSlotValueByName('keyword') | |
} | |
} | |
const dataType = semantic.modifier[0].split('_')[2] | |
const keyWord = getKeyWord(semantic, dataType) | |
const api = await KKBOX.init() | |
const data = await api | |
.searchFetcher | |
.setSearchCriteria(keyWord, dataType) | |
.fetchSearchResult() | |
.then(response => { | |
return response.data[dataType + 's'].data | |
}) | |
if (dataType === 'artist') { | |
return data[0].url | |
} else { | |
const id = data[0].id | |
return `https://widget.kkbox.com/v1/?id=${id}&type=${dataType === 'track' ? 'song' : dataType}` | |
} | |
} | |
switch (type) { | |
case 'kkbox': | |
return (data.length > 0) ? data[0].url : desc.result | |
case 'baike': | |
return data[0].description | |
case 'news': | |
return data[0].detail | |
case 'joke': | |
return data[0].content | |
case 'cooking': | |
return data[0].content | |
case 'selection': | |
return handleSelectionType(desc) | |
case 'ds': | |
return desc.result + '\n請用 /help 指令看看我能怎麼幫助您' | |
case 'music_kkbox': | |
return handleMusicKKBOXType(semantic[0]) | |
default: | |
return desc.result | |
} | |
} | |
} | |
module.exports = new Olami() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment