技能名稱:歷史上的今天html
入口詞:打開歷史上的今天json
語音交互:(有些是先寫上)session
{ "intents": [ { "intent": "PAUSE_HISTORY_DAY", "slots": [], "user_says": [ "暫停", "停下" ] }, { "intent": "EXIT_HISTORY_DAY", "slots": [], "user_says": [ "我不想聽了", "我不想聽", "我聽夠了", "拜拜", "取消" ] }, { "intent": "HISTORY_DAY", "slots": [ { "name": "month", "type": "ROKID.NUMBER_ZH" }, { "name": "day", "type": "ROKID.NUMBER_ZH" }, { "name": "oneday", "type": "ROKID.DAY_ZH" } ], "user_says": [ "我要了解$month月$day號", "歷史上的$month月$day日", "我想聽$month月$day號", "我想聽$month月$day日", "我想了解$oneday", "我要了解$month月$day日", "$month月$day日", "$oneday", "我要了解$oneday", "歷史上的$month月$day號", "$month月$day號" ] }, { "intent": "RESUME_HISTORY_DAY", "slots": [], "user_says": [ "繼續播放", "繼續" ] } ] }
配置採用JS Engine:--增長暫停,繼續播放功能app
exports.handler = function(event, context, callback) { var rokid = Rokid.handler(event, context,callback); rokid.registerHandlers(handlers); rokid.execute(); }; dictText = {'一':'01','二':'02','三':'03','四':'04','五':'05','六':'06','七':'07','八':'08','九':'09','十':'10','十一':'11','十二':'12','十三':'13','十四':'14','十五':'15','十六':'16','十七':'17','十八':'18','十九':'19','二十':'20','二十一':'21','二十二':'22','二十三':'23','二十四':'24','二十五':'25','二十六':'26','二十七':'27','二十八':'28','二十九':'29','三十':'30','三十一':'31'}; onedaydictText = {'今天':0,'明天':1,'後天':2,'大後天':3,'大大後天':4,'大大大後天':5,'昨天':-1,'前天':-2,'大前天':-3,'大大前天':-4,'大大大前天':-5}; //若是是6月,返回06 function formatMonth(monthori){ if (!isNaN(monthori)) { if (parseInt(monthori) < 10) { monthurl = '0' + monthori; } else { monthurl = monthori; } } else { monthurl = dictText[monthori]; } return monthurl; } //若是是8號,返回08 function formatDay(dayori){ if (!isNaN(dayori)) { if (parseInt(dayori) < 10) { dayurl = '0' + dayori; } else { dayurl = dayori; } } else { dayurl = dictText[dayori]; } return dayurl; } var handlers = { "ROKID.INTENT.WELCOME":function(){ try{ this.emit(":tts",{ tts: "歡迎使用歷史上的今天這個技能!你能夠對我說想了解幾月幾號。" }); this.callback(null); }catch(e){ this.callback(e); } }, "PAUSE_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "",action:"PAUSE" }); this.callback(null); }catch(e){ this.callback(e); } }, "RESUME_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "",action:"RESUME" }); this.callback(null); }catch(e){ this.callback(e); } }, "HISTORY_DAY":function(){ try{ var oneday = Rokid.param.request.content.slots.oneday; var monthori = Rokid.param.request.content.slots.month; var dayori = Rokid.param.request.content.slots.day; var monthurl = ''; var dayurl = ''; if (oneday){ var dd = new Date(); dd.setDate(dd.getDate() + onedaydictText[oneday]); monthori = dd.getMonth()+1; dayori = dd.getDate(); monthurl = formatMonth(monthori); dayurl = formatDay(dayori); } else{ monthurl = formatMonth(monthori); dayurl = formatDay(dayori); } result = Rokid.sync_request('GET','https://baike.baidu.com/cms/home/eventsOnHistory/' + monthurl + '.json'); result = Rokid.resHandler(result); var hdori = result[monthurl][monthurl+dayurl]; var hdorilen = hdori.length; var res = '好的,爲您播報' + monthori + '月' + dayori + '號' + '的歷史。'; for (var index = hdorilen-1; index >= 0; index --){ var yearori = hdori[index].year; var yearstr = ''; if (parseInt(yearori) >= 0) { yearstr = yearori + '年'; }else{ yearstr = '公元前' + Math.abs(yearori) + '年'; } var title = hdori[index].title.replace(/<.*?>/ig,"").replace("\n","").replace("。",""); res += yearstr + title + '。'; } res += monthori + '月' + dayori + '號' + '的歷史已爲您播報完畢,請問您還想了解幾月幾號的歷史呢?'; this.emit(":tts",{ tts: res }); this.callback(null); }catch(e){ this.callback(e); } }, "EXIT_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "好的,已經退出歷史上的今天這個技能,再見!若是須要,您能夠直接對我說打開歷史上的今天。", action:"STOP" }); this.callback(null); }catch(e){ this.callback(e); } } };
針對上一段的JSEngine2.0實現(主要是數據獲取部分的改動,以下):post
更新於2017-10-11日下午測試
exports.handler = function(event, context, callback) { var rokid = Rokid.handler(event, context,callback); rokid.registerHandlers(handlers); rokid.execute(); }; dictText = {'一':'01','二':'02','三':'03','四':'04','五':'05','六':'06','七':'07','八':'08','九':'09','十':'10','十一':'11','十二':'12','十三':'13','十四':'14','十五':'15','十六':'16','十七':'17','十八':'18','十九':'19','二十':'20','二十一':'21','二十二':'22','二十三':'23','二十四':'24','二十五':'25','二十六':'26','二十七':'27','二十八':'28','二十九':'29','三十':'30','三十一':'31'}; onedaydictText = {'今天':0,'明天':1,'後天':2,'大後天':3,'大大後天':4,'大大大後天':5,'昨天':-1,'前天':-2,'大前天':-3,'大大前天':-4,'大大大前天':-5}; //若是是6月,返回06 function formatMonth(monthori){ if (!isNaN(monthori)) { if (parseInt(monthori) < 10) { monthurl = '0' + monthori; } else { monthurl = monthori; } } else { monthurl = dictText[monthori]; } return monthurl; } //若是是8號,返回08 function formatDay(dayori){ if (!isNaN(dayori)) { if (parseInt(dayori) < 10) { dayurl = '0' + dayori; } else { dayurl = dayori; } } else { dayurl = dictText[dayori]; } return dayurl; } var handlers = { "ROKID.INTENT.WELCOME":function(){ try{ this.emit(":tts",{ tts: "歡迎使用歷史上的今天這個技能!你能夠對我說想了解幾月幾號。" }); this.callback(null); }catch(e){ this.callback(e); } }, "PAUSE_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "",action:"PAUSE" }); this.callback(null); }catch(e){ this.callback(e); } }, "RESUME_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "",action:"RESUME" }); this.callback(null); }catch(e){ this.callback(e); } }, "HISTORY_DAY":function(){ try{ var oneday = Rokid.param.request.content.slots.oneday; var monthurl = ''; var dayurl = ''; if (oneday){ var dd = new Date(); dd.setDate(dd.getDate() + onedaydictText[oneday]); monthori = dd.getMonth()+1; dayori = dd.getDate(); monthurl = formatMonth(monthori); dayurl = formatDay(dayori); } else{
var monthori = JSON.parse(Rokid.param.request.content.slots.month).text;
monthurl = formatMonth(monthori); dayurl = formatDay(dayori); } // result = Rokid.sync_request('GET','https://baike.baidu.com/cms/home/eventsOnHistory/' + monthurl + '.json'); // result = Rokid.resHandler(result); Rokid.request({ url: 'https://baike.baidu.com/cms/home/eventsOnHistory/' + monthurl + '.json', method: "GET", headers: { 'User-Agent': 'request' } }, (err, resssssss, body) => { if(err) { this.emit(":error", err); } var result = JSON.parse(body); var hdori = result[monthurl][monthurl+dayurl]; var hdorilen = hdori.length; var res = '好的,爲您播報' + monthori + '月' + dayori + '號' + '的歷史。'; for (var index = hdorilen-1; index >= 0; index --){ var yearori = hdori[index].year; var yearstr = ''; if (parseInt(yearori) >= 0) { yearstr = yearori + '年'; }else{ yearstr = '公元前' + Math.abs(yearori) + '年'; } var title = hdori[index].title.replace(/<.*?>/ig,"").replace("\n","").replace("。",""); res += yearstr + title + '。'; } res += monthori + '月' + dayori + '號' + '的歷史已爲您播報完畢,請問您還想了解幾月幾號的歷史呢?'; this.emit(":tts",{ tts: res }); this.callback(null); }) }catch(e){ this.callback(e); } }, "EXIT_HISTORY_DAY":function(){ try{ this.emit(":tts",{ tts: "好的,已經退出歷史上的今天這個技能,再見!若是須要,您能夠直接對我說打開歷史上的今天。", action:"STOP" }); this.callback(null); }catch(e){ this.callback(e); } } };
集成測試:this
輸入語句:歷史上的6月8號(或者歷史上的六月八號)url
服務請求:
{ "context": { "application": { "applicationId": "RE85B257695B46C1A9AC5BDF2EAF0DC3" }, "device": { "basic": { "locale": "zh-cn", "timestamp": 0, "vendor": "F934FCE346C64393BEDBF8ECD9672446" }, "location": {}, "media": {} } }, "request": { "content": { "applicationId": "RE85B257695B46C1A9AC5BDF2EAF0DC3", "intent": "history_day", "slots": { "day": "8", "month": "6" } }, "reqId": "C92AF2274A634B11B70AB291A324AA0D", "reqType": "INTENT" }, "session": { "attributes": {}, "newSession": false }, "version": "2.0.0" }
服務返回:
{ "appId": "RE85B257695B46C1A9AC5BDF2EAF0DC3", "response": { "action": { "form": "cut", "shouldEndSession": true, "type": "NORMAL", "version": "2.0.0", "voice": { "behaviour": "APPEND", "item": { "tts": "好的,爲您播報6月8號的歷史。2012年上海合做組織北京峯會閉幕。2009年加蓬前總統哈吉奧馬爾·邦戈·翁丁巴去世。1995年中國研製出高功率氦氖激光器。1955年互聯網之父蒂姆·伯納斯·李出生。1921年印尼前總統蘇哈托誕生。1916年DNA雙螺旋結構的發現者之一克裏克出生。1876年法國女做家喬治·桑逝世。1867年普魯士軍隊吞併荷爾斯泰因公國。1867年美國建築師弗蘭克·勞埃德·賴特誕生。1845年美國第七任總統安德魯·傑克遜逝世。1824年加拿大批准第一個專利。1810年德國做曲家舒曼誕生。1809年英裔美國政治活動家托馬斯·潘恩逝世。1529年中世紀的一次重大宗教戰爭卡佩爾戰爭爆發。632年伊斯蘭教創立者、政治家、宗教領袖穆罕默德逝世。" }, "needEventCallback": false } }, "resType": "INTENT", "respId": "C92AF2274A634B11B70AB291A324AA0D" }, "session": { "attributes": {} }, "startWithActiveWord": false, "version": "2.0.0" }
輸入語句:我想了解今天spa
服務請求:
{ "context": { "application": { "applicationId": "RBE99BF67CD644C4BA4E6561833A2ED1" }, "device": { "basic": { "locale": "zh-cn", "timestamp": 0, "vendor": "7F9EB83B204C42DFB3ABEBA88DAA6237" }, "location": {}, "media": {} }, "user": {} }, "request": { "content": { "applicationId": "RBE99BF67CD644C4BA4E6561833A2ED1", "intent": "HISTORY_DAY", "slots": { "oneday": "今天" } }, "reqId": "076712871B92414E861831E109206FD5", "reqType": "INTENT" }, "session": { "attributes": {}, "newSession": false, "sessionId": "F087A392781D409CA8EFBB17CD6FB1ED" }, "version": "2.0.0" }
服務返回:
{ "appId": "RBE99BF67CD644C4BA4E6561833A2ED1", "response": { "action": { "form": "cut", "shouldEndSession": true, "type": "NORMAL", "version": "2.0.0", "voice": { "action": "PLAY", "item": { "tts": "好的,爲您播報7月3號的歷史。2012年美國著名演員安迪·格里菲斯去世。2005年西班牙正式經過同性婚姻相關法案。1986年美國自由神像矗立紐約100週年。1980年開國上將鄧華去世。1969年英國滾石樂隊的創始團員布萊恩·瓊斯去世。1962年美國電影演員湯姆·克魯斯出生。1904年同治、光緒皇帝的老師翁同龢去世。1898年有機化學家黃鳴龍去世。1898年京師大學堂成立。1883年奧地利做家卡夫卡出生。1876年中國第一條鐵路淞滬鐵路正式通車運營。1844年大海雀滅絕。1518年中國明代醫學家兼藥物學家李時珍出生。1423年法國瓦盧瓦王朝國王路易十一逝世。1062年中國北宋官員包拯去世。7月3號的歷史已爲您播報完畢,請問您還想了解幾月幾號的歷史呢?" } } }, "resType": "INTENT", "respId": "076712871B92414E861831E109206FD5" }, "session": {}, "startWithActiveWord": false, "version": "2.0.0" }
PS:強烈感謝大俊俊提供的JS Engine和各類技術支持,大俊俊貢獻了百分之八十。設計
至此大功告成。。。。
還有一些交互方面的事情須要考慮。。。
此技能Rokid開發者社區skill之【歷史上的今天】之簡介+玩法+設計+實現+心得,參考http://www.cnblogs.com/zhzhang/p/7136583.html
待續