Search code examples
javascriptnode.jsazurebotframeworkbots

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


I'm trying to pass a property, that is inside the first position of an array of objects, to another module so I can use this value later. I've tried to pass it as module(args), but it keeps reading the default value which is 0. Is there a way to do this?

I tried to implement some React.context but the Bot framework Emulator is refusing it.

/////////////////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 })


Solution

  • Since you solved your original problem, I'll answer the one in your comment.

    Your problem is here:

    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 contains the totals for each of your items. When you call map on it, you're passing each item individually to getTotals, which passes each item to checkout()

    The reason you can't sum all of the totals and can only sum one item's total is that you pass cartId to checkout and cartId has been changed to just a single item. Instead, there's a couple of different things you could do:

    1. Pass the whole cartId from cartItems and use something like for (var key in cartItems) in totalConstructor() and checkoutConstructor(). This is probably the easiest, but not very memory efficient.

    2. Use BotBuilder's State Storage to store your totals array in userData, then sum that at the end. This might be more difficult to implement, but would be a much better route to go. Here's a sample that can help you get started.