首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Alexa技能:如何从模板中调用自定义意图?

Alexa技能:如何从模板中调用自定义意图?
EN

Stack Overflow用户
提问于 2022-11-10 09:46:38
回答 1查看 33关注 0票数 0

我试图创建一个alexa技能的帮助下,“调查”模板,使用个性化+语音识别的基础上。

个性化+基于语音识别的auth工作良好。我增加了一个新的意图‘介绍’,需要触发的基础上的话语-‘介绍’,但这是不起作用的预期。

  1. 打开我的机器人-> (欢迎来自alexa)
  2. 让我们开始(调用StartMyStandupIntentHandler意图和基于语音id的auth ) ->你好,有什么可以帮助您的吗?
  3. introduce ->不调用IntroduceHandler,但是我有一个IntentReflectorHandler,它说:您刚刚触发了导入意图。您正在听到这个响应,因为introduce还没有一个意图处理程序。

index.js:

代码语言:javascript
运行
复制
const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
    const requestAttributes = handlerInput.attributesManager.getRequestAttributes();

    const skillName = requestAttributes.t('SKILL_NAME');
    const name = personalization.getPersonalizedPrompt(handlerInput);
    var speakOutput = ""
    if (name && name.length > 0) {
        speakOutput = requestAttributes.t('GREETING_PERSONALIZED', skillName);
    } else {
        speakOutput = requestAttributes.t('GREETING', skillName);
    }
    const repromptOutput = requestAttributes.t('GREETING_REPROMPT');

    return handlerInput.responseBuilder
      .speak(speakOutput)
      .reprompt(repromptOutput)
      .getResponse();
  },
};


const StartMyStandupIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'StartMyStandupIntent';
  },
  async handle(handlerInput) {
    const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
    let speakOutput;
    const name = personalization.getPersonalizedPrompt(handlerInput);
    let response = handlerInput.responseBuilder;
    if (name && name.length > 0) {
        speakOutput = 'Hello '+ name +'! How can i help you?';
        const upsServiceClient = handlerInput.serviceClientFactory.getUpsServiceClient();
        let profileName
        let profileEmail
        try {
            profileName = await upsServiceClient.getPersonsProfileGivenName();
            profileEmail = await upsServiceClient.getProfileEmail();
        } catch (error) {
            return handleError(error, handlerInput)
        }

        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        sessionAttributes.userEmail = profileEmail;
        sessionAttributes.userName = profileName;
        handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
    } else {
      speakOutput = requestAttributes.t('PERSONALIZED_FALLBACK')
    }
    return response
      .speak(speakOutput)
      .withShouldEndSession(false)
      .getResponse()
  },
};

const Introduce_Handler  =  {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'Introduce'
    },
     handle(handlerInput) {
        const responseBuilder = handlerInput.responseBuilder;

        let say = 'Hi everyone, As the famous saying goes,  \'The human voice is the most perfect instrument of all\', . ';
        say += 'A very Warm greetings to all. ';

        return responseBuilder
            .speak(say)
            .withShouldEndSession(false)
            .getResponse();
    },
};

/**
 * Voice consent request - response is handled via Skill Connections.
 * Hence we need to handle async response from the Voice Consent.
 * The user could have accepted or rejected or skipped the voice consent request.
 * Create your custom callBackFunction to handle accepted flow - in this case its handling identifying the person
 * The rejected/skipped default handling is taken care by the library.
 *
 * @params handlerInput - the handlerInput received from the IntentRequest
 * @returns
 **/
async function handleCallBackForVoiceConsentAccepted(handlerInput) {
    const upsServiceClient = handlerInput.serviceClientFactory.getUpsServiceClient();
    let profileName = await upsServiceClient.getProfileEmail();
    let profileEmail = await upsServiceClient.getPersonsProfileGivenName();
    const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
    sessionAttributes.userEmail = profileEmail;
    sessionAttributes.userName = profileName;
    handlerInput.attributesManager.setSessionAttributes(sessionAttributes);

    // this is done because currently intent chaining is not supported from any
    // Skill Connections requests, such as SessionResumedRequest.
    const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
    const name = personalization.getPersonalizedPrompt(handlerInput);
    let speakOutput = 'Hello '+ name +'! How can i help you?';
    //let repromptOutput = requestAttributes.t('ABOUT_REPROMPT');

    let response = handlerInput.responseBuilder;
    return response
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse()
}

交互模型json:

