gormpersistence

package module
v2.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 4 Imported by: 0

README

gormpersistence

Support for GORM as persistence backend.

Installation

go get github.com/and3rson/telemux/gormpersistence/v2

Example usage

package main

import (
    "fmt"
    "log"
    "os"

    tm "github.com/and3rson/telemux/v2"
    "github.com/and3rson/telemux/gormpersistence/v2"
    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
    db, _ := gorm.Open(postgres.Open(os.Getenv("DB_DSN")), &gorm.Config{})
    // Create GORMPersistence
    p := gormpersistence.GORMPersistence{db}

    // Create required tables
    p.AutoMigrate()
    // ...alias for:
    // db.AutoMigrate(&gormpersistence.ConversationState{}, &gormpersistence.ConversationData{})

    bot, _ := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN"))
    bot.Debug = true
    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60

    updates, _ := bot.GetUpdatesChan(u)

    mux := tm.NewMux().
        AddHandler(tm.NewConversationHandler(
            "upload_photo_dialog",
            p, // We provide GORMPersistence as persistence backend for our conversation handler
            tm.StateMap{
                "": {
                    // Handlers for initial state, i. e. for conversation activation
                    // ...
                },
                "state1": {
                    // Handlers for state1
                    // ...
                },
                "state2": {
                    // Handlers for state2
                    // ...
                }
            },
            []*tm.Handler{
                // Default handlers
            },
        ))

    for update := range updates {
        mux.Dispatch(bot, update)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConversationData

type ConversationData struct {
	tm.PersistenceKey
	Data datatypes.JSONMap `gorm:"not null"`
}

ConversationData is a model that contains conversation data for users.

type ConversationState

type ConversationState struct {
	tm.PersistenceKey
	State string `gorm:"not null"`
}

ConversationState is a model that contains conversation states for users.

type GORMPersistence

type GORMPersistence struct {
	DB *gorm.DB
}

GORMPersistence is an implementation of Persistence. It stores conversation states & conversation data in database via GORM.

func (*GORMPersistence) AutoMigrate

func (p *GORMPersistence) AutoMigrate() error

AutoMigrate creates tables for ConversationState & ConversationData models

func (*GORMPersistence) GetData

func (p *GORMPersistence) GetData(pk tm.PersistenceKey) tm.Data

GetData reads conversation data from database

func (*GORMPersistence) GetState

func (p *GORMPersistence) GetState(pk tm.PersistenceKey) string

GetState reads conversation state from database

func (*GORMPersistence) SetData

func (p *GORMPersistence) SetData(pk tm.PersistenceKey, data tm.Data)

SetData writes conversation data to database

func (*GORMPersistence) SetState

func (p *GORMPersistence) SetState(pk tm.PersistenceKey, state string)

SetState writes conversation state to database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL