0

Ich habe mir ein paar andere Antworten angeschaut und kann wirklich nicht funktionieren.Wie ein http GET aufrufen und die Antwort in AWS Lambda verwenden?

Ich möchte ein Lambda von Alexa anrufen - das ist in Ordnung und gibt mir eine Antwort. Ich möchte diese Antwort auf einen Aufruf an einen HTTP-GET-Webdienst basieren. Ich habe den Code unten so weit gekommen (aktualisiert, um die volle Lambda Code zu erhalten):

/** 

Copyright 2016 Brian Donohue. 

*/ 

'use strict'; 

// Route the incoming request based on type (LaunchRequest, IntentRequest, 
// etc.) The JSON body of the request is provided in the event parameter. 
exports.handler = function (event, context) { 
    try { 
     console.log("event.session.application.applicationId=" + event.session.application.applicationId); 

     /** 
     * Uncomment this if statement and populate with your skill's application ID to 
     * prevent someone else from configuring a skill that sends requests to this function. 
     */ 

//  if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.05aecccb3-1461-48fb-a008-822ddrt6b516") { 
//   context.fail("Invalid Application ID"); 
//  } 

     if (event.session.new) { 
      onSessionStarted({requestId: event.request.requestId}, event.session); 
     } 

     if (event.request.type === "LaunchRequest") { 
      onLaunch(event.request, 
       event.session, 
       function callback(sessionAttributes, speechletResponse) { 
        context.succeed(buildResponse(sessionAttributes, speechletResponse)); 
       }); 
     } else if (event.request.type === "IntentRequest") { 
      onIntent(event.request, 
       event.session, 
       function callback(sessionAttributes, speechletResponse) { 
        context.succeed(buildResponse(sessionAttributes, speechletResponse)); 
       }); 
     } else if (event.request.type === "SessionEndedRequest") { 
      onSessionEnded(event.request, event.session); 
      context.succeed(); 
     } 
    } catch (e) { 
     context.fail("Exception: " + e); 
    } 
}; 

/** 
* Called when the session starts. 
*/ 
function onSessionStarted(sessionStartedRequest, session) { 
    console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId 
     + ", sessionId=" + session.sessionId); 

    // add any session init logic here 
} 

/** 
* Called when the user invokes the skill without specifying what they want. 
*/ 
function onLaunch(launchRequest, session, callback) { 
    console.log("onLaunch requestId=" + launchRequest.requestId 
     + ", sessionId=" + session.sessionId); 

    var cardTitle = "Hello, World!" 
    var speechOutput = "You can tell Hello, World! to say Hello, World!" 
    callback(session.attributes, 
     buildSpeechletResponse(cardTitle, speechOutput, "", true)); 
} 

/** 
* Called when the user specifies an intent for this skill. 
*/ 
function onIntent(intentRequest, session, callback) { 
    console.log("onIntent requestId=" + intentRequest.requestId 
     + ", sessionId=" + session.sessionId); 

    var intent = intentRequest.intent, 
     intentName = intentRequest.intent.name; 

    // dispatch custom intents to handlers here 
    if (intentName == 'TestIntent') { 
     handleTestRequest(intent, session, callback); 
    } 
    else { 
     throw "Invalid intent"; 
    } 
} 

/** 
* Called when the user ends the session. 
* Is not called when the skill returns shouldEndSession=true. 
*/ 
function onSessionEnded(sessionEndedRequest, session) { 
    console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId 
     + ", sessionId=" + session.sessionId); 

    // Add any cleanup logic here 
} 

function handleTestRequest(intent, session, callback) { 

    //---Custom Code--- 
var speechOutput; 


var myCallback = function(data) { 
    console.log('got data: '+data); 
    speechOutput = data; 
}; 


var usingItNow = function(callback) { 
const http = require('http'); 

var url = "http://services.groupkt.com/country/get/iso2code/IN"; 

    var req = http.get(url, (res) => { 
     var body = ""; 

     res.on("data", (chunk) => { 
      body += chunk; 
     }); 

     res.on("end",() => { 
      var result = JSON.parse(body); 

      //callback({"email":"test","name" : result.name}); 
      callback('test'); 
     }); 
    }).on("error", (error) => { 
     //callback(err); 
     console.log('error'); 
    }); 
}; 

usingItNow(myCallback); 




    //----------------- 
    callback(session.attributes, 
     buildSpeechletResponseWithoutCard("Testing Output " + speechOutput, "", "true")); 

} 

// ------- Helper functions to build responses ------- 



function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { 
    return { 
     outputSpeech: { 
      type: "PlainText", 
      text: output 
     }, 
     card: { 
      type: "Simple", 
      title: title, 
      content: output 
     }, 
     reprompt: { 
      outputSpeech: { 
       type: "PlainText", 
       text: repromptText 
      } 
     }, 
     shouldEndSession: shouldEndSession 
    }; 
} 

function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) { 
    return { 
     outputSpeech: { 
      type: "PlainText", 
      text: output 
     }, 
     reprompt: { 
      outputSpeech: { 
       type: "PlainText", 
       text: repromptText 
      } 
     }, 
     shouldEndSession: shouldEndSession 
    }; 
} 

function buildResponse(sessionAttributes, speechletResponse) { 
    return { 
     version: "1.0", 
     sessionAttributes: sessionAttributes, 
     response: speechletResponse 
    }; 
} 

ich diesen Code aktualisiert haben, auf der Grundlage der Antwort unten, aber mein Test Rückruf wird nicht einmal arbeiten, nie die wirkliche Geist ein. Ich nehme an, dass ich etwas vermisse ...

Danke.

Edit: hier sind meine Probe Äußerungen und Absichten (alle von Beispiel I gefolgt)

{ 
    "intents": [ 
    { 
     "intent": "TestIntent" 
    }, 
    { 
     "intent": "AMAZON.PauseIntent" 
    }, 
    { 
     "intent": "AMAZON.ResumeIntent" 
    } 
    ] 
} 

TestIntent hello world 
TestIntent say hello world 
TestIntent to say hello world 
TestIntent test 

Antwort

2

Sie brauchen nicht zu ‚Rückkehr‘ etwas implizit aus http.get. Ihre Antwort sollte als Aufruf wieder zurück, wie unten,

var url = `URL HERE`; 
 

 
    var req = http.get(url, (res) => { 
 
     var body = ""; 
 

 
     res.on("data", (chunk) => { 
 
      body += chunk 
 
     }); 
 

 
     res.on("end",() => { 
 
      var result = JSON.parse(body); 
 
         
 
      callBack(result) 
 
     }); 
 
    }).on("error", (error) => { 
 
     callBack(err); 
 
    });

Ich habe allein nur die Lambda-Funktion Logik aktualisiert erhalten Details von REST Anruf. Bitte beachten Sie, dass es keine Build-Antwort für Alexa, sondern nur den REST-Service enthält. Sie finden den gleichen Code unten,

const http = require('http'); 
 

 
exports.handler = (event, context, callback) => { 
 
    
 

 
     usingItNow(function (result, error) { 
 

 

 
      if (error) { 
 
       console.log('error') 
 
      } else { 
 
       console.log("Final result is"+JSON.stringify(result)) 
 
       callback(null,buildSpeechletResponseWithoutCard(result.name,"sample re-prompt",true)) 
 
      } 
 

 

 
     }); 
 

 
}; 
 

 
var usingItNow = function (callback) { 
 
    
 
    var url = "http://services.groupkt.com/country/get/iso2code/IN"; 
 

 
    var req = http.get(url, (res) => { 
 
     var body = ""; 
 

 
     res.on("data", (chunk) => { 
 
      body += chunk; 
 
     }); 
 

 
     res.on("end",() => { 
 
      var result = JSON.parse(body); 
 
      
 
      callback({ "email": "test", "name":result.RestResponse.result.name }); 
 
      //callback('test'); 
 
     }); 
 
    }).on("error", (error) => { 
 
     //callback(err); 
 
     console.log('error'); 
 
    }); 
 
}; 
 

 
function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) { 
 
    return { 
 
     outputSpeech: { 
 
      type: "PlainText", 
 
      text: output 
 
     }, 
 
     reprompt: { 
 
      outputSpeech: { 
 
       type: "PlainText", 
 
       text: repromptText 
 
      } 
 
     }, 
 
     shouldEndSession: shouldEndSession 
 
    }; 
 
}

und das Ergebnis, das ich bekam,

enter image description here

+0

Danke für die Antwort. Wo würde ich das in meinen Code einfügen, um einen Teil der Antwort als String zu erhalten? Ich werde jetzt damit spielen. –

+1

Ich habe die Antwort aktualisiert, um alles in Ihr Codebeispiel einzufügen. –

+0

Nochmals vielen Dank. Ich vermute, dass ich etwas falsch mache, da der Rückruf in Ihrem Code nicht funktioniert, aber auch nicht der Callback ("Test"), den ich verwendet habe. Ich werde meinen ursprünglichen Beitrag aktualisieren, um den vollständigen Code anzuzeigen. –