Skip to content

Storage ​

mtgo stores session data (auth keys, peer info, user data) through a pluggable Storage interface. Pick the backend that matches your infrastructure, or implement your own.

Module ​

bash
go get github.com/mtgo-labs/storage

Quick Start ​

The easiest way to get started is with the NewAdapter wrapper, which auto-detects the storage type:

go
import "github.com/mtgo-labs/storage"

// Use with a pre-opened database connection
adapter, err := storage.NewAdapter(db)
if err != nil {
    log.Fatal(err)
}

client, _ := telegram.NewClient(apiID, apiHash, &telegram.Config{
    BotToken:    botToken,
    SessionName: "my_bot",
    Storage:     adapter,
})

Storage Interface ​

All backends implement the storage.Storage interface:

go
type Storage interface {
    // Session lifecycle
    SaveSession(session *Session) error
    LoadSession(sessionName string) (*Session, error)
    DeleteSession(sessionName string) error

    // Peer caching (when SavePeers is enabled)
    SavePeer(peer *Peer) error
    LoadPeer(id int64) (*Peer, error)
    LoadPeerByUsername(username string) (*Peer, error)
    DeletePeer(id int64) error

    // User metadata
    SetUserID(id int64) error
    GetUserID() (int64, error)
    SetIsBot(isBot bool) error
    GetIsBot() (bool, error)
    SetFirstName(name string) error
    GetFirstName() (string, error)
    SetLastName(name string) error
    GetLastName() (string, error)
    SetUsername(username string) error
    GetUsername() (string, error)

    // State management
    SetUpdateState(dcID int, state []byte) error
    GetUpdateState(dcID int) ([]byte, error)

    // Lifecycle
    Close() error
}

Available Backends ​

BackendModuleUse Case
SQLitegithub.com/mtgo-labs/storage/sqliteSingle-process bots, local development
PostgreSQLgithub.com/mtgo-labs/storage/postgresProduction deployments, multi-process
MongoDBgithub.com/mtgo-labs/storage/mongodbDocument-oriented workloads
Redisgithub.com/mtgo-labs/storage/redisEphemeral sessions, caching layer
GORMgithub.com/mtgo-labs/storage/gormCustom databases via GORM (MySQL, etc.)
MemoryBuilt-inTesting, ephemeral bots

Conversation Store ​

Storage adapters also support the ConversationStore interface used by the conversations plugin:

go
type ConversationStore interface {
    SaveConversation(c *Conversation) error
    LoadConversation(chatID, userID int64) (*Conversation, error)
    DeleteConversation(chatID, userID int64) error
}

SQLite, PostgreSQL, and MongoDB all implement this interface. When you pass a supported storage adapter to the conversations plugin, it auto-detects and uses it for persistent conversation state.

Custom Storage ​

See Custom Storage for implementing your own backend.

Schema ​

Each backend manages its own schema. The SQL-based backends (SQLite, PostgreSQL, GORM) use sqlc-generated queries with these tables:

  • sessions — auth keys, config, DC info
  • peers — cached peer info (user ID, access hash, username)
  • kv — key-value store for user metadata and state
  • conversations — active conversation state (for conversations plugin)

Released under the Apache-2.0 License.