【问题标题】:How to save MS bot state data and conversations ?如何保存 MS bot 状态数据和对话?
【发布时间】:2018-03-25 13:04:29
【问题描述】:

我正在使用 node js 和 Microsoft Bot builder sdk 来编写 BOT。目前,我将所有内容保存在数据包(conversationData、userData 等)中,并使用 CosmosDb 来存储状态。我按照这篇文章配置 CosmosDB 并根据 nodejs 进行了更改。 https://azure.microsoft.com/en-in/blog/bot-conversation-history-with-azure-cosmos-db/

这种方法几乎没有问题,

  1. 当我们在对话框中调用 endConversation() 时,conversationData 包被清除。这是 sdk 设计所期望的,但我们希望为具有相同用户(相同对话 id)的多个对话流保留此数据。现在,当用户开始新意图时,db 中的 json cosmosDb 将替换为 conversationData 上的新键。 例如:在 {place} 安排与 {name} 的 {day} 会议。 我们保存了 conversationData.name、conversationData.day 和 conversationData。地方。 同一用户在 {place2} 重新安排与 {name2} 的 {day2} 会议。 documentDb 条目被替换为 conversationData.name1 、 conversationData.day2 和 conversationData。地点2

理想情况下,我们希望保留所有内容。

有没有更好的方法来保存聊天记录和对话数据、用户数据 MS BOT 中的数据包?

【问题讨论】:

    标签: node.js botframework bots azure-cosmosdb cortana


    【解决方案1】:

    所有的存储实现都只写 getData 和 saveData,它们内部有一个 key:value 存储,其中 key 通常是 userId + conversationId,但你可以随意设置它,只要你能从传递给 @ 的参数中可靠地派生它987654325@和setData

    查看 redis https://github.com/suttna/botbuilder-redis-storage - https://github.com/suttna/botbuilder-redis-storage/blob/master/src/storage.ts 中的示例,了解非常容易理解的示例存储实现。

    你会使用这样的自定义实现

    // Create new storage with redis client
    var storage = new YourStorage()
    
    // this is just here for the sake of initializing a `bot`
    var connector = new builder.ChatConnector()
    var bot = new builder.UniversalBot(connector)
    
    // Configure bot to use YourStorage
    bot.set('storage', storage)
    
    bot.set('persistConversationData', true);
    

    storage 只是一个实现的对象

    public saveData(context: IBotStorageContext, data: IBotStorageData, callback?: (err: Error) => void)
    
    public getData(context: IBotStorageContext, callback: (err: Error, data: IBotStorageData) => void)
    

    我完全只是从链接的 redis 模块中复制了这些签名,但它们在默认存储的 BotBuilder 源中是相同的 - https://github.com/Microsoft/BotBuilder/blob/5cf71c742f27d89e9dbc4076f850122bd6edac11/Node/calling/src/storage/BotStorage.ts

    样本在打字稿中。如果您不熟悉,请忽略: 后面的位,它表示事物的类型。

    【讨论】:

    • 我很困惑。你的意思是覆盖 CosmosDb 的 getData 和 setData 方法,这样我就可以改变对话数据包的行为,一旦对话结束,它就会被清除。我的目标是将所有内容保存在会话对象上的数据库中。 session.userData.{}
    • 是的,或者只是推出您自己的存储实现。它们不需要编写太多代码,只需执行 redis 实现所做的工作,除了保存到 cosmos 或任何您的存储引擎。 session.userData 由 botbuilder 在您提供的任何存储上使用这些方法填充。
    • session.userData 将具有 "${userId};userData" 的可搜索键,并且在调用 session.endConversation() 时不会被清除。正如 Catalyst 提到的,您可以更改存储实现。听起来您想生成一个类似于 userId;conversationId;currentDate 的新密钥
    • @StevenG 感谢您的反馈,但我认为我没有关注如何更改存储实现。您的意思是覆盖方法 saveData() 和 getData()。这是我迄今为止根据 Catalyst 的评论对 cosmos 所做的事情。 --- var cosmosStorage = setCosmosStorage(); function setCosmosStorage() { var docDbClient = new azure.DocumentDbClient({ //连接细节。}); var cosmosStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);返回宇宙存储; }
    • //日志中间件(RJ-db,logging)bot.use({ receive: function (event, next) { logUserConversation("User", event, event.user.id); next(); }, send: function (event, next) { logUserConversation("Bot", event, event.address.user.id); next(); } }); var chatHistory = [] const logUserConversation = (identifier, event, id) => { chatHistory[event.address.conversation.id] += (event.text); console.log('*** BOT/User Message: ' + event.text); };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多