簡體   English   中英

如何創建類似“ context.Provider” /“ context.Consumer”的結構以在機器人應用中傳遞值?

[英]How to create a `context.Provider`/`context.Consumer`-like structure to pass values in a bot app?

我正在嘗試將一個對象數組的第一個位置內的屬性傳遞給另一個模塊,以便以后可以使用該值。 我試圖將其作為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()使用諸如for (var key in cartItems)totalConstructor() 這可能是最簡單的,但不是很高效的內存。

  2. 使用BotBuilder的狀態存儲totals數組存儲在userData ,然后對其求和。 這可能更難以實現,但是將是一條更好的選擇。 這是一個可以幫助您入門的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM