Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ability definition
- const https = require('https');
- function extractSlots(data) {
- slots = {}
- slotsArray = data["slots"]
- for (var i = 0; i < slotsArray.length; i++) {
- slot = slotsArray[i]
- name = slot["name"]
- slots[slot["name"]] = slot["value"]
- }
- return slots
- }
- function extractData(jsonData) {
- detailJson = jsonData["HeWeather6"][0];
- if (detailJson["status"] != "ok") {
- city = detailJson["status"]
- temperature = detailJson["status"];
- imageUrl = "https://www.clipartmax.com/png/full/249-2493413_mostly-cloudy-weather-icon-partly-cloudy-icon-png.png";
- } else {
- city = detailJson["basic"]["location"]
- temperature = detailJson["now"]["tmp"]
- cond_code = detailJson["now"]["cond_code"]
- imageUrl = "https://cdn.heweather.com/cond_icon/" + cond_code + ".png"
- console.log(city)
- console.log(imageUrl)
- console.log(temperature)
- }
- return {
- "city": city,
- "temperature": temperature,
- "imageURL": imageUrl
- }
- }
- const ability = {
- "interactionModel": {
- "languageModel": {
- "invocationName": "weather",
- "intents": [{
- "name": "WeatherIntent",
- "samples": [
- "What is the weather in {city}"
- ]
- },
- {
- "name": "FallbackIntent",
- "samples": []
- },
- {
- "name": "CancelIntent",
- "samples": []
- },
- {
- "name": "HelpIntent",
- "samples": []
- },
- {
- "name": "StopIntent",
- "samples": []
- }
- ],
- "types": []
- }
- }
- }
- const MATCH_FOUND = "MATCH_FOUND"
- const MATCH_NOT_FOUND = "MATCH_NOT_FOUND"
- const invocationName = "weather"
- const validInvocation = (_invocationName) => {
- return _invocationName && _invocationName === invocationName;
- }
- const types = {}
- const validateAndGetSlotValues = (slots = [], intentSlots = {}) => {
- const slot_values_resolved = []
- for (let slot of slots) {
- // check for the slot val presence
- const status = ((intentSlots.hasOwnProperty(slot.name) && (intentSlots[slot.name] === slot.type)) &&
- (types.hasOwnProperty(slot.type) && types[slot.type].indexOf(slot.value) >= 0)) ?
- MATCH_FOUND : MATCH_NOT_FOUND;
- slot_values_resolved.push({
- type: slot.type,
- name: slot.name,
- value: slot.value,
- status
- })
- }
- return slot_values_resolved;
- }
- const getSlotsResolvedSay = (slotVals) => {
- let resolved_say = '';
- for (let slot of slotVals) {
- resolved_say += `
- Slot "${slot.name}" with value "${slot.value}" is ${slot.status === MATCH_FOUND ? "found" : "not found"}`
- }
- return resolved_say;
- }
- const intentHandlersMap = {
- WeatherIntent_Handler(data, callback) {
- slotsMap = extractSlots(data)
- if (slotsMap["city"] == undefined) {
- city = "beijing"
- } else {
- city = slotsMap["city"]
- }
- https.get("https://free-api.heweather.net/s6/weather/now?location=" + encodeURI(city) + "&key=HE1901180204291331", (res) => {
- //console.log('payloads:', res.d)
- let data = '';
- res.on('data', (d) => {
- data += d
- });
- res.on('end', () => {
- //console.log(JSON.parse(data));
- jsonData = JSON.parse(data);
- filteredData = extractData(jsonData)
- res = {
- "layout": "CityWeatherLayout",
- "payload": {
- "header": "header",
- "background": "backgroundColor1Style",
- "imageUrl": filteredData["imageURL"],
- "city": filteredData["city"],
- "temparature": filteredData["temperature"].toString()
- }
- }
- callback(null, res)
- });
- }).on('error', (e) => {
- console.error(e);
- callback(null, e);
- });
- },
- CityIntent_Handler(data, callback) {
- res = {
- "layout": "CityDetailLayout",
- "payload": {
- "header": "header",
- "city": "北京 (Beijing)",
- "details": "北京市简称京,是中华人民共和国的首都。",
- "background": "backgroundColor2Style"
- }
- }
- callback(null, res);
- },
- FallbackIntent_Handler(data) {
- const slotsTypes = {}
- const samplesFormat = []
- if (validInvocation(data.invocationName)) {
- let say = 'Hello, intent FallbackIntent is called...'
- const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
- say += getSlotsResolvedSay(resolved_slots)
- return say
- } else {
- return 'Invalid invocation, please check the invocation name...'
- }
- },
- CancelIntent_Handler(data) {
- const slotsTypes = {}
- const samplesFormat = []
- if (validInvocation(data.invocationName)) {
- let say = 'Hello, intent CancelIntent is called...'
- const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
- say += getSlotsResolvedSay(resolved_slots)
- return say
- } else {
- return 'Invalid invocation, please check the invocation name...'
- }
- },
- HelpIntent_Handler(data) {
- const slotsTypes = {}
- const samplesFormat = []
- if (validInvocation(data.invocationName)) {
- let say = 'Hello, intent HelpIntent is called...'
- const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
- say += getSlotsResolvedSay(resolved_slots)
- return say
- } else {
- return 'Invalid invocation, please check the invocation name...'
- }
- },
- StopIntent_Handler(data) {
- const slotsTypes = {}
- const samplesFormat = []
- if (validInvocation(data.invocationName)) {
- let say = 'Hello, intent StopIntent is called...'
- const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
- say += getSlotsResolvedSay(resolved_slots)
- return say
- } else {
- return 'Invalid invocation, please check the invocation name...'
- }
- },
- };
- exports.myHandler = (req, callback) => {
- // console.log(req.array[0])
- const data = JSON.parse(req.array[0])
- intentHandlersMap[`${data.intentName}_Handler`](data, callback)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement