javascript - 如何创建类似 `context.Provider`/`context.Consumer` 的结构来在机器人应用程序中传递值?

标签 javascript node.js azure botframework bots

我正在尝试将对象数组的第一个位置内的属性传递给另一个模块,以便稍后可以使用该值。我尝试将它作为 module(args) 传递,但它一直读取默认值 0。有没有办法做到这一点?

我尝试实现一些 React.context 但 Bot 框架模拟器拒绝它。

/////////////////Module that ll acquire the value/////////////////////////////
    getCard(bot, builder, params) {
        let configValues = { ...params[0] }

        bot.dialog(`${configValues.path}`, function (session) {
            var msg = new builder.Message(session);

            const cardItem = (obj) => {
                return (new builder.HeroCard(session)
                    .title(`${obj.title}`)
                    .text(`R$ ${obj.price}`)
                    .images([builder.CardImage.create(session, `${obj.img}`)])
                    .buttons([
                        builder.CardAction.imBack(session, `${obj.price} Item adicionado!`, 'add to cart')
                        // !onClick event must add the current obj.price to 
                        // the configValues.total(Ex: configValues.total += obj.price)!
                    ])
                )
            }
            msg.attachmentLayout(builder.AttachmentLayout.carousel)
            msg.attachments(
                eval(params.map(obj => cardItem(obj)))
            );
            //!in here before end the dialog is where i want to update
// the configValues.total so i can show it in the -> Checkout module
            session.send(msg).endDialog()
        }).triggerAction({ matches: configValues.regex });
    }
}
//////////////CheckOut.Module///////////////////////////////
{...}
let configValues = { ...params[0] }
    let state = {
        nome: "",
        endereco: "",
        pagamento: "",
        total: configValues.total // this is the value to be read
    }

    bot.dialog('/intent', [
        {...},
        (session, results) => {
            state.pagamento = results.response
            session.send(
                JSON.stringify(state) // here is the place to be printed
            )
        {...}   
    ]
    ).triggerAction({ matches: /^(finalizar|checar|encerrar|confirmar pedido|terminar)/i })

最佳答案

既然您解决了最初的问题,我将回答您评论中的问题。

您的问题在这里:

cartId.map((obj, i , arr) => {
            // if (!obj.total) {
            //     obj.total.reduce(i => i += i)
            // }
            const newtotal = new total
            newtotal.getTotals(bot, builder, obj, arr)
        })

cartId 包含每件商品的总计。当您对其调用 map 时,您会将每个项目单独传递给 getTotals,后者将每个项目传递给 checkout()

您无法对所有总计进行求和且只能对一项的总计进行求和的原因是您将 cartId 传递给 checkoutcartId 已更改为仅单个项目。相反,您可以执行以下几项不同的操作:

  1. cartItems 传递整个 cartId 并在 totalConstructor( )checkoutConstructor()。这可能是最简单的,但内存效率不高。

  2. 使用BotBuilder's State Storagetotals 数组存储在 userData 中,然后在最后求和。这可能更难实现,但会是一条更好的路线。 Here's a sample这可以帮助您入门。

关于javascript - 如何创建类似 `context.Provider`/`context.Consumer` 的结构来在机器人应用程序中传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541982/

相关文章:

javascript - jquery - 如何使用 '.first()' 之后选择元素?

javascript - 为什么我收到 ReferenceError : Response is not defined?

javascript - 等待所有回调完成执行,没有静态时间

node.js - Azure blockBlobURL.download() 对文件大小有限制吗?

javascript - 在隐藏表单输入中附加文件名

javascript - 在 KnockoutJs 中对可观察数组进行排序时 UI 未更新

php - PHP 中的 NodeJS 缓冲区等效项

javascript - 如何使用 javascript 函数返回 Node 模块中的对象

azure - 使用 Linq 的 Select 查询 DocumentDB 的属性列表

asp.net - 配置 Azure Web 和 SQL 英国文化