请求开发Alexa电台技能Lambda脚本:按电台名播放流媒体
Got it, let's walk through how to build this Lambda function step by step. I'll break down the key parts so you can adapt it to your setup easily.
核心思路
When a user says "Alexa, play $station on <技能名>", Alexa will pass the $station value as a slot to your Lambda function. We need to:
- Extract the station name from the request
- Match it against your JSON catalog to get the streaming URL
- Build a response that tells Alexa to play the stream
- Return the required voice reply
完整Lambda代码示例 (Node.js)
We'll use the Alexa Skills Kit v2 (the current recommended framework) for this:
const Alexa = require('ask-sdk-core'); // Replace this with your actual station JSON catalog // You can also load it from a file like require('./stations.json') const STATION_CATALOG = { "Indie Mix": "https://your-radio-domain.com/indie", "Jazz Lounge": "https://your-radio-domain.com/jazz", "News Talk": "https://your-radio-domain.com/news" }; // Handler for the play station intent const PlayStationHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'PlayStationIntent'; }, handle(handlerInput) { const { requestEnvelope, responseBuilder } = handlerInput; // Pull the station name from the user's request const requestedStation = Alexa.getSlotValue(requestEnvelope, 'StationName'); // Match the station (case-insensitive for better user experience) const matchedStation = Object.keys(STATION_CATALOG).find(station => station.toLowerCase() === requestedStation.toLowerCase() ); // Handle case where station isn't found if (!matchedStation) { return responseBuilder .speak(`Sorry, I can't find the ${requestedStation} station. Try another one!`) .getResponse(); } const stationUrl = STATION_CATALOG[matchedStation]; const skillName = "Your Skill Name"; // Replace with your actual skill name // Build response to play the stream and speak the confirmation return responseBuilder .speak(`OK, playing ${matchedStation} on ${skillName}`) .addAudioPlayerPlayDirective('REPLACE_ALL', stationUrl, stationUrl, 0, null) .getResponse(); } }; // Fallback error handler const ErrorHandler = { canHandle() { return true; }, handle(handlerInput, error) { console.log(`Error caught: ${error.message}`); return handlerInput.responseBuilder .speak("Oops, something went wrong. Could you try that again?") .getResponse(); } }; // Export the Lambda handler exports.handler = Alexa.SkillBuilders.custom() .addRequestHandlers(PlayStationHandler) .addErrorHandlers(ErrorHandler) .lambda();
Key Details to Note
Station Catalog Matching:
- The
STATION_CATALOGobject is a stand-in for your JSON directory. You can easily replace it with a file import or an API call to fetch your live station list. - We use case-insensitive matching so users don't have to worry about exact capitalization when speaking the station name.
- The
Media Playback Directive:
- The
addAudioPlayerPlayDirectivetells Alexa to start streaming the URL.REPLACE_ALLensures any currently playing audio is stopped before starting the new station. - Make sure your station URL uses a format Alexa supports (MP3, AAC, etc.) and has CORS enabled so Alexa can access it.
- The
Alexa Console Setup:
- You'll need to create a
PlayStationIntentin the Alexa Developer Console, with aStationNameslot. Add all your station names as slot values (or use dynamic slots if your catalog updates often). - Sample utterances should include phrases like
play {StationName} on {SkillName}to map user speech to your intent.
- You'll need to create a
Quick Tips
- Test using the Alexa Developer Console's test tool or a physical Alexa device, and check Lambda CloudWatch logs to debug any issues.
- If your station catalog is large, store it in S3 or a database instead of hardcoding it in the Lambda function for easier updates.
内容的提问来源于stack exchange,提问作者eleventh
相关产品推荐
相关产品推荐

