Можно ли поместить отдельную функцию экспорта в Alexa Skill? - PullRequest
1 голос
/ 10 апреля 2019

Я очень новичок в программировании и пытаюсь учиться, поэтому, пожалуйста, потерпите меня.

Я пытаюсь создать базовый навык Alexa, который:

  • задает несколько вопросов
  • Затем отправляет электронное письмо с небольшой информацией, собранной из навыка Alexa.через AWS-Lambda с использованием AWS-SES.

Моя главная проблема заключается в том, чтобы найти нехватку информации и рекомендаций о том, как объединить эти два понятия, чтобы заставить меня работать.

Можно ли смешать два и добиться того, к чему я стремлюсь?Любая помощь приветствуется.


Эта лямбда-функция сама по себе работает и будет отправлять электронные письма, принимая в качестве ввода следующее тело запроса:

{
  "SubCategory": "General",
  "MainCategory": "Problem",
  "Email": "email@email.com"
}

Это лямбда-функция:

var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-1'});
var toEmail = "email@email.com";

exports.handler = function(event, context) {
    console.log("Incoming: ", event);
    var eParams = {
        Destination: {
            ToAddresses: [toEmail]
        },
        Message: {
            Body: {
                Text: {
                    Data: event.SubCategory
                }
            },
            Subject: {
                Data: event.MainCategory
            }
        },
        Source: event.Email
    };
    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);
            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);
        }
    }); 
};

А это функция Alexa Lambda:

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

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speechText = 'Hi there, looks like you need some technical support. Lets get started. Are you having a problem or do you have a question?';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const ProblemIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'ProblemIntent';
    },
    handle(handlerInput) {
        const speechText = 'Which of these categories does your problem fall into? General, Network, or Security?';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const QuestionIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'QuestionIntent';
    },
    handle(handlerInput) {
        const speechText = 'Which of these categories does your question fall into? Software Development, Business Consulting, or virtual C.I.O. engagement? ';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const GeneralIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'GeneralIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a General Problem then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const NetworkIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'NetworkIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a Network Problem then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const SecurityIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'SecurityIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a Security Problem then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const VirtualCioIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'VirtualCioIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a Virtual C.I.O. question then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const BusinessConsultingIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'BusinessConsultingIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a Business Consulting question then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const SoftwareDevelopmentIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'SoftwareDevelopmentIntent';
    },
    handle(handlerInput) {
        const speechText = 'Looks like you have a Software Development question then, lets go ahead and notify the helpdesk. They should reach out to you soon.';
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const HelpIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speechText = 'Please say either Problem or Question.';

        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
                || handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speechText = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speechText)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = handlerInput.requestEnvelope.request.intent.name;
        const speechText = `You just triggered ${intentName}`;

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

const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.message}`);
        const speechText = `Sorry, I couldn't understand what you said. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};


exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        ProblemIntentHandler,
        QuestionIntentHandler,
        GeneralIntentHandler,
        NetworkIntentHandler,
        SecurityIntentHandler,
        VirtualCioIntentHandler,
        BusinessConsultingIntentHandler,
        SoftwareDevelopmentIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler) 
    .addErrorHandlers(
        ErrorHandler)
    .lambda();
...