Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. var fetch = require('node-fetch');
  2.  
  3. var endpoint = "https://app.wehaus.com/api/v2/" // ENDPOINT GOES HERE
  4.  
  5. exports.handler = (event, context) => {
  6.  
  7. try {
  8.  
  9. if (event.session.new) {
  10. // New Session
  11. console.log("NEW SESSION")
  12. }
  13.  
  14. switch (event.request.type) {
  15.  
  16. case "LaunchRequest":
  17. // Launch Request
  18. console.log(`LAUNCH REQUEST`)
  19. context.succeed(
  20. generateResponse(
  21. buildSpeechletResponse("Welcome to an Alexa by Wehaus", true),
  22. {}
  23. )
  24. )
  25. break;
  26.  
  27. case "IntentRequest":
  28. // Intent Request
  29. console.log(`INTENT REQUEST`)
  30.  
  31. switch(event.request.intent.name) {
  32. case "TurnOnAll":
  33. fetch(endpoint + 'devices', {
  34. method: 'GET',
  35. headers: {
  36. "X-User-Email": "oficina@wehaus.com",
  37. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  38. }
  39. }).then((data) => {
  40. if(data){
  41. let response = data.json();
  42. response.then((devices) => {
  43. if(devices){
  44. devices.forEach((device) => {
  45. if(device.primitive === "ActionDevice"){
  46. fetch(endpoint + 'device/' + device.id, {
  47. method: 'PUT',
  48. headers: {
  49. "X-User-Email": "oficina@wehaus.com",
  50. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  51. },
  52. body: JSON.stringify({
  53. state: 'on'
  54. })
  55. })
  56. }
  57. })
  58. }
  59. })
  60. context.succeed(
  61. generateResponse(
  62. buildSpeechletResponse(`All your lights is set on`, true),
  63. {}
  64. )
  65. )
  66. }
  67. })
  68. break;
  69.  
  70. case "TurnOffAll":
  71. fetch(endpoint + 'devices', {
  72. method: 'GET',
  73. headers: {
  74. "X-User-Email": "oficina@wehaus.com",
  75. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  76. }
  77. }).then((data) => {
  78. if(data){
  79. let response = data.json();
  80. response.then((devices) => {
  81. if(devices){
  82. devices.forEach((device) => {
  83. if(device.primitive === "ActionDevice"){
  84. fetch(endpoint + 'device/' + device.id, {
  85. method: 'PUT',
  86. headers: {
  87. "X-User-Email": "oficina@wehaus.com",
  88. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  89. },
  90. body: JSON.stringify({
  91. state: 'off'
  92. })
  93. })
  94. }
  95. })
  96. }
  97. })
  98. context.succeed(
  99. generateResponse(
  100. buildSpeechletResponse(`All your lights is set off`, true),
  101. {}
  102. )
  103. )
  104. }
  105. })
  106. break;
  107.  
  108. case "TurnOnDevice":
  109. console.log(event.request.intent.slots.ID.value)
  110. fetch(endpoint + 'device/' + event.request.intent.slots.ID.value, {
  111. method: 'PUT',
  112. headers: {
  113. "X-User-Email": "oficina@wehaus.com",
  114. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  115. },
  116. body: JSON.stringify({
  117. state: 'on'
  118. })
  119. }).then((data) => {
  120. if(data){
  121. context.succeed(
  122. generateResponse(
  123. buildSpeechletResponse(`Your device is set on`, true),
  124. {}
  125. )
  126. )
  127. }
  128. })
  129. break;
  130.  
  131. case "TurnOffDevice":
  132. console.log(event.request.intent.slots.ID.value)
  133. fetch(endpoint + 'device/' + event.request.intent.slots.ID.value, {
  134. method: 'PUT',
  135. headers: {
  136. "X-User-Email": "oficina@wehaus.com",
  137. "X-User-Token": "mKyokxzJyC_3F45QiEry"
  138. },
  139. body: JSON.stringify({
  140. state: 'off'
  141. })
  142. }).then((data) => {
  143. if(data){
  144. context.succeed(
  145. generateResponse(
  146. buildSpeechletResponse(`Your device is set on`, true),
  147. {}
  148. )
  149. )
  150. }
  151. })
  152. break;
  153.  
  154. default:
  155. throw "Invalid intent"
  156. }
  157.  
  158. break;
  159.  
  160. case "SessionEndedRequest":
  161. // Session Ended Request
  162. console.log(`SESSION ENDED REQUEST`)
  163. break;
  164.  
  165. default:
  166. context.fail(`INVALID REQUEST TYPE: ${event.request.type}`)
  167.  
  168. }
  169.  
  170. } catch(error) { context.fail(`Exception: ${error}`) }
  171.  
  172. }
  173.  
  174. // Helpers
  175. buildSpeechletResponse = (outputText, shouldEndSession) => {
  176.  
  177. return {
  178. outputSpeech: {
  179. type: "PlainText",
  180. text: outputText
  181. },
  182. shouldEndSession: shouldEndSession
  183. }
  184.  
  185. }
  186.  
  187. generateResponse = (speechletResponse, sessionAttributes) => {
  188.  
  189. return {
  190. version: "1.0",
  191. sessionAttributes: sessionAttributes,
  192. response: speechletResponse
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement