0

Please help me if any body resolved the issue and below is my lambda funciton code. when i comment all funcitons in exports.handler funciton and execute/test single function it is working but if we enable all funcitons then i am getting the error as Error handled: Cannot read property 'value' of undefined

Node JS code for alexa skill kit for different intents created.

GetNewFactHandler,
    CreateJIRAIssueHandler,
    UpdateJIRAIssueHandler,
    GetJIRAIssueHandler,
    GetJIRAIssueCountHandler,


/* eslint-disable  func-names */
/* eslint-disable  no-console */
var http = require('http');
var https = require('https');

var projectKey='';
var iType='';
var keyValueID='';
var userName='';
var IssueNumber='';

const Alexa = require('ask-sdk');

const GetNewFactHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest'
      || (request.type === 'IntentRequest'
        && request.intent.name === 'GetNewFactIntent');
  },
  handle(handlerInput) {
    const factArr = data;
    const factIndex = Math.floor(Math.random() * factArr.length);
    const randomFact = factArr[factIndex];
    const speechOutput = GET_FACT_MESSAGE + randomFact;

    return handlerInput.responseBuilder
      .speak(speechOutput)
      .withSimpleCard(SKILL_NAME, randomFact)
      .getResponse();
  },
};


var reqTimeout = 3000;

const CreateJIRAIssueHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        const proKey = request.intent.slots.PROJECTKEY.value;
        const issueType = request.intent.slots.ISSUETYPE.value;
        iType = capitalize_Words(issueType);
        projectKey = proKey.toUpperCase();
        return request.type === 'IntentRequest' &&
            request.intent.name === 'CreateJIRAIssue';
    },
    async handle(handlerInput) {
        const createIssue = await createJiraIssue(projectKey, iType);
        const speechOutput = JSON.parse(createIssue).key;
        return handlerInput.responseBuilder
            .speak('Issue ' + speechOutput + ' created')
            .getResponse();
    },
};

function capitalize_Words(str) {
    return str.replace(/\w\S*/g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
};

//mule real time server
var muleCreateIssuePath = '/jira/createissue';
var muleProtocol = 'https';
var createIssueMethod = 'POST';
var muleUpdateIssuePath = '/jira/issue/';
var updateIssueMethod = 'PUT';
var getIssueMethod = 'GET';

var MuleReqOpts = {
    host: '',
    port: 443,
    path: undefined, // To be set.
    method: undefined,
    headers: {
        'Content-Type': 'application/json'
    },
    agent: false,
    // auth: jiraUsername + ':' + jiraPassword
    auth: ''

};

var MuleHttp = muleProtocol === 'https' ? https : http;

function createJiraIssue(projectKey, iType) {

    MuleReqOpts.path = muleCreateIssuePath;
    MuleReqOpts.method = createIssueMethod;

    var MuleReqBody = {
        'fields': {
            'project': {
                'key': projectKey
            },
            'summary': 'Test Message',
            'description': 'Issue created for alexa skill kit from Integration alexa to jira',
            'issuetype': {
                'name': iType // Make sure your JIRA project configuration(s) supports this Issue Type.
            }
        }
    };

    return doApiCall(MuleHttp, MuleReqOpts, MuleReqBody, 'creating issue', 201);
};

function doApiCall(httplib, reqOpts, reqBody, happening, successCode) {
    return new Promise(((resolve, reject) => {
        var req = httplib.request(reqOpts, (res) => {
            res.setEncoding('utf8');
            let returnData = '';
            res.on('data', (chunk) => {
                returnData += chunk;
            });
            res.on('end', () => {
                resolve(returnData);
            });
            res.on('error', (error) => {
                reject(error);
            });
        });
        req.write(JSON.stringify(reqBody));
        req.end();

        req.on('error', function(err) {
            context.done(new Error(' request error: ' + err.message));
        });
        req.setTimeout(reqTimeout, function() {
            context.done(new Error(' request timeout after ' + reqTimeout + ' milliseconds.'));
        });
    }));
};

const UpdateJIRAIssueHandler = {

    canHandle(handlerInput) {
            console.log('1');

        const request = handlerInput.requestEnvelope.request;
                    console.log('2');

        userName = request.intent.slots.USER.value;

        var keyValue = request.intent.slots.PROJECTKEY.value;
        console.log('keyValue : ' + keyValue);

        keyValueID = keyValue.toUpperCase();
        IssueNumber = request.intent.slots.ISSUENUMBER.value;
        let INumber = Number(IssueNumber);
            console.log('IssueNumber value: '+INumber);

        console.log('key value id: ' + keyValueID);
        return request.type === 'IntentRequest' &&
            request.intent.name === 'UpdateJIRAIssue';
    },
    async handle(handlerInput) {
        const updateIssue = await updateJiraIssue(userName);
        console.log('updateIssue log: ' + updateIssue);

        const speechOutput = JSON.parse(updateIssue).responseMessage;
        console.log('speechOutput log: ' + speechOutput);
        return handlerInput.responseBuilder
            .speak(speechOutput)
            .getResponse();
    },
};


function updateJiraIssue(userName) {
    console.log('inside method: ' +userName);
    MuleReqOpts.method = updateIssueMethod;

    var postdata = {
        "fields": {
            "assignee": {
                "name": userName
            }
        }
    }

    return doApiUpdateCall(MuleHttp, MuleReqOpts, postdata, 'updating issue', 201);
};

function doApiUpdateCall(httplib, options, postdata, happening, successCode) {

    options.path = muleUpdateIssuePath + keyValueID +'-'+IssueNumber ;

    return new Promise(((resolve, reject) => {

        var req = httplib.request(options, (res) => {
            console.log(options);

            res.setEncoding('utf8');
            let returnData = '';
            res.on('data', (body) => {
                returnData += body;
                console.log(returnData);

            });
            res.on('end', () => {
                resolve(returnData);
            });
            console.log('1');
            console.log('Body: ');
        });

        req.on('error', function(e) {
            console.log('problem with request: ' + e.message);
        });

        var jirapostString = JSON.stringify(postdata);
        console.log(jirapostString);
        req.write(jirapostString);
        req.end();
    }));
};

const GetJIRAIssueHandler = {
    canHandle(handlerInput) {
        console.log('1');
        const request = handlerInput.requestEnvelope.request;
        var keyValue = request.intent.slots.Issue.value;
        console.log('keyValue: ' + keyValue);

        keyValueID = keyValue.toUpperCase();
        return request.type === 'IntentRequest' &&
            request.intent.name === 'GetJIRAIssue';
    },
    async handle(handlerInput) {
        const getIssue = await GetJIRAIssue();
        console.log('getIssue log: ' + getIssue);

        const assigneeName = JSON.parse(getIssue).fields.assignee.name;
        const key = JSON.parse(getIssue).fields.assignee.key;
        const reporterName = JSON.parse(getIssue).fields.reporter.name;
        const issueType = JSON.parse(getIssue).fields.issuetype.name;
        const projectName = JSON.parse(getIssue).fields.project.name;
        const summaryDetails = JSON.parse(getIssue).fields.summary;

        const speechOutput = 'Here are the issue details summary: Assignee : ' + assigneeName.concat(' ,reporter : ' + reporterName, ' ,Issue Key : ' + key, ' ,IssueType : ' + issueType, ' ,ProjectName : ' + projectName, ' ,Summary : ' + summaryDetails);

        console.log('speechOutput log: ' + speechOutput);

        return handlerInput.responseBuilder
            .speak(speechOutput)
            .getResponse();
    },
};


function GetJIRAIssue() {
    MuleReqOpts.method = getIssueMethod;

    return doApiGetIssueCall(MuleHttp, MuleReqOpts, 'get issue details', 201);

};

function doApiGetIssueCall(httplib, options, happening, successCode) {

    options.path = muleUpdateIssuePath + keyValueID;

    return new Promise(((resolve, reject) => {

        https.get(options, (res) => {
            console.log(options);

            res.setEncoding('utf8');
            let returnData = '';
            res.on('data', (body) => {
                returnData += body;
                console.log('response :', returnData);

            });
            res.on('end', () => {
                resolve(returnData);
            });
        });

    }));
};

const GetJIRAIssueCountHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        userName = request.intent.slots.USER.value;
        console.log('userName Value: ' + userName);
        const issueType = request.intent.slots.ISSUETYPE.value;
        iType = capitalize_Words(issueType);
        return request.type === 'IntentRequest' &&
            request.intent.name === 'GetJIRAIssueCount';
    },
    async handle(handlerInput) {
        const getIssueCount = await GetJIRAIssueCount();
        console.log('getIssue log: ' + getIssueCount);
        const total = JSON.parse(getIssueCount).total;

        const speechOutput = ('Here is the '+iType+' count details: ' +total );

        console.log('speechOutput log: ' + speechOutput);

        return handlerInput.responseBuilder
            .speak(speechOutput)
            .getResponse();
    },
};

