Advertisement
wcchuo

weather_fn

Jan 21st, 2019
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. // Ability definition
  2. const https = require('https');
  3.  
  4. function extractSlots(data) {
  5. slots = {}
  6. slotsArray = data["slots"]
  7. for (var i = 0; i < slotsArray.length; i++) {
  8. slot = slotsArray[i]
  9. name = slot["name"]
  10. slots[slot["name"]] = slot["value"]
  11. }
  12. return slots
  13. }
  14.  
  15. function extractData(jsonData) {
  16. detailJson = jsonData["HeWeather6"][0];
  17. if (detailJson["status"] != "ok") {
  18. city = detailJson["status"]
  19. temperature = detailJson["status"];
  20. imageUrl = "https://www.clipartmax.com/png/full/249-2493413_mostly-cloudy-weather-icon-partly-cloudy-icon-png.png";
  21. } else {
  22. city = detailJson["basic"]["location"]
  23. temperature = detailJson["now"]["tmp"]
  24. cond_code = detailJson["now"]["cond_code"]
  25. imageUrl = "https://cdn.heweather.com/cond_icon/" + cond_code + ".png"
  26. console.log(city)
  27. console.log(imageUrl)
  28. console.log(temperature)
  29. }
  30. return {
  31. "city": city,
  32. "temperature": temperature,
  33. "imageURL": imageUrl
  34. }
  35. }
  36.  
  37. const ability = {
  38. "interactionModel": {
  39. "languageModel": {
  40. "invocationName": "weather",
  41. "intents": [{
  42. "name": "WeatherIntent",
  43. "samples": [
  44. "What is the weather in {city}"
  45. ]
  46. },
  47. {
  48. "name": "FallbackIntent",
  49. "samples": []
  50. },
  51. {
  52. "name": "CancelIntent",
  53. "samples": []
  54. },
  55. {
  56. "name": "HelpIntent",
  57. "samples": []
  58. },
  59. {
  60. "name": "StopIntent",
  61. "samples": []
  62. }
  63. ],
  64. "types": []
  65. }
  66. }
  67. }
  68. const MATCH_FOUND = "MATCH_FOUND"
  69. const MATCH_NOT_FOUND = "MATCH_NOT_FOUND"
  70. const invocationName = "weather"
  71. const validInvocation = (_invocationName) => {
  72. return _invocationName && _invocationName === invocationName;
  73. }
  74. const types = {}
  75. const validateAndGetSlotValues = (slots = [], intentSlots = {}) => {
  76. const slot_values_resolved = []
  77. for (let slot of slots) {
  78. // check for the slot val presence
  79. const status = ((intentSlots.hasOwnProperty(slot.name) && (intentSlots[slot.name] === slot.type)) &&
  80. (types.hasOwnProperty(slot.type) && types[slot.type].indexOf(slot.value) >= 0)) ?
  81. MATCH_FOUND : MATCH_NOT_FOUND;
  82. slot_values_resolved.push({
  83. type: slot.type,
  84. name: slot.name,
  85. value: slot.value,
  86. status
  87. })
  88. }
  89. return slot_values_resolved;
  90. }
  91.  
  92. const getSlotsResolvedSay = (slotVals) => {
  93. let resolved_say = '';
  94. for (let slot of slotVals) {
  95. resolved_say += `
  96. Slot "${slot.name}" with value "${slot.value}" is ${slot.status === MATCH_FOUND ? "found" : "not found"}`
  97. }
  98. return resolved_say;
  99. }
  100.  
  101. const intentHandlersMap = {
  102. WeatherIntent_Handler(data, callback) {
  103. slotsMap = extractSlots(data)
  104. if (slotsMap["city"] == undefined) {
  105. city = "beijing"
  106. } else {
  107. city = slotsMap["city"]
  108. }
  109. https.get("https://free-api.heweather.net/s6/weather/now?location=" + encodeURI(city) + "&key=HE1901180204291331", (res) => {
  110. //console.log('payloads:', res.d)
  111. let data = '';
  112. res.on('data', (d) => {
  113. data += d
  114. });
  115. res.on('end', () => {
  116. //console.log(JSON.parse(data));
  117. jsonData = JSON.parse(data);
  118. filteredData = extractData(jsonData)
  119. res = {
  120. "layout": "CityWeatherLayout",
  121. "payload": {
  122. "header": "header",
  123. "background": "backgroundColor1Style",
  124. "imageUrl": filteredData["imageURL"],
  125. "city": filteredData["city"],
  126. "temparature": filteredData["temperature"].toString()
  127. }
  128. }
  129. callback(null, res)
  130. });
  131. }).on('error', (e) => {
  132. console.error(e);
  133. callback(null, e);
  134. });
  135. },
  136. CityIntent_Handler(data, callback) {
  137. res = {
  138. "layout": "CityDetailLayout",
  139. "payload": {
  140. "header": "header",
  141. "city": "北京 (Beijing)",
  142. "details": "北京市简称京,是中华人民共和国的首都。",
  143. "background": "backgroundColor2Style"
  144. }
  145. }
  146. callback(null, res);
  147. },
  148. FallbackIntent_Handler(data) {
  149. const slotsTypes = {}
  150. const samplesFormat = []
  151. if (validInvocation(data.invocationName)) {
  152. let say = 'Hello, intent FallbackIntent is called...'
  153. const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
  154. say += getSlotsResolvedSay(resolved_slots)
  155. return say
  156. } else {
  157. return 'Invalid invocation, please check the invocation name...'
  158. }
  159. },
  160. CancelIntent_Handler(data) {
  161. const slotsTypes = {}
  162. const samplesFormat = []
  163. if (validInvocation(data.invocationName)) {
  164. let say = 'Hello, intent CancelIntent is called...'
  165. const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
  166. say += getSlotsResolvedSay(resolved_slots)
  167. return say
  168. } else {
  169. return 'Invalid invocation, please check the invocation name...'
  170. }
  171. },
  172.  
  173. HelpIntent_Handler(data) {
  174. const slotsTypes = {}
  175. const samplesFormat = []
  176. if (validInvocation(data.invocationName)) {
  177. let say = 'Hello, intent HelpIntent is called...'
  178. const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
  179. say += getSlotsResolvedSay(resolved_slots)
  180. return say
  181. } else {
  182. return 'Invalid invocation, please check the invocation name...'
  183. }
  184. },
  185. StopIntent_Handler(data) {
  186. const slotsTypes = {}
  187. const samplesFormat = []
  188. if (validInvocation(data.invocationName)) {
  189. let say = 'Hello, intent StopIntent is called...'
  190. const resolved_slots = validateAndGetSlotValues(data.slots, slotsTypes)
  191. say += getSlotsResolvedSay(resolved_slots)
  192. return say
  193. } else {
  194. return 'Invalid invocation, please check the invocation name...'
  195. }
  196. },
  197. };
  198.  
  199. exports.myHandler = (req, callback) => {
  200. // console.log(req.array[0])
  201. const data = JSON.parse(req.array[0])
  202. intentHandlersMap[`${data.intentName}_Handler`](data, callback)
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement