Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Latest commit

 

History

History
28 lines (20 loc) · 903 Bytes

bot-persistent.md

File metadata and controls

28 lines (20 loc) · 903 Bytes

Persist bot

For much easier save/restore of bot's state there is MagicPersistentBot that saves it's state to disk after each incoming message.

Everyting you need is only subclass from MagicPersistentBot and implement onRestoreState and onSaveState. On bot restart onRestoreState is called and you can easily restore saved state.

import im.actor.bots.framework.*
import im.actor.bots.framework.persistence.*
import org.json.JSONObject

class EchoPersistentBot(scope: MagicForkScope) : MagicPersistentBot(scope) {

    var receivedCount: Int = 0

    override fun onRestoreState(state: JSONObject) {
        receivedCount = state.optInt("counter", 0)
    }

    override fun onMessage(message: MagicBotMessage) {
        sendText("Received ${receivedCount++} messages")
    }

    override fun onSaveState(state: JSONObject) {
        state.put("counter", receivedCount)
    }
}