slider

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: 7 Imported by: 0

README

Paginator

slider.png

Getting Started

package main

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

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

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) {
	slides := []slider.Slide{
		{
			Text:  "*0\\. YouTube* is an American online video sharing and social media platform headquartered in San Bruno, California\\. It was launched on _February 14, 2005_, by *Steve Chen*, *Chad Hurley*, and *Jawed Karim*",
			Photo: "AgACAgIAAxkBAAO6YmF_Z9yAfUWldGLLKi1Urz9C-TYAAs67MRtwRhFLgH_zopol78cBAAMCAANzAAMkBA",
		},
		{
			Text:  "*1\\. VK* \\(short for its original name VKontakte; Russian: ВКонтакте, meaning InContact\\) is a Russian online social media and social networking service based in *Saint Petersburg*",
			Photo: "AgACAgIAAxkBAAPAYmGBRNtafgABrZ2aldPsFzddkY_sAALWuzEbcEYRS3zTgjyXacqwAQADAgADcwADJAQ",
		},
		{
			Text:  "*2\\. Skype* is a proprietary telecommunications application operated by Skype Technologies, a division of *Microsoft*, best known for VoIP\\-based videotelephony, videoconferencing and voice calls",
			Photo: "AgACAgIAAxkBAAPCYmGBWcH4Kls8nO0tOOtvFll1R3gAAte7MRtwRhFLyCEY4pSLI8QBAAMCAANzAAMkBA",
		},
		{
			Text:  "*3\\. Reddit* \\(\\/ˈrɛdɪt\\/, stylized as reddit\\) is an American social news aggregation, web content rating, and discussion website",
			Photo: "AgACAgIAAxkBAAPEYmGBcyzFR02gbVpw70Zx_kB1Qm0AAtm7MRtwRhFLwLUyMVCSlY0BAAMCAANzAAMkBA",
		},
		{
			Text:  "*4\\. Twitter* is an American microblogging and social networking service on which users post and interact with messages known as *tweets*",
			Photo: "AgACAgIAAxkBAAPGYmGBjRDAOSinB--dnXc7wDwP7MkAAtu7MRtwRhFLs31K09kYL8cBAAMCAANzAAMkBA",
		},
		{
			Text:  "*5\\. Pinterest* is an image sharing and social media service designed to enable saving and discovery of information on the internet using images, and on a smaller scale, animated GIFs and videos, in the form of pinboards",
			Photo: "AgACAgIAAxkBAAPIYmGBovpHYPsfqQfDVo-RoTYnk4cAAt27MRtwRhFLWj59BXsormYBAAMCAANzAAMkBA",
		},
		{
			Text:  "*6\\. Instagram* is an American photo and video sharing social networking service founded by *Kevin Systrom* and *Mike Krieger*\\. In April 2012, Facebook Inc\\. acquired the service for approximately *US$1 billion* in cash and stock",
			Photo: "AgACAgIAAxkBAAPKYmGBuHVyWid7w-vCHfbr9gTBIiEAAt-7MRtwRhFLD05Sel7_-AMBAAMCAANzAAMkBA",
		},
		{
			Text:  "*7\\. LinkedIn* is an American business and employment\\-oriented online service that operates via websites and mobile apps\\. Launched on May 5, 2003",
			Photo: "AgACAgIAAxkBAAPMYmGB5ZZnzA-fmVDGeH2d7p8xfkEAAuC7MRtwRhFLnGvjK3Sf6qMBAAMCAANzAAMkBA",
		},
		{
			Text:  "*8\\. Facebook* is an American online social media and social networking service owned by Meta Platforms\\. Founded in 2004 by *Mark Zuckerberg* with fellow Harvard College students and roommates *Eduardo Saverin*, *Andrew McCollum*, *Dustin Moskovitz*, and *Chris Hughes*",
			Photo: "AgACAgIAAxkBAAPOYmGB_2yKD2BMG2IIFFA7ONY9tWUAAuG7MRtwRhFLIbkibx890csBAAMCAANzAAMkBA",
		},
	}

	opts := []slider.Option{
		slider.OnSelect("Select", true, sliderOnSelect),
		slider.OnCancel("Cancel", true, sliderOnCancel),
	}

	sl := slider.New(slides, opts...)

	sl.Show(ctx, b, strconv.Itoa(update.Message.Chat.ID))
}

func sliderOnSelect(ctx context.Context, b *bot.Bot, message *models.Message, item int) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: message.Chat.ID,
		Text:   "Select " + strconv.Itoa(item),
	})
}

func sliderOnCancel(ctx context.Context, b *bot.Bot, message *models.Message) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: message.Chat.ID,
		Text:   "Cancel",
	})
}

If you want to use an upload image instead preloaded image or image URL, you should to place file content to slide.Photo field and set slide.IsUpload field to true.

slide := &slider.Slide{
    Photo: "<file_content>",
    Text: "My Image",
    IsUpload: true,    
}

Options

See in options.go file

ShowOptions

You can use ShowOptions to customize the appearance of the slider.

For example:

sl := slider.New(slides)

sl.Show(ctx, b, strconv.Itoa(update.Message.Chat.ID), ShowWithThreadID(42))

See in show_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 OnErrorFunc

type OnErrorFunc func(err error)

type OnSelectFunc

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

type Option

type Option func(s *Slider)

func NoDeleteAfterCancel added in v0.3.2

func NoDeleteAfterCancel() Option

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

func NoDeleteAfterSelect added in v0.3.2

func NoDeleteAfterSelect() Option

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

func OnCancel

func OnCancel(buttonText string, deleteOnCancel bool, f OnCancelFunc) Option

OnCancel is a callback function that is called when the user cancels the slide selection

func OnError

func OnError(f OnErrorFunc) Option

OnError is a errors handler

func OnSelect

func OnSelect(buttonText string, deleteOnSelect bool, f OnSelectFunc) Option

OnSelect is a callback function that is called when the user selects a slide

func WithPrefix added in v0.3.1

func WithPrefix(s string) Option

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

type ShowOption added in v0.4.1

type ShowOption func(p *bot.SendPhotoParams)

func ShowWithReply added in v0.4.1

func ShowWithReply(r *models.ReplyParameters) ShowOption

ShowWithReply sets the reply parameters for the message

func ShowWithThreadID added in v0.4.1

func ShowWithThreadID(threadID int) ShowOption

ShowWithThreadID sets the thread ID for the message

type Slide

type Slide struct {
	Photo    string
	IsUpload bool
	Text     string
}

type Slider

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

func New

func New(b *bot.Bot, slides []Slide, opts ...Option) *Slider

func (*Slider) Prefix added in v0.3.1

func (s *Slider) Prefix() string

Prefix returns the prefix of the widget

func (*Slider) Show

func (s *Slider) Show(ctx context.Context, b *bot.Bot, chatID any, opts ...ShowOption) (*models.Message, error)

Jump to

Keyboard shortcuts

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