【问题标题】:How to create a `context.Provider`/`context.Consumer`-like structure to pass values in a bot app?如何创建类似于`context.Provider`/`context.Consumer`的结构来在机器人应用程序中传递值?
【发布时间】:2019-08-27 17:55:16
【问题描述】:

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

我尝试实现一些React.context,但 Bot 框架 Emulator 拒绝它。

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

【问题讨论】:

  • 你能解释一下发生了什么吗?默认值在哪里设置?错误发生在哪里?模拟器给出了什么错误?
  • 您可以共享的代码越多越好。我真的看不出您的 CheckOut 模块是如何被调用的,或者大部分代码是如何相互作用的。
  • 我已经用一种完全不同的策略解决了部分问题,现在这个值确实在传递,但在其他方面我必须根据触发的匹配创建另一个 bot.dialog()一个 postBack() ,其中将存储由 bot.dialog() 上的 results.response 捕获的给定值并将其推送到价格数组中,然后我从单个 cardBox 计算总数,但新问题是汇总所有被点击的 CardBox [GitHub] (github.com/VyTorBrB/botTalk)

标签: javascript node.js azure botframework bots


【解决方案1】:

既然你解决了你原来的问题,那我就回答你评论里的那个。

你的问题在这里:

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 传递给checkout,而cartId 已更改为仅一项。相反,您可以做一些不同的事情:

  1. cartItems 传递整个cartId 并在totalConstructor()checkoutConstructor() 中使用类似for (var key in cartItems) 的内容。这可能是最简单的,但内存效率不是很高。

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

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多