显示列表模板

时间:2019-07-03 06:57:20

标签: alexa-skills-kit

我正在尝试实现Alexa技能套件的列表模板功能。但是,我无法以适当的格式返回响应。

我已经使用官方文档实现了该功能。但是,我没有得到如何将列表模板的响应返回到我的自定义意图

'ListTemplate':function(){

    var title = "This is a sample list";
    var speechOutput = "Showing the sample list";

    var template = {

        "type":"Display.RenderTemplate",
        "template":{
            "type":"ListTemplate1",
            "token":"ListTemplate",
            "title":title,
            "backButton":"VISIBLE",
            "backgroundImage":{
                "contentDescription":"backgroundImage",
                "sources":[
                    {
                        "url":"https://democard.s3.amazonaws.com/hostel-720.jpg"
                    }]
            },

            "listItems":[{
                "token":"item1",
                "image":{
                    "sources":[{
                        "url":"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
                    }],
                    "contentDescription":"first item of list"
                },
                "textContent":{
                    "primaryText":{
                        "type":"PlainText",
                        "text":"primary Text is here"
                    },
                    "secondaryText":{
                        "type":"PlainText",
                        "text":"Secondary text is here"
                    }

                },
            },

            {
                "token":"item2",
                "image":{
                    "sources":[{
                        "url":"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"

                }],
                "contentDescription":"second item"

            },
            "textContent":{
                "primaryText":{
                    "type":"PlainText",
                    "text":"primary text is here"
                },
                "secondaryText":{
                    "type":"PlainText",
                    "text":"secondary text"
                }
            }
            }
        ]
        }};




    var directives =[ template ];

    //return build_speechlet_response(title,speechOutput,directives, SESSION_LIST);

//功能

build_speechlet_response(title,speechOutput,directives,phase){

    const response = {
         "version": "1.0",
         "response": {
             "outputSpeech":{
                 "type":"PlainText",
                 "text":"what else would you like to see"

             },
             "card":{
              'type':'Simple',
              'title':title,
              'content':speechOutput
             },
             "directives":directives,
             "shouldEndSession":'False'
         },
         "sessionAttributes":{
             "template":"list_"
         }

     };

    // return response;

         this.emit(':tell',response);


},

我应该得到的答复必须是自定义列表。但是我不明白

2 个答案:

答案 0 :(得分:0)

看起来这个问题是response是一个对象。它应该类似于this.emit(':tell', speechOutput)(其中speechOutput是一个字符串)。

如果您还想发送卡,则为this.emit(':tellWithCard', speechOutput, cardTitle, cardContent, imageObj)

但是,由于您尝试使用渲染模板,因此将类似于:

this.response.speak(speechOutput)
  .cardRenderer(cardTitle, cardContent, cardImage)
  .renderTemplate(template);

this.emit(':responseReady');

您可以在此处找到更多信息-https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/tree/1.x

我确实注意到您正在使用SDK的v1-我真的建议您使用v2,因为它更直接。

https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs

希望有帮助。

答案 1 :(得分:0)

I tried the following code yet the response was not rendered. 

const DisplayListIntentHandler = {
    canHandle(handlerInput){
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'DisplayList';
    },

    handle(handlerInput){
        var title = "This is a sample list";
        var speechOutput = "Showing the sample list";

        var template = {

        type:'Display.RenderTemplate',
        template:{
            type:"ListTemplate1",
            token:"ListTemplate",
            title:'title',
            backButton:"VISIBLE",
            backgroundImage:{
                contentDescription:"backgroundImage",
                sources:[
                    {
                        url:"https://democard.s3.amazonaws.com/hostel-720.jpg"
                    }]
            },

            listItems:[{
                token:"item1",
                image:{
                    sources:[{
                        url:"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
                    }],
                    contentDescription:"first item of list"
                },
                textContent:{
                    primaryText:{
                        type:"PlainText",
                        text:"primary Text is here"
                    },
                    secondaryText:{
                        type:"PlainText",
                        text:"Secondary text is here"
                    }

                },
            },

            {
                token:"item2",
                image:{
                    sources:[{
                        url:"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"

                }],
                contentDescription:"second item"

            },
            textContent:{
                primaryText:{
                    type:"PlainText",
                    text:"primary text is here"
                },
                secondaryText:{
                    type:"PlainText",
                    text:"secondary text"
                }
            }
            }
        ]
        }};

        return handlerInput.responseBuilder
        .addRenderTemplateDirective(template)
        .getResponse();

    }
};