简体   繁体   English

带有Yummly API的AWS Lambda Alexa Intent中的异步功能

[英]Async Function in AWS Lambda Alexa Intent with Yummly API

I'm trying to retrieve data from the Yummly API through Amazon Alexa using ws-yummly in Node.js deployed on AWS Lambda. 我正在尝试使用AWS Lambda上部署的Node.js中的ws-yummly通过Amazon Alexa从Yummly API检索数据。 I'm fairly new to all aspects of this, but new to javascript in particular (Python is my 'native' language). 我在这方面的各个方面都比较陌生,但是对于javascript来说尤其陌生(Python是我的“母语”语言)。

Here is what I have for my recommendation intent: 这是我的推荐意图:

"RecommendationIntent": function () {
        // delegate to Alexa to collect all the required slots

        let filledSlots = delegateSlotCollection.call(this);

        if (!filledSlots) {
            return;
        }

        console.log("filled slots: " + JSON.stringify(filledSlots));
        // at this point, we know that all required slots are filled.
        let slotValues = getSlotValues(filledSlots);

        console.log(JSON.stringify(slotValues));

        const mainIngredientQuery = slotValues.mainIngredient.resolved;

                async function main (queryWord) {
                    const resp = await Yummly.query(queryWord)
                        .maxTotalTimeInSeconds(1400)
                        .maxResults(20)
                        .minRating(3)
                        .get();
                    const names = resp.matches.map(recipe => recipe.recipeName);
                    const speechOutput = String(names[0]);
                        this.response.speak(speechOutput);
                        this.emit(":responseReady");
                }
                main(mainIngredientQuery).catch(error => console.error(error))
    },

This is in the index.js file deployed on AWS lambda. 这位于部署在AWS lambda上的index.js文件中。 I have isolated the problem to the async function. 我已将问题隔离到异步功能。 I have tested the function locally and it returns to console.log a list of recipe names. 我已经在本地测试了该功能,它返回到console.log配方名称列表。 I want to have Alexa say these names. 我想让Alexa说这些名字。 Or at least one name. 或至少一个名字。

If I put the speechOutput assignment inside (as it is now), then I get an error that the 'Speechlet Response is set to null'. 如果将语音输出分配放入其中(如现在),则会收到“语音响应设置为null”的错误消息。

If I tell it to 'return names' and set the external assignment to names or names[0] I get object promise or undefined (respectively). 如果我告诉它“返回名称”并将外部分配设置为名称或名称[0],则会分别得到对象承诺或未定义。

Everything else in my program works fine and test these two bits apart they work, but putting them together doesn't work. 我程序中的其他所有内容都可以正常工作,并分开测试它们是否可以正常工作,但是将它们放在一起不起作用。 I think that this is a syntax or placement error, but I don't understand the structure or formatting well enough yet (still learning) to understand what to try next. 我认为这是语法或布局错误,但我还不了解其结构或格式(仍然在学习),不足以理解下一步该怎么做。

How about using Promise.then like this: 如何使用Promise.then像这样:

async function main (queryWord) {
  const resp = await Yummly.query(queryWord)
                    .maxTotalTimeInSeconds(1400)
                    .maxResults(20)
                    .minRating(3)
                    .get();
  const names = resp.matches.map(recipe => recipe.recipeName);

  return String(names[0]);

}

main(mainIngredientQuery)
  .catch( error => console.error(error) )
  .then( data => {

    this.response.speak( data );
    this.emit(":responseReady");

});

I'm updating this in case anyone else has the same problem. 如果有人遇到同样的问题,我将进行更新。

If you notice, in my original code, I had an async function inside the intent. 如果您发现,在我的原始代码中,我在意图内有一个异步函数。 That didnt work because the intent itself was/is a function. 那没有用,因为意图本身是一个功能。 By making the intent function an async function instead, I was able to solve the problem. 通过使intent函数成为异步函数,我得以解决该问题。

The following is working code for an async/await Alexa Intent. 以下是异步/等待Alexa Intent的工作代码。

The full index.js is available on my github if you want to take a look, but that will be a more advanced final version. 如果您想看一看完整的index.js,可以在我的github上找到,但这将是一个更高级的最终版本。 The code below immediately follows up on the original question. 下面的代码可立即跟进原始问题。

"RecommendationIntent": async function main () {
    // delegate to Alexa to collect all the required slots

    let filledSlots = delegateSlotCollection.call(this);

    if (!filledSlots) {
        return;
    }

    console.log("filled slots: " + JSON.stringify(filledSlots));
    // at this point, we know that all required slots are filled.
    let slotValues = getSlotValues(filledSlots);

    console.log(JSON.stringify(slotValues));

    const mainIngredientQuery = slotValues.mainIngredient.resolved;
            const resp = await Yummly.query('chicken')
            .maxTotalTimeInSeconds(1400)
            .maxResults(20)
            .minRating(3)
            .get();
        const names = resp.matches.map(recipe => recipe.recipeName);
        console.log(names);
            const speechOutput = names[0];
            this.response.speak(speechOutput);
            this.emit(":responseReady");
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM