You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求开发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:

  1. Extract the station name from the request
  2. Match it against your JSON catalog to get the streaming URL
  3. Build a response that tells Alexa to play the stream
  4. 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
  1. Station Catalog Matching:

    • The STATION_CATALOG object 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.
  2. Media Playback Directive:

    • The addAudioPlayerPlayDirective tells Alexa to start streaming the URL. REPLACE_ALL ensures 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.
  3. Alexa Console Setup:

    • You'll need to create a PlayStationIntent in the Alexa Developer Console, with a StationName slot. 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.
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:34:53