Pass along sign in credentials when enabling skill through ask-smapi-sdk

141 views Asked by At

I'm doing utterance and other automated testing in my build/deploy flow for my Alexa skill. Now I also want to use the simulate API (https://developer.amazon.com/en-US/docs/alexa/smapi/skill-simulation-api.html) to do entire conversational flows but I get stuck immediately. My skill uses account linking so somehow I need to pass along credentials for a user. In the documentation I can't see any way to pass along any other arguments than the session id, locale and utterance. Should I somehow use the LWA profile to sign in to my service and in that case, how would I do that (since it's only client id and client secret, no email/password)?

This is how I have setup the javascript code for my LWA profile (that works for all requests besides the simulate API and invocation API).

const ASK = require("ask-smapi-sdk");
const refreshTokenConfig = {
  clientId: "amzn1.application-oa2-client.xxx",
  clientSecret: "xxx",
  refreshToken: "Atzr|xxx",
};
const smapiClient = new ASK.StandardSmapiClientBuilder()
  .withRefreshTokenConfig(refreshTokenConfig)
  .client();

If I would call this skill now using the simulate API a request/response sample would look something like this.

await smapiClient.setSkillEnablementV1(skillId, "development");
const response = await smapiClient.simulateSkillV2(skillId, "development", {
  device: {
    locale: "en-US",
  },
  input: {
    content: "Alexa, ask mySkill to do something",
  },
  session: {
    mode: "FORCE_NEW_SESSION",
  },
});

response would now contain this section in the response json.

body: {
  version: "1.0",
  response: {
    outputSpeech: {
      type: "SSML",
      ssml: "<speak>Hi, to use mySkill, please go to your Alexa app and link your mySkill account.</speak>",
    },
    card: { type: "LinkAccount" },
      shouldEndSession: true,
      type: "_DEFAULT_RESPONSE",
    },
    sessionAttributes: {
      ...
    },
    userAgent: "ask-node/2.10.1 Node/v12.19.0",
  },
},

To repeat my question, how can I sign in a user programmatically, should I use my LWA profile somehow or is there another process?

Thanks!

1

There are 1 answers

0
Ahmad S On

I have two classes made for this to make my life easier

skillsimulation:

const fetch = require('node-fetch');

class simulationId{


  constructor(){

    this.token = process.env.TOKEN
  }
  
  async requestSimulationId(utterance, skillId, locale){

    var requestBody = 
    {
      "session": {
        "mode": "FORCE_NEW_SESSION"
      },
      "input": {
        "content": utterance
      },
      "device": {
        "locale": locale
      }
    }

    const rawResponse = await fetch(skillId, {
    
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': this.token
    },
    body: JSON.stringify(requestBody)
  });


  const buildResponse = rawResponse.json();

  return buildResponse;

}

}

module.exports = simulationId;

simualtion response:

const fetch = require('node-fetch');

class simulationResponse{

  
  constructor(){

    this.token = process.env.TOKEN
  
  }

  async requestSimulationResponse(simulationId, skillId){

    const rawResponse = await fetch(skillId+simulationId.id, { 
      method: 'GET',
      headers:{
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': this.token
      }});


    const buildResponse = rawResponse.json();
    
    return buildResponse;


  }

}

module.exports = simulationResponse;