All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.justai.jaicf.context.manager.mongo.MongoBotContextManager.kt Maven / Gradle / Ivy

Go to download

MongoDB BotContextManager implementation to store your JAICF bot's context

There is a newer version: 1.3.8
Show newest version
package com.justai.jaicf.context.manager.mongo

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.api.BotResponse
import com.justai.jaicf.context.BotContext
import com.justai.jaicf.context.RequestContext
import com.justai.jaicf.context.manager.BotContextManager
import com.mongodb.client.MongoCollection
import com.mongodb.client.model.Filters
import com.mongodb.client.model.ReplaceOptions
import org.bson.Document

class MongoBotContextManager(
    private val collection: MongoCollection
): BotContextManager {

    @Suppress("DEPRECATION")
    private val mapper = jacksonObjectMapper().enableDefaultTyping()

    override fun loadContext(request: BotRequest, requestContext: RequestContext): BotContext {
        return collection
            .find(Filters.eq("_id", request.clientId))
            .iterator().tryNext()?.let { doc ->
                val model = mapper.readValue(doc.toJson(), BotContextModel::class.java)

                BotContext(model._id, model.dialogContext).apply {
                    result = model.result
                    client.putAll(model.client)
                    session.putAll(model.session)
                }

        } ?: BotContext(request.clientId)
    }

    override fun saveContext(
        botContext: BotContext,
        request: BotRequest?,
        response: BotResponse?,
        requestContext: RequestContext
    ) {
        BotContextModel(
            _id = botContext.clientId,
            result = botContext.result,
            client = botContext.client.toMap(),
            session = botContext.session.toMap(),
            dialogContext = botContext.dialogContext
        ).apply {
            val doc = Document.parse(mapper.writeValueAsString(this))
            collection.replaceOne(Filters.eq("_id", _id), doc, ReplaceOptions().upsert(true))
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy