progress

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: MIT Imports: 5 Imported by: 0

README

Progress

progress.png

If you want to use Cancel button, you should to use option WithCancel(buttonText string, deleteOnCancel bool, onCancel OnCancelFunc).

You can to customize the progress text with option WithRenderTextFunc(f RenderTextFunc)

Render func receives the progress value and returns the text.

func MyRenderFunc(value float64) string {
    s := fmt.Sprintf("My Progress: %.2f%%", value)
    
    return bot.EscapedMarkdown(s)
}

Progress use Markdown for rendering. If you have any Markdown syntax, you should escape your text, if needed. In this example we have dot in the text. So, we need to escape it.

Getting Started

package main

import (
	"context"
	"os"
	"os/signal"

	"github.com/go-telegram/bot"
	"github.com/go-telegram/bot/models"
	"github.com/go-telegram/ui/progress"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	telegramBotToken := os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN")

	opts := []bot.Option{
		bot.WithDefaultHandler(defaultHandler),
	}

	b := bot.New(telegramBotToken, opts...)

	b.Start(ctx)
}

func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
	p := progress.New()

	p.Show(ctx, b, update.Message.Chat.ID)

	go doSomeLongTaskSimple(ctx, b, p, update.Message.Chat.ID)
}

func doSomeLongTaskSimple(ctx context.Context, b *bot.Bot, p *progress.Progress, chatID int) {
	v := 0.0
	for {
		time.Sleep(time.Second)
		if v == 100 {
			p.Delete(ctx, b)
			p.Done(ctx, b)
			b.SendMessage(ctx, &bot.SendMessageParams{
				ChatID: chatID,
				Text:   "Completed",
			})
			return
		}

		v += rand.Float64() * 30
		if v > 100 {
			v = 100
		}
		p.SetValue(ctx, b, v)
	}
}

Options

See in options.go file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OnCancelFunc

type OnCancelFunc func(ctx context.Context, b *bot.Bot, message models.MaybeInaccessibleMessage)

type OnErrorHandler

type OnErrorHandler func(err error)

type Option

type Option func(p *Progress)

func OnError

func OnError(f OnErrorHandler) Option

OnError sets the error handler.

func StartValue

func StartValue(value float64) Option

StartValue sets the start value.

func WithCancel

func WithCancel(buttonText string, deleteOnCancel bool, onCancel OnCancelFunc) Option

WithCancel sets the cancel function.

func WithPrefix added in v0.3.1

func WithPrefix(s string) Option

WithPrefix is a keyboard option that sets a prefix for the widget

func WithRenderTextFunc

func WithRenderTextFunc(f RenderTextFunc) Option

WithRenderTextFunc sets the render text function.

type Progress

type Progress struct {
	// contains filtered or unexported fields
}

func New

func New(b *bot.Bot, opts ...Option) *Progress

func (*Progress) Delete

func (p *Progress) Delete(ctx context.Context, b *bot.Bot)

func (*Progress) Done added in v0.3.2

func (p *Progress) Done(ctx context.Context, b *bot.Bot)

ctx is being passed for forward compatibility reasons

func (*Progress) Prefix added in v0.3.1

func (p *Progress) Prefix() string

Prefix returns the prefix of the widget

func (*Progress) SetValue

func (p *Progress) SetValue(ctx context.Context, b *bot.Bot, value float64)

func (*Progress) Show

func (p *Progress) Show(ctx context.Context, b *bot.Bot, chatID any) error

type RenderTextFunc

type RenderTextFunc func(value float64) string

Jump to

Keyboard shortcuts

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