[英]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?
我试图将其作为module(args)传递,但它一直在读取默认值0。有没有办法做到这一点?
I tried to implement some React.context
but the Bot framework Emulator is refusing it. 我试图实现一些
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 })
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. cartId
包含每个项目的总计。 When you call map
on it, you're passing each item individually to getTotals
, which passes each item to checkout()
当您在其上调用
map
时,您将每个项目分别传递给getTotals
,后者将每个项目传递给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. 您不能将所有总数相加,而只能将一项总数相加的原因是,您将
cartId
传递给checkout
而cartId
已更改为仅一项。 Instead, there's a couple of different things you could do: 相反,您可以执行几项不同的操作:
Pass the whole cartId
from cartItems
and use something like for (var key in cartItems)
in totalConstructor()
and checkoutConstructor()
. 从
cartItems
传递整个cartId
,并在totalConstructor()
和checkoutConstructor()
使用诸如for (var key in cartItems)
的totalConstructor()
。 This is probably the easiest, but not very memory efficient. 这可能是最简单的,但不是很高效的内存。
Use BotBuilder's State Storage to store your totals
array in userData
, then sum that at the end. 使用BotBuilder的状态存储将
totals
数组存储在userData
,然后对其求和。 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.
这是一个可以帮助您入门的示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.