代码语言:javascript
运行
复制
{
    "interactionModel": {
        "languageModel": {
            "invocationName": "my bot",
            "modelConfiguration": {
                "fallbackIntentSensitivity": {
                    "level": "LOW"
                }
            },
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "GetCodeIntent",
                    "slots": [
                        {
                            "name": "MeetingCode",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "My code is {MeetingCode}",
                        "The code is {MeetingCode}",
                        "{MeetingCode}"
                    ]
                },
                {
                    "name": "GetReportIntent",
                    "slots": [
                        {
                            "name": "questionYesterday",
                            "type": "AMAZON.SearchQuery",
                            "samples": [
                                "{questionYesterday}"
                            ]
                        },
                        {
                            "name": "questionToday",
                            "type": "AMAZON.SearchQuery",
                            "samples": [
                                "{questionToday}"
                            ]
                        },
                        {
                            "name": "questionBlocking",
                            "type": "AMAZON.SearchQuery",
                            "samples": [
                                "{questionBlocking}"
                            ]
                        }
                    ],
                    "samples": [
                        "{questionToday} today",
                        "{questionYesterday} yesterday",
                        "yesterday {questionYesterday}",
                        "today {questionToday}"
                    ]
                },
                {
                    "name": "AMAZON.FallbackIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.YesIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NoIntent",
                    "samples": []
                },
                {
                    "name": "ResetPinIntent",
                    "slots": [],
                    "samples": [
                        "where do i get a pin",
                        "what is my pin",
                        "how do i get a pin",
                        "i need a new pin",
                        "i forgot my pin"
                    ]
                },
                {
                    "name": "StartMyStandupIntent",
                    "slots": [],
                    "samples": [
                        "yes let's get started",
                        "yes let's begin",
                        "let's begin",
                        "let's get started"
                    ]
                },
                {
                    "name": "Introduce",
                    "slots": [],
                    "samples": [
                        "introduce yourself",
                        "introduce",
                        "intro"
                    ]
                }
            ],
            "types": []
        },
        "dialog": {
            "intents": [
                {
                    "name": "GetReportIntent",
                    "delegationStrategy": "ALWAYS",
                    "confirmationRequired": false,
                    "prompts": {},
                    "slots": [
                        {
                            "name": "questionYesterday",
                            "type": "AMAZON.SearchQuery",
                            "confirmationRequired": false,
                            "elicitationRequired": true,
                            "prompts": {
                                "elicitation": "Elicit.Slot.420907304064.1434077833163"
                            }
                        },
                        {
                            "name": "questionToday",
                            "type": "AMAZON.SearchQuery",
                            "confirmationRequired": false,
                            "elicitationRequired": true,
                            "prompts": {
                                "elicitation": "Elicit.Slot.173201382582.539843571833"
                            }
                        },
                        {
                            "name": "questionBlocking",
                            "type": "AMAZON.SearchQuery",
                            "confirmationRequired": false,
                            "elicitationRequired": true,
                            "prompts": {
                                "elicitation": "Elicit.Slot.173201382582.1204298947985"
                            }
                        }
                    ]
                }
            ],
            "delegationStrategy": "ALWAYS"
        },
        "prompts": [
            {
                "id": "Elicit.Slot.288779318596.409557698368",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "Alright, first question. What did you do yesterday?"
                    }
                ]
            },
            {
                "id": "Elicit.Slot.288779318596.1420775370020",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "Got it. What will you do today?"
                    }
                ]
            },
            {
                "id": "Elicit.Slot.288779318596.88143460540",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "Okay, last question. Is there anything blocking your progress?"
                    }
                ]
            },
            {
                "id": "Elicit.Slot.420907304064.1434077833163",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "What did you work on yesterday?"
                    }
                ]
            },
            {
                "id": "Elicit.Slot.173201382582.539843571833",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "What will you work on today?"
                    }
                ]
            },
            {
                "id": "Elicit.Slot.173201382582.1204298947985",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "What if anything is blocking your progress?"
                    }
                ]
            }
        ]
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-22 07:56:56

我怀疑你错过了将意图处理程序添加到技能对象中。从Alexa文件中看到这个片段

代码语言:javascript
运行
复制
let skill;

exports.handler = async function (event, context) {
  console.log(`REQUEST++++${JSON.stringify(event)}`);
  if (!skill) {
    skill = Alexa.SkillBuilders.custom()
      .addRequestHandlers(
        LaunchRequestHandler,
        StartMyStandupIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
      )
      .addErrorHandlers(ErrorHandler)
      .create();
  }

  const response = await skill.invoke(event, context);
  console.log(`RESPONSE++++${JSON.stringify(response)}`);

  return response;
};

需要将Introduce_Handler添加到addRequestHandlers方法调用中。另外,确保在意图反射器处理程序之前添加。ASK将根据添加到技能对象的顺序确定使用哪个处理程序的优先级。您的代码可能如下所示:

代码语言:javascript
运行
复制
.addRequestHandlers(
        LaunchRequestHandler,
        AskWeatherIntentHandler,
        Introduce_Handler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler 
      )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74387010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档