Skip to content

Latest commit

 

History

History
 
 

gormpersistence

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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)
    }
}