Search code examples
javascriptpythonjsonalexa-skills-kit

How can I implement code into an Alexa Skill?


I'm running into significant problems understanding how to turn my Python or JS code into a skill for Alexa. I have code written in Python and half written in JS, but no matter what I try, I can't seem to implement it into an Alexa skill.

I've looked through so many tutorials, examples, and walkthroughs now that I'm starting to think that Alexa can't run a short code to determine her response. It seems like every single tutorial is for a facts skill which pulls from an API, which is not what I'm looking to do. At least, I don't think so - I'm still learning.

I've written the code itself in VSCode, but I've been putting as much of the skill together in Amazon Developer Panel as possible, but I'm to a point where I need to calculate how many days have passed since the start date, get the modelo, and output the chores on the revolving four-day list. So far as I can tell, that isn't something I can do without writing a function or two, but everything seems to be a class in JSON. Is that all Alexa uses?

I feel like I've somehow missed something really important in my education so far, or perhaps I'm completely misunderstanding Alexa skills, but I desperately need help to make sense of this.

For Example, if I have this portion of the JSON code:

    const ChoresIntentHandler = {
canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ChoresIntent';
},
handle(handlerInput) {
    const speakOutput = 'Oh, you know. Stuff.';

    return handlerInput.responseBuilder
        .speak(speakOutput)
        //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
        .getResponse();
}
    };

Say I want to implement this code:

    var one_day = 1000 * 60 * 60 * 24

    const measure_from = new Date('2022-04-08');
    const today = new Date();
    const diff_in_days = Math.floor((today - measure_from) / one_day) - 1 ;

Where on earth do I put it?

Thank you in advance. I've been trying to figure this out for over a month and I'm out of ideas.


Solution

  • Short answer:

    Anywhere really, it just depends on your logic.

    In that case, if you want this part of code to run after the ChoresIntent is invoked, then within the handle function.

    Long answer:

    This is the official tutorial you will need to understand how to build an alexa skill from scratch and will cover 99% of your actual & future questions

    Regarding your question, you need to understand that this is how Alexa SDK is designed:

    • One Intent = One user interaction
    • One Intent has 2 function:
      • canHandle, a simple condition from the request to determine if it should call the handle function
      • handle, the function executed if it matches the canHandle function.

    You also have the possibility to run a piece of code after each request. You have what is called interceptors, that allow you to execute code just after receiving the response (to allow your intent to have data fetch from a database) or you can do it before replying the request to the user, after the intent.

    In any case, I recommend you to follow the tutorial carefully, it will explain how you can create a skill on Alexa, but it will not teach you the basic of JS or Python required.