datepicker

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 10 Imported by: 1

README

Datepicker

datepicker_1.png

Getting Started

package main

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

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

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) {
	kb := datepicker.New(b, onDatepickerSimpleSelect)

	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID:      update.Message.Chat.ID,
		Text:        "Select any date",
		ReplyMarkup: kb,
	})
}

func onDatepickerSimpleSelect(ctx context.Context, b *bot.Bot, mes *models.Message, date time.Time) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: mes.Chat.ID,
		Text:   "You select " + date.Format("2006-01-02"),
	})
}

Languages

You can define datepicker language by using Language(lang string) option. Dy default it is en.

Supported languages are defined in langs.json.

You can define your own language by following this example:

langsData := map[string]map[string]string{
    "mylang": map[string]string{
        "Monday":    "Mo",
        "Tuesday":   "Tu",
        "Wednesday": "We",
        "Thursday":  "Th",
        "Friday":    "Fr",
        "Saturday":  "Sa",
        "Sunday":    "Su",
        "January":   "January",
        "February":  "February",
        "March":     "March",
        "April":     "April",
        "May":       "May",
        "June":      "June",
        "July":      "July",
        "August":    "August",
        "September": "September",
        "October":   "October",
        "November":  "November",
        "December":  "December",
        "Cancel":    "Cancel",
        "Prev":      "Prev",
        "Next":      "Next",
        "Back":      "Back"
    },
}

telegramBotToken := os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN")

opts := []bot.Option{
    bot.Languages(langsData),
    bot.Language("mylang"),
}

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

In this example you can see all supported language keys

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 DatePicker

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

func New

func New(b *bot.Bot, onSelect OnSelectHandler, opts ...Option) *DatePicker

func (*DatePicker) MarshalJSON

func (datePicker *DatePicker) MarshalJSON() ([]byte, error)

func (*DatePicker) Prefix added in v0.3.1

func (datePicker *DatePicker) Prefix() string

Prefix returns the prefix of the widget

type DatesMode

type DatesMode int
const (
	DateModeExclude DatesMode = iota
	DateModeInclude
)

type LangsData

type LangsData map[string]map[string]string

LangsData contains all languages data key is language code, value is language data with key:value pairs

type OnCancelHandler

type OnCancelHandler func(ctx context.Context, bot *bot.Bot, mes models.MaybeInaccessibleMessage)

type OnErrorHandler

type OnErrorHandler func(err error)

type OnSelectHandler

type OnSelectHandler func(ctx context.Context, bot *bot.Bot, mes models.MaybeInaccessibleMessage, date time.Time)

type Option

type Option func(dp *DatePicker)

func CurrentDate

func CurrentDate(current time.Time) Option

CurrentDate sets the current date.

func Dates

func Dates(datesMode DatesMode, dates []time.Time) Option

Dates sets the dates. And mode for include/exclude.

func From

func From(from time.Time) Option

From sets the minimum date.

func Language

func Language(lang string) Option

Language sets the language of the datepicker.

func Languages

func Languages(langs LangsData) Option

Languages sets the languages of the datepicker. All supported keys you can see in langs.json file

func NoDeleteAfterCancel added in v0.3.2

func NoDeleteAfterCancel() Option

NoDeleteAfterCancel is a keyboard option that prevents the hide keyboard after cancel

func NoDeleteAfterSelect added in v0.3.2

func NoDeleteAfterSelect() Option

NoDeleteAfterSelect is a keyboard option that prevents the hide keyboard after select

func OnCancel

func OnCancel(f OnCancelHandler) Option

OnCancel sets the callback function for the cancel button.

func OnError

func OnError(f OnErrorHandler) Option

OnError sets the callback function for the error.

func StartFromSunday

func StartFromSunday() Option

StartFromSunday sets the first day of the week to Sunday.

func To

func To(to time.Time) Option

To sets the maximum date.

func WithPrefix added in v0.3.1

func WithPrefix(s string) Option

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

Jump to

Keyboard shortcuts

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