alexa node.js意图问题

vktxenjb  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(400)

我使用开发者控制台创建了一个alexa技能。我在nodejs代码中添加了一个新的intent并添加了相应的处理程序。启动请求正在工作,但未调用我的意图处理程序。请帮忙。。可能是什么问题?为了简单起见,我在这里粘贴基本代码。。
json:

  1. {
  2. "interactionModel": {
  3. "languageModel": {
  4. "invocationName": "intent test skill",
  5. "intents": [
  6. {
  7. "name": "AMAZON.CancelIntent",
  8. "samples": []
  9. },
  10. {
  11. "name": "AMAZON.HelpIntent",
  12. "samples": []
  13. },
  14. {
  15. "name": "AMAZON.StopIntent",
  16. "samples": []
  17. },
  18. {
  19. "name": "HelloWorldIntent",
  20. "slots": [],
  21. "samples": [
  22. "hello",
  23. "how are you",
  24. "say hi world",
  25. "say hi",
  26. "hi",
  27. "say hello world",
  28. "say hello"
  29. ]
  30. },
  31. {
  32. "name": "AMAZON.NavigateHomeIntent",
  33. "samples": []
  34. },
  35. {
  36. "name": "AMAZON.FallbackIntent",
  37. "samples": []
  38. },
  39. {
  40. "name": "helloworldtwo",
  41. "slots": [],
  42. "samples": [
  43. "second intent",
  44. "trigger second intent"
  45. ]
  46. }
  47. ],
  48. "types": []
  49. }
  50. }
  51. }

node.js:

  1. const Alexa = require('ask-sdk-core');
  2. const LaunchRequestHandler = {
  3. canHandle(handlerInput) {
  4. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
  5. },
  6. handle(handlerInput) {
  7. const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
  8. return handlerInput.responseBuilder
  9. .speak(speakOutput)
  10. .reprompt(speakOutput)
  11. .getResponse();
  12. }
  13. };
  14. const HelloWorldIntentHandler = {
  15. canHandle(handlerInput) {
  16. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  17. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
  18. },
  19. handle(handlerInput) {
  20. const speakOutput = 'Hello World!';
  21. return handlerInput.responseBuilder
  22. .speak(speakOutput)
  23. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  24. .getResponse();
  25. }
  26. };
  27. const HelloWorldTwoHandler = {
  28. canHandle(handlerInput) {
  29. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  30. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'helloworldtwo';
  31. },
  32. handle(handlerInput) {
  33. const speakOutput = 'intent triggered';
  34. return handlerInput.responseBuilder
  35. .speak(speakOutput)
  36. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  37. .getResponse();
  38. }
  39. };
  40. const HelpIntentHandler = {
  41. canHandle(handlerInput) {
  42. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  43. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
  44. },
  45. handle(handlerInput) {
  46. const speakOutput = 'You can say hello to me! How can I help?';
  47. return handlerInput.responseBuilder
  48. .speak(speakOutput)
  49. .reprompt(speakOutput)
  50. .getResponse();
  51. }
  52. };
  53. const CancelAndStopIntentHandler = {
  54. canHandle(handlerInput) {
  55. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  56. && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
  57. || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
  58. },
  59. handle(handlerInput) {
  60. const speakOutput = 'Goodbye!';
  61. return handlerInput.responseBuilder
  62. .speak(speakOutput)
  63. .getResponse();
  64. }
  65. };
  66. const FallbackIntentHandler = {
  67. canHandle(handlerInput) {
  68. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  69. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
  70. },
  71. handle(handlerInput) {
  72. const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
  73. return handlerInput.responseBuilder
  74. .speak(speakOutput)
  75. .reprompt(speakOutput)
  76. .getResponse();
  77. }
  78. };
  79. const SessionEndedRequestHandler = {
  80. canHandle(handlerInput) {
  81. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
  82. },
  83. handle(handlerInput) {
  84. console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
  85. // Any cleanup logic goes here.
  86. return handlerInput.responseBuilder.getResponse(); // notice we send an empty response
  87. }
  88. };
  89. const IntentReflectorHandler = {
  90. canHandle(handlerInput) {
  91. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
  92. },
  93. handle(handlerInput) {
  94. const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
  95. const speakOutput = `You just triggered ${intentName}`;
  96. return handlerInput.responseBuilder
  97. .speak(speakOutput)
  98. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  99. .getResponse();
  100. }
  101. };
  102. const ErrorHandler = {
  103. canHandle() {
  104. return true;
  105. },
  106. handle(handlerInput, error) {
  107. const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
  108. console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
  109. return handlerInput.responseBuilder
  110. .speak(speakOutput)
  111. .reprompt(speakOutput)
  112. .getResponse();
  113. }
  114. };
  115. exports.handler = Alexa.SkillBuilders.custom()
  116. .addRequestHandlers(
  117. LaunchRequestHandler,
  118. HelloWorldIntentHandler,
  119. HelloWorldTwoHandler,
  120. HelpIntentHandler,
  121. CancelAndStopIntentHandler,
  122. FallbackIntentHandler,
  123. SessionEndedRequestHandler,
  124. IntentReflectorHandler)
  125. .addErrorHandlers(
  126. ErrorHandler)
  127. .withCustomUserAgent('sample/hello-world/v1.2')
  128. .lambda();
  129. const Alexa = require('ask-sdk-core');
  130. const LaunchRequestHandler = {
  131. canHandle(handlerInput) {
  132. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
  133. },
  134. handle(handlerInput) {
  135. const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
  136. return handlerInput.responseBuilder
  137. .speak(speakOutput)
  138. .reprompt(speakOutput)
  139. .getResponse();
  140. }
  141. };
  142. const HelloWorldIntentHandler = {
  143. canHandle(handlerInput) {
  144. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  145. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
  146. },
  147. handle(handlerInput) {
  148. const speakOutput = 'Hello World!';
  149. return handlerInput.responseBuilder
  150. .speak(speakOutput)
  151. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  152. .getResponse();
  153. }
  154. };
  155. const HelloWorldTwoHandler = {
  156. canHandle(handlerInput) {
  157. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  158. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'helloworldtwo';
  159. },
  160. handle(handlerInput) {
  161. const speakOutput = 'intent triggered';
  162. return handlerInput.responseBuilder
  163. .speak(speakOutput)
  164. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  165. .getResponse();
  166. }
  167. };
  168. const HelpIntentHandler = {
  169. canHandle(handlerInput) {
  170. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  171. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
  172. },
  173. handle(handlerInput) {
  174. const speakOutput = 'You can say hello to me! How can I help?';
  175. return handlerInput.responseBuilder
  176. .speak(speakOutput)
  177. .reprompt(speakOutput)
  178. .getResponse();
  179. }
  180. };
  181. const CancelAndStopIntentHandler = {
  182. canHandle(handlerInput) {
  183. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  184. && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
  185. || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
  186. },
  187. handle(handlerInput) {
  188. const speakOutput = 'Goodbye!';
  189. return handlerInput.responseBuilder
  190. .speak(speakOutput)
  191. .getResponse();
  192. }
  193. };
  194. const FallbackIntentHandler = {
  195. canHandle(handlerInput) {
  196. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  197. && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
  198. },
  199. handle(handlerInput) {
  200. const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
  201. return handlerInput.responseBuilder
  202. .speak(speakOutput)
  203. .reprompt(speakOutput)
  204. .getResponse();
  205. }
  206. };
  207. const SessionEndedRequestHandler = {
  208. canHandle(handlerInput) {
  209. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
  210. },
  211. handle(handlerInput) {
  212. console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
  213. // Any cleanup logic goes here.
  214. return handlerInput.responseBuilder.getResponse(); // notice we send an empty response
  215. }
  216. };
  217. const IntentReflectorHandler = {
  218. canHandle(handlerInput) {
  219. return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
  220. },
  221. handle(handlerInput) {
  222. const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
  223. const speakOutput = `You just triggered ${intentName}`;
  224. return handlerInput.responseBuilder
  225. .speak(speakOutput)
  226. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  227. .getResponse();
  228. }
  229. };
  230. const ErrorHandler = {
  231. canHandle() {
  232. return true;
  233. },
  234. handle(handlerInput, error) {
  235. const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
  236. console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
  237. return handlerInput.responseBuilder
  238. .speak(speakOutput)
  239. .reprompt(speakOutput)
  240. .getResponse();
  241. }
  242. };
  243. exports.handler = Alexa.SkillBuilders.custom()
  244. .addRequestHandlers(
  245. LaunchRequestHandler,
  246. HelloWorldIntentHandler,
  247. HelloWorldTwoHandler,
  248. HelpIntentHandler,
  249. CancelAndStopIntentHandler,
  250. FallbackIntentHandler,
  251. SessionEndedRequestHandler,
  252. IntentReflectorHandler)
  253. .addErrorHandlers(
  254. ErrorHandler)
  255. .withCustomUserAgent('sample/hello-world/v1.2')
  256. .lambda();

你们能告诉我哪里出了问题吗?

ffx8fchx

ffx8fchx1#

shouldendsession没有设置为false,因为我没有显式声明它,也没有在响应生成器中使用reprompt方法。这就是这里的问题:)

相关问题