首页 文章

如何保存MS bot状态数据和对话?

提问于
浏览
0

我正在使用节点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/

这种方法很少受到关注,

当我们在对话框中调用endConversation()时,会清除

  • conversationData包 . 这是由sdk设计所预期的,但是我们希望将这些数据保存为具有相同用户(相同会话ID)的多个会话流 . 现在,当用户启动新意图时,db中的json cosmosDb将被conversationData上的新键替换 . 例如:在安排的 Session . 我们保存conversationData.name,conversationData.day和conversationData . 地点 . 同一用户在 Session 安排开始 . documentDb条目被conversationData.name1,conversationData.day2和conversationData替换 . place2

理想情况下,我们想保留一切 .

有没有更好的方法来保存MS BOT中的聊天记录和conversationData,userData数据库?

1 回答

  • 1

    所有存储实现都只写getData和saveData,它们内部有一个键:值存储,其中key通常是userId conversationId,但只要你可以从传递给 getDatasetData 的参数中可靠地获取它,你就可以随意使用它 .

    查看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

    样本是打字稿 . 如果您不熟悉,请在 : 之后忽略该位,这表示事物的类型 .

相关问题