function GetJIRAIssueCount() {
    MuleReqOpts.method = getIssueMethod;

    return doApiGetIssueCountCall(MuleHttp, MuleReqOpts, 'get issue count details', 201);

};

function doApiGetIssueCountCall(httplib, options, happening, successCode) {

    options.path = muleUpdateIssuePath + userName +'/'+iType;

    return new Promise(((resolve, reject) => {

        https.get(options, (res) => {
            console.log(options);

            res.setEncoding('utf8');
            let returnData = '';
            res.on('data', (body) => {
                returnData += body;
                console.log('response :', returnData);

            });
            res.on('end', () => {
                resolve(returnData);
            });
        });

    }));
};

const HelpHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'AMAZON.HelpIntent';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(HELP_MESSAGE)
      .reprompt(HELP_REPROMPT)
      .getResponse();
  },
};

const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && (request.intent.name === 'AMAZON.CancelIntent'
        || request.intent.name === 'AMAZON.StopIntent');
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(STOP_MESSAGE)
      .getResponse();
  },
};

const SessionEndedRequestHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'SessionEndedRequest';
  },
  handle(handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

    return handlerInput.responseBuilder.getResponse();
  },
};

const ErrorHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput, error) {
    console.log(`Error handled: ${error.message}`);

    return handlerInput.responseBuilder
      .speak('Sorry, an error occurred.')
      .reprompt('Sorry, an error occurred.')
      .getResponse();
  },
};

const SKILL_NAME = 'Space Facts';
const GET_FACT_MESSAGE = 'Here\'s your fact: ';
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';

const data = [
  'A year on Mercury is just 88 days long.',
  'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
  'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
  'On Mars, the Sun appears about half the size as it does on Earth.',
  'Earth is the only planet not named after a god.',
  'Jupiter has the shortest day of all the planets.',
  'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
  'The Sun contains 99.86% of the mass in the Solar System.',
  'The Sun is an almost perfect sphere.',
  'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
  'Saturn radiates two and a half times more energy into space than it receives from the sun.',
  'The temperature inside the Sun can reach 15 million degrees Celsius.',
  'The Moon is moving approximately 3.8 cm away from our planet every year.',
];

const skillBuilder = Alexa.SkillBuilders.custom();

exports.handler = skillBuilder
  .addRequestHandlers(
    GetNewFactHandler,
    CreateJIRAIssueHandler,
    UpdateJIRAIssueHandler,
    GetJIRAIssueHandler,
    GetJIRAIssueCountHandler,
    HelpHandler,
    ExitHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();
Raj
  • 1
  • 2
  • Lambda function does not execute the code even if there is an error on one line so check if there are any typo error's and could you also specify at what intent is skill failing. – sharan kenam Jan 07 '20 at 06:24

2 Answers2

1

Check all of your code for instances where you are looking for someObject.someNestedProperty.value.

I suspect that your slot values in your Intents are not the same as defined in your model/some-LANG.json file. Trying to access value property from someObject without a someNestedProperty will cause your error (i.e. request.intent.slots.PROJECTKEY.value).

If this is a Alexa-hosted skill, CloudWatch logging is built right in. Open the Alexa Developer Console, edit your skill, then go to the Code tab. On the bottom left, you should see a link for Logs: Amazon CloudWatch. You can log anything you want with a simple console.log() statement.

Also, be aware that the ask-sdk-core npm package contains helper methods to get slotValues (among other great tools). For more information, see this link: https://developer.amazon.com/en-US/docs/alexa/alexa-skills-kit-sdk-for-nodejs/utilities.html

0

Thanks all for your suggestions and prompt response. Now the issue is resolved and the solution is the addRequestHandlers method will execute methods sequentially even if you execute/trigger third handler it will execute 1st and 2nd and goes to 3rd one. Here the issue is when you trigger a 3rd handler command from alexa it will passs the slot values related to the 3rd handler utterance and hence the 1st and 2nd haldler having slot values are not able to resolve and failed. so i put a condition overthere to skip 1st and 2nd handler execution and it worked.

Raj
  • 1
  • 2