telegohandler

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: MIT Imports: 7 Imported by: 48

Documentation

Overview

Package telegohandler provides handlers & predicates for Telego.

Bot handlers provide an easy way to make net/http like handlers, but with predicates instead of paths. In addition to just predicates it, also provides groups and middlewares.

You can create BotHandler, register new handlers and start processing updates from the update channel which you provide. All handlers process updates concurrently, but keep in mind that predicates are checked sequentially. This gives an ability to process one update only with the first matched handler.

Example

This example shows how you can create BotHandler and register new handlers. Note, that order of registration directly impacts order of checking matched handlers, and only the first matched handler will process the update.

package main

import (
	"fmt"
	"os"

	"github.com/mymmrac/telego"
	th "github.com/mymmrac/telego/telegohandler"
	tu "github.com/mymmrac/telego/telegoutil"
)

func main() {
	botToken := os.Getenv("TOKEN")

	// Note: Please keep in mind that default logger may expose sensitive information, use in development only
	bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Get updates channel
	updates, _ := bot.UpdatesViaLongPolling(nil)

	// Create bot handler and specify from where to get updates
	bh, _ := th.NewBotHandler(bot, updates)

	// Stop handling updates
	defer bh.Stop()

	// Stop getting updates
	defer bot.StopLongPolling()

	// Register new handler with match on command `/start`
	bh.Handle(func(bot *telego.Bot, update telego.Update) {
		// Send message
		_, _ = bot.SendMessage(tu.Messagef(
			tu.ID(update.Message.Chat.ID),
			"Hello %s!", update.Message.From.FirstName,
		))
	}, th.CommandEqual("start"))

	// Register new handler with match on any command
	// Handlers will match only once and in order of registration, so this handler will be called on any command
	// except `/start` command
	bh.Handle(func(bot *telego.Bot, update telego.Update) {
		// Send message
		_, _ = bot.SendMessage(tu.Message(
			tu.ID(update.Message.Chat.ID),
			"Unknown command, use /start",
		))
	}, th.AnyCommand())

	// Start handling updates
	bh.Start()
}

One more example of handler usage. It shows how to use specific handlers to process individual fields of telego.Update.

package main

import (
	"fmt"
	"os"

	"github.com/mymmrac/telego"
	th "github.com/mymmrac/telego/telegohandler"
	tu "github.com/mymmrac/telego/telegoutil"
)

func main() {
	botToken := os.Getenv("TOKEN")

	// Note: Please keep in mind that default logger may expose sensitive information, use in development only
	bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Get updates channel
	updates, _ := bot.UpdatesViaLongPolling(nil)

	// Create bot handler and specify from where to get updates
	bh, _ := th.NewBotHandler(bot, updates)

	// Stop handling updates
	defer bh.Stop()

	// Stop getting updates
	defer bot.StopLongPolling()

	// Register new handler with match on command `/start`
	bh.HandleMessage(func(bot *telego.Bot, message telego.Message) {
		// Send a message with inline keyboard
		_, _ = bot.SendMessage(tu.Messagef(
			tu.ID(message.Chat.ID),
			"Hello %s!", message.From.FirstName,
		).WithReplyMarkup(tu.InlineKeyboard(
			tu.InlineKeyboardRow(tu.InlineKeyboardButton("Go!").WithCallbackData("go"))),
		))
	}, th.CommandEqual("start"))

	// Register new handler with match on a call back query with data equal to `go` and non-nil message
	bh.HandleCallbackQuery(func(bot *telego.Bot, query telego.CallbackQuery) {
		// Send message
		_, _ = bot.SendMessage(tu.Message(tu.ID(query.Message.Chat.ID), "GO GO GO"))

		// Answer callback query
		_ = bot.AnswerCallbackQuery(tu.CallbackQuery(query.ID).WithText("Done"))
	}, th.AnyCallbackQueryWithMessage(), th.CallbackDataEqual("go"))

	// Start handling updates
	bh.Start()
}

In this example, usage of groups and middleware will be shown.

package main

import (
	"fmt"
	"os"

	"github.com/mymmrac/telego"
	th "github.com/mymmrac/telego/telegohandler"
)

func main() {
	botToken := os.Getenv("TOKEN")

	// Note: Please keep in mind that default logger may expose sensitive information, use in development only
	bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Get updates channel
	updates, _ := bot.UpdatesViaLongPolling(nil)

	// Create bot handler and specify from where to get updates
	bh, _ := th.NewBotHandler(bot, updates)

	// Stop handling updates
	defer bh.Stop()

	// Stop getting updates
	defer bot.StopLongPolling()

	// Add global middleware, it will be applied in order of addition
	bh.Use(
		func(bot *telego.Bot, update telego.Update, next th.Handler) {
			fmt.Println("Global middleware") // Will be called first
			next(bot, update)
		},
		func(bot *telego.Bot, update telego.Update, next th.Handler) {
			fmt.Println("Global middleware 2") // Will be called second
			next(bot, update)
		},
	)

	// Create any groups with or without predicates
	// Note: Updates first checked by groups and only then by handlers (group -> ... -> group -> handler)
	task := bh.Group(th.TextContains("task"))

	// Add middleware to groups
	task.Use(func(bot *telego.Bot, update telego.Update, next th.Handler) {
		fmt.Println("Group based middleware") // Will be called third

		if len(update.Message.Text) < 10 {
			next(bot, update)
		}
	})

	// Handle updates on a group
	task.HandleMessage(func(bot *telego.Bot, message telego.Message) {
		fmt.Println("Task...") // Will be called fourth
	})

	// Start handling updates
	bh.Start()
}

Index

Constants

View Source
const (
	CommandMatchCmdGroup         = 1
	CommandMatchBotUsernameGroup = 2
	CommandMatchArgsGroup        = 3
)

Command match group indexes in the CommandRegexp

View Source
const CommandMatchGroupsLen = 4

CommandMatchGroupsLen represents the length of match groups in the CommandRegexp

Variables

View Source
var CommandRegexp = regexp.MustCompile(`(?s)^/(\w+)(@\w+)?(?:\s+(.+?)\s*)?$`)

CommandRegexp matches to command and has match groups on command, bot username and arguments

Functions

This section is empty.

Types

type BotHandler

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

BotHandler represents a bot handler that can handle updated matching by predicates

func NewBotHandler

func NewBotHandler(bot *telego.Bot, updates <-chan telego.Update, options ...BotHandlerOption) (*BotHandler, error)

NewBotHandler creates new bot handler

func (*BotHandler) BaseGroup added in v0.26.0

func (h *BotHandler) BaseGroup() *HandlerGroup

BaseGroup returns a base group that is used by default in BotHandler methods

func (*BotHandler) Group added in v0.21.0

func (h *BotHandler) Group(predicates ...Predicate) *HandlerGroup

Group creates a new group of handlers and middlewares from the base group Note: Updates first checked by group and only after that by handler

Warning: Panics if nil predicates passed

func (*BotHandler) Handle

func (h *BotHandler) Handle(handler Handler, predicates ...Predicate)

Handle registers new handler in the base group, update will be processed only by first-matched handler, order of registration determines the order of matching handlers. Important to notice, update's context will be automatically canceled once the handler will finish processing or the bot handler stopped. Note: All handlers will process updates in parallel, there is no guaranty on order of processed updates, also keep in mind that middlewares and predicates are checked sequentially.

Warning: Panics if nil handler or predicates passed

func (*BotHandler) HandleCallbackQuery added in v0.10.4

func (h *BotHandler) HandleCallbackQuery(handler CallbackQueryHandler, predicates ...Predicate)

HandleCallbackQuery same as Handle, but assumes that the update contains a callback query

func (*BotHandler) HandleCallbackQueryCtx added in v0.27.0

func (h *BotHandler) HandleCallbackQueryCtx(handler CallbackQueryHandlerCtx, predicates ...Predicate)

HandleCallbackQueryCtx same as Handle, but assumes that the update contains a callback query

func (*BotHandler) HandleChannelPost added in v0.11.1

func (h *BotHandler) HandleChannelPost(handler MessageHandler, predicates ...Predicate)

HandleChannelPost same as Handle, but assumes that the update contains a channel post

func (*BotHandler) HandleChannelPostCtx added in v0.27.0

func (h *BotHandler) HandleChannelPostCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleChannelPostCtx same as Handle, but assumes that the update contains a channel post

func (*BotHandler) HandleChatJoinRequest added in v0.11.1

func (h *BotHandler) HandleChatJoinRequest(handler ChatJoinRequestHandler, predicates ...Predicate)

HandleChatJoinRequest same as Handle, but assumes that the update contains chat join request

func (*BotHandler) HandleChatJoinRequestCtx added in v0.27.0

func (h *BotHandler) HandleChatJoinRequestCtx(handler ChatJoinRequestHandlerCtx, predicates ...Predicate)

HandleChatJoinRequestCtx same as Handle, but assumes that the update contains chat join request

func (*BotHandler) HandleChatMemberUpdated added in v0.11.1

func (h *BotHandler) HandleChatMemberUpdated(handler ChatMemberUpdatedHandler, predicates ...Predicate)

HandleChatMemberUpdated same as Handle, but assumes that the update contains chat member

func (*BotHandler) HandleChatMemberUpdatedCtx added in v0.27.0

func (h *BotHandler) HandleChatMemberUpdatedCtx(handler ChatMemberUpdatedHandlerCtx, predicates ...Predicate)

HandleChatMemberUpdatedCtx same as Handle, but assumes that the update contains chat member

func (*BotHandler) HandleChosenInlineResult added in v0.11.1

func (h *BotHandler) HandleChosenInlineResult(handler ChosenInlineResultHandler, predicates ...Predicate)

HandleChosenInlineResult same as Handle, but assumes that the update contains a chosen inline result

func (*BotHandler) HandleChosenInlineResultCtx added in v0.27.0

func (h *BotHandler) HandleChosenInlineResultCtx(handler ChosenInlineResultHandlerCtx, predicates ...Predicate)

HandleChosenInlineResultCtx same as Handle, but assumes that the update contains a chosen inline result

func (*BotHandler) HandleEditedChannelPost added in v0.11.1

func (h *BotHandler) HandleEditedChannelPost(handler MessageHandler, predicates ...Predicate)

HandleEditedChannelPost same as Handle, but assumes that the update contains an edited channel post

func (*BotHandler) HandleEditedChannelPostCtx added in v0.27.0

func (h *BotHandler) HandleEditedChannelPostCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleEditedChannelPostCtx same as Handle, but assumes that the update contains an edited channel post

func (*BotHandler) HandleEditedMessage added in v0.11.1

func (h *BotHandler) HandleEditedMessage(handler MessageHandler, predicates ...Predicate)

HandleEditedMessage same as Handle, but assumes that the update contains an edited message

func (*BotHandler) HandleEditedMessageCtx added in v0.27.0

func (h *BotHandler) HandleEditedMessageCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleEditedMessageCtx same as Handle, but assumes that the update contains an edited message

func (*BotHandler) HandleInlineQuery added in v0.11.1

func (h *BotHandler) HandleInlineQuery(handler InlineQueryHandler, predicates ...Predicate)

HandleInlineQuery same as Handle, but assumes that the update contains an inline query

func (*BotHandler) HandleInlineQueryCtx added in v0.27.0

func (h *BotHandler) HandleInlineQueryCtx(handler InlineQueryHandlerCtx, predicates ...Predicate)

HandleInlineQueryCtx same as Handle, but assumes that the update contains an inline query

func (*BotHandler) HandleMessage added in v0.10.3

func (h *BotHandler) HandleMessage(handler MessageHandler, predicates ...Predicate)

HandleMessage same as Handle, but assumes that the update contains a message

func (*BotHandler) HandleMessageCtx added in v0.27.0

func (h *BotHandler) HandleMessageCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleMessageCtx same as Handle, but assumes that the update contains a message

func (*BotHandler) HandleMyChatMemberUpdated added in v0.11.1

func (h *BotHandler) HandleMyChatMemberUpdated(handler ChatMemberUpdatedHandler, predicates ...Predicate)

HandleMyChatMemberUpdated same as Handle, but assumes that the update contains my chat member

func (*BotHandler) HandleMyChatMemberUpdatedCtx added in v0.27.0

func (h *BotHandler) HandleMyChatMemberUpdatedCtx(handler ChatMemberUpdatedHandlerCtx, predicates ...Predicate)

HandleMyChatMemberUpdatedCtx same as Handle, but assumes that the update contains my chat member

func (*BotHandler) HandlePoll added in v0.11.1

func (h *BotHandler) HandlePoll(handler PollHandler, predicates ...Predicate)

HandlePoll same as Handle, but assumes that the update contains a poll

func (*BotHandler) HandlePollAnswer added in v0.11.1

func (h *BotHandler) HandlePollAnswer(handler PollAnswerHandler, predicates ...Predicate)

HandlePollAnswer same as Handle, but assumes that the update contains a poll answer

func (*BotHandler) HandlePollAnswerCtx added in v0.27.0

func (h *BotHandler) HandlePollAnswerCtx(handler PollAnswerHandlerCtx, predicates ...Predicate)

HandlePollAnswerCtx same as Handle, but assumes that the update contains a poll answer

func (*BotHandler) HandlePollCtx added in v0.27.0

func (h *BotHandler) HandlePollCtx(handler PollHandlerCtx, predicates ...Predicate)

HandlePollCtx same as Handle, but assumes that the update contains a poll

func (*BotHandler) HandlePreCheckoutQuery added in v0.11.1

func (h *BotHandler) HandlePreCheckoutQuery(handler PreCheckoutQueryHandler, predicates ...Predicate)

HandlePreCheckoutQuery same as Handle, but assumes that the update contains a pre checkout query

func (*BotHandler) HandlePreCheckoutQueryCtx added in v0.27.0

func (h *BotHandler) HandlePreCheckoutQueryCtx(handler PreCheckoutQueryHandlerCtx, predicates ...Predicate)

HandlePreCheckoutQueryCtx same as Handle, but assumes that the update contains a pre checkout query

func (*BotHandler) HandleShippingQuery added in v0.11.1

func (h *BotHandler) HandleShippingQuery(handler ShippingQueryHandler, predicates ...Predicate)

HandleShippingQuery same as Handle, but assumes that the update contains a shipping query

func (*BotHandler) HandleShippingQueryCtx added in v0.27.0

func (h *BotHandler) HandleShippingQueryCtx(handler ShippingQueryHandlerCtx, predicates ...Predicate)

HandleShippingQueryCtx same as Handle, but assumes that the update contains a shipping query

func (*BotHandler) IsRunning added in v0.9.1

func (h *BotHandler) IsRunning() bool

IsRunning tells if Start is running

func (*BotHandler) Start

func (h *BotHandler) Start()

Start starts handling of updates, blocks execution Note: Calling BotHandler.Start method multiple times after the first one does nothing.

func (*BotHandler) Stop

func (h *BotHandler) Stop()

Stop stops handling of updates, will block until all updates have been processes. It's recommended to use BotHandler.StopWithContext if you want to force stop after some timeout.

func (*BotHandler) StopWithContext added in v0.30.0

func (h *BotHandler) StopWithContext(ctx context.Context)

StopWithContext stops handling of updates, blocks until all updates have been processes or when context is canceled. Note: Calling BotHandler.StopWithContext method multiple times or before BotHandler.Start does nothing.

func (*BotHandler) Use added in v0.21.0

func (h *BotHandler) Use(middlewares ...Middleware)

Use applies middleware to the base group Note: The chain will be stopped if middleware doesn't call the next func, if there is no context timeout then update will be stuck, if there is time out then the group will be skipped since not all middlewares were called

Warning: Panics if nil middlewares passed

type BotHandlerOption added in v0.13.0

type BotHandlerOption func(bh *BotHandler) error

BotHandlerOption represents an option that can be applied to bot handler

type CallbackQueryHandler added in v0.10.4

type CallbackQueryHandler func(bot *telego.Bot, query telego.CallbackQuery)

CallbackQueryHandler handles callback queries that came from bot

type CallbackQueryHandlerCtx added in v0.27.0

type CallbackQueryHandlerCtx func(ctx context.Context, bot *telego.Bot, query telego.CallbackQuery)

CallbackQueryHandlerCtx handles callback queries that came from bot with context

type ChatJoinRequestHandler added in v0.11.1

type ChatJoinRequestHandler func(bot *telego.Bot, request telego.ChatJoinRequest)

ChatJoinRequestHandler handles chat join request that came from bot

type ChatJoinRequestHandlerCtx added in v0.27.0

type ChatJoinRequestHandlerCtx func(ctx context.Context, bot *telego.Bot, request telego.ChatJoinRequest)

ChatJoinRequestHandlerCtx handles chat join request that came from bot with context

type ChatMemberUpdatedHandler added in v0.11.1

type ChatMemberUpdatedHandler func(bot *telego.Bot, chatMember telego.ChatMemberUpdated)

ChatMemberUpdatedHandler handles chat member that came from bot

type ChatMemberUpdatedHandlerCtx added in v0.27.0

type ChatMemberUpdatedHandlerCtx func(ctx context.Context, bot *telego.Bot, chatMember telego.ChatMemberUpdated)

ChatMemberUpdatedHandlerCtx handles chat member that came from bot with context

type ChosenInlineResultHandler added in v0.11.1

type ChosenInlineResultHandler func(bot *telego.Bot, result telego.ChosenInlineResult)

ChosenInlineResultHandler handles chosen inline result that came from bot

type ChosenInlineResultHandlerCtx added in v0.27.0

type ChosenInlineResultHandlerCtx func(ctx context.Context, bot *telego.Bot, result telego.ChosenInlineResult)

ChosenInlineResultHandlerCtx handles chosen inline result that came from bot with context

type Handler

type Handler func(bot *telego.Bot, update telego.Update)

Handler handles update that came from bot

type HandlerGroup added in v0.21.0

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

HandlerGroup represents a group of handlers, middlewares and child groups

func (*HandlerGroup) Group added in v0.21.0

func (h *HandlerGroup) Group(predicates ...Predicate) *HandlerGroup

Group creates a new group of handlers and middlewares from the parent group Note: Updates first checked by group and only after that by handler

Warning: Panics if nil predicates passed

func (*HandlerGroup) Handle added in v0.21.0

func (h *HandlerGroup) Handle(handler Handler, predicates ...Predicate)

Handle registers new handler in the group, update will be processed only by first-matched handler, order of registration determines the order of matching handlers. Important to notice, update's context will be automatically canceled once the handler will finish processing or the bot handler stopped. Note: All handlers will process updates in parallel, there is no guaranty on order of processed updates, also keep in mind that middlewares and predicates are checked sequentially.

Warning: Panics if nil handler or predicates passed

func (*HandlerGroup) HandleCallbackQuery added in v0.21.0

func (h *HandlerGroup) HandleCallbackQuery(handler CallbackQueryHandler, predicates ...Predicate)

HandleCallbackQuery same as Handle, but assumes that the update contains a callback query

func (*HandlerGroup) HandleCallbackQueryCtx added in v0.27.0

func (h *HandlerGroup) HandleCallbackQueryCtx(handler CallbackQueryHandlerCtx, predicates ...Predicate)

HandleCallbackQueryCtx same as Handle, but assumes that the update contains a callback query

func (*HandlerGroup) HandleChannelPost added in v0.21.0

func (h *HandlerGroup) HandleChannelPost(handler MessageHandler, predicates ...Predicate)

HandleChannelPost same as Handle, but assumes that the update contains a channel post

func (*HandlerGroup) HandleChannelPostCtx added in v0.27.0

func (h *HandlerGroup) HandleChannelPostCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleChannelPostCtx same as Handle, but assumes that the update contains a channel post

func (*HandlerGroup) HandleChatJoinRequest added in v0.21.0

func (h *HandlerGroup) HandleChatJoinRequest(handler ChatJoinRequestHandler, predicates ...Predicate)

HandleChatJoinRequest same as Handle, but assumes that the update contains chat join request

func (*HandlerGroup) HandleChatJoinRequestCtx added in v0.27.0

func (h *HandlerGroup) HandleChatJoinRequestCtx(handler ChatJoinRequestHandlerCtx, predicates ...Predicate)

HandleChatJoinRequestCtx same as Handle, but assumes that the update contains chat join request

func (*HandlerGroup) HandleChatMemberUpdated added in v0.21.0

func (h *HandlerGroup) HandleChatMemberUpdated(handler ChatMemberUpdatedHandler, predicates ...Predicate)

HandleChatMemberUpdated same as Handle, but assumes that the update contains chat member

func (*HandlerGroup) HandleChatMemberUpdatedCtx added in v0.27.0

func (h *HandlerGroup) HandleChatMemberUpdatedCtx(handler ChatMemberUpdatedHandlerCtx, predicates ...Predicate)

HandleChatMemberUpdatedCtx same as Handle, but assumes that the update contains chat member

func (*HandlerGroup) HandleChosenInlineResult added in v0.21.0

func (h *HandlerGroup) HandleChosenInlineResult(handler ChosenInlineResultHandler, predicates ...Predicate)

HandleChosenInlineResult same as Handle, but assumes that the update contains a chosen inline result

func (*HandlerGroup) HandleChosenInlineResultCtx added in v0.27.0

func (h *HandlerGroup) HandleChosenInlineResultCtx(handler ChosenInlineResultHandlerCtx, predicates ...Predicate)

HandleChosenInlineResultCtx same as Handle, but assumes that the update contains a chosen inline result

func (*HandlerGroup) HandleEditedChannelPost added in v0.21.0

func (h *HandlerGroup) HandleEditedChannelPost(handler MessageHandler, predicates ...Predicate)

HandleEditedChannelPost same as Handle, but assumes that the update contains an edited channel post

func (*HandlerGroup) HandleEditedChannelPostCtx added in v0.27.0

func (h *HandlerGroup) HandleEditedChannelPostCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleEditedChannelPostCtx same as Handle, but assumes that the update contains an edited channel post

func (*HandlerGroup) HandleEditedMessage added in v0.21.0

func (h *HandlerGroup) HandleEditedMessage(handler MessageHandler, predicates ...Predicate)

HandleEditedMessage same as Handle, but assumes that the update contains an edited message

func (*HandlerGroup) HandleEditedMessageCtx added in v0.27.0

func (h *HandlerGroup) HandleEditedMessageCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleEditedMessageCtx same as Handle, but assumes that the update contains an edited message

func (*HandlerGroup) HandleInlineQuery added in v0.21.0

func (h *HandlerGroup) HandleInlineQuery(handler InlineQueryHandler, predicates ...Predicate)

HandleInlineQuery same as Handle, but assumes that the update contains an inline query

func (*HandlerGroup) HandleInlineQueryCtx added in v0.27.0

func (h *HandlerGroup) HandleInlineQueryCtx(handler InlineQueryHandlerCtx, predicates ...Predicate)

HandleInlineQueryCtx same as Handle, but assumes that the update contains an inline query

func (*HandlerGroup) HandleMessage added in v0.21.0

func (h *HandlerGroup) HandleMessage(handler MessageHandler, predicates ...Predicate)

HandleMessage same as Handle, but assumes that the update contains a message

func (*HandlerGroup) HandleMessageCtx added in v0.27.0

func (h *HandlerGroup) HandleMessageCtx(handler MessageHandlerCtx, predicates ...Predicate)

HandleMessageCtx same as Handle, but assumes that the update contains a message

func (*HandlerGroup) HandleMyChatMemberUpdated added in v0.21.0

func (h *HandlerGroup) HandleMyChatMemberUpdated(handler ChatMemberUpdatedHandler, predicates ...Predicate)

HandleMyChatMemberUpdated same as Handle, but assumes that the update contains my chat member

func (*HandlerGroup) HandleMyChatMemberUpdatedCtx added in v0.27.0

func (h *HandlerGroup) HandleMyChatMemberUpdatedCtx(handler ChatMemberUpdatedHandlerCtx, predicates ...Predicate)

HandleMyChatMemberUpdatedCtx same as Handle, but assumes that the update contains my chat member

func (*HandlerGroup) HandlePoll added in v0.21.0

func (h *HandlerGroup) HandlePoll(handler PollHandler, predicates ...Predicate)

HandlePoll same as Handle, but assumes that the update contains a poll

func (*HandlerGroup) HandlePollAnswer added in v0.21.0

func (h *HandlerGroup) HandlePollAnswer(handler PollAnswerHandler, predicates ...Predicate)

HandlePollAnswer same as Handle, but assumes that the update contains a poll answer

func (*HandlerGroup) HandlePollAnswerCtx added in v0.27.0

func (h *HandlerGroup) HandlePollAnswerCtx(handler PollAnswerHandlerCtx, predicates ...Predicate)

HandlePollAnswerCtx same as Handle, but assumes that the update contains a poll answer

func (*HandlerGroup) HandlePollCtx added in v0.27.0

func (h *HandlerGroup) HandlePollCtx(handler PollHandlerCtx, predicates ...Predicate)

HandlePollCtx same as Handle, but assumes that the update contains a poll

func (*HandlerGroup) HandlePreCheckoutQuery added in v0.21.0

func (h *HandlerGroup) HandlePreCheckoutQuery(handler PreCheckoutQueryHandler, predicates ...Predicate)

HandlePreCheckoutQuery same as Handle, but assumes that the update contains a pre checkout query

func (*HandlerGroup) HandlePreCheckoutQueryCtx added in v0.27.0

func (h *HandlerGroup) HandlePreCheckoutQueryCtx(handler PreCheckoutQueryHandlerCtx, predicates ...Predicate)

HandlePreCheckoutQueryCtx same as Handle, but assumes that the update contains a pre checkout query

func (*HandlerGroup) HandleShippingQuery added in v0.21.0

func (h *HandlerGroup) HandleShippingQuery(handler ShippingQueryHandler, predicates ...Predicate)

HandleShippingQuery same as Handle, but assumes that the update contains a shipping query

func (*HandlerGroup) HandleShippingQueryCtx added in v0.27.0

func (h *HandlerGroup) HandleShippingQueryCtx(handler ShippingQueryHandlerCtx, predicates ...Predicate)

HandleShippingQueryCtx same as Handle, but assumes that the update contains a shipping query

func (*HandlerGroup) Use added in v0.21.0

func (h *HandlerGroup) Use(middlewares ...Middleware)

Use applies middleware to the group Note: The chain will be stopped if middleware doesn't call the next func, if there is no context timeout then update will be stuck, if there is time out then the group will be skipped since not all middlewares were called

Warning: Panics if nil middlewares passed

type InlineQueryHandler added in v0.11.1

type InlineQueryHandler func(bot *telego.Bot, query telego.InlineQuery)

InlineQueryHandler handles inline queries that came from bot

type InlineQueryHandlerCtx added in v0.27.0

type InlineQueryHandlerCtx func(ctx context.Context, bot *telego.Bot, query telego.InlineQuery)

InlineQueryHandlerCtx handles inline queries that came from bot with context

type MessageHandler added in v0.10.3

type MessageHandler func(bot *telego.Bot, message telego.Message)

MessageHandler handles message that came from bot

type MessageHandlerCtx added in v0.27.0

type MessageHandlerCtx func(ctx context.Context, bot *telego.Bot, message telego.Message)

MessageHandlerCtx handles message that came from bot with context

type Middleware added in v0.21.0

type Middleware func(bot *telego.Bot, update telego.Update, next Handler)

Middleware applies any function on bot and update before calling other middlewares, predicates and handler Note: Calling next multiple times does nothing after first call, calling next in goroutine is allowed, but user should expect that context will be closed sooner than handler ends

Warning: Not calling next at all is allowed, but if context doesn't close, update will be stuck forever, however if context closes since not all middlewares were executed, the handler group will be skipped

func PanicRecovery added in v0.21.0

func PanicRecovery() Middleware

PanicRecovery returns a middleware that will recover handler from panic Note: It's not recommend to ignore panics, use PanicRecoveryHandler instead to handle them

func PanicRecoveryHandler added in v0.27.0

func PanicRecoveryHandler(panicHandler func(recovered any)) Middleware

PanicRecoveryHandler returns a middleware that will recover handler from panic and call panic handler

func Timeout added in v0.27.0

func Timeout(timeout time.Duration) Middleware

Timeout returns a middleware that will add timeout to context

type PollAnswerHandler added in v0.11.1

type PollAnswerHandler func(bot *telego.Bot, answer telego.PollAnswer)

PollAnswerHandler handles poll answer that came from bot

type PollAnswerHandlerCtx added in v0.27.0

type PollAnswerHandlerCtx func(ctx context.Context, bot *telego.Bot, answer telego.PollAnswer)

PollAnswerHandlerCtx handles poll answer that came from bot with context

type PollHandler added in v0.11.1

type PollHandler func(bot *telego.Bot, poll telego.Poll)

PollHandler handles poll that came from bot

type PollHandlerCtx added in v0.27.0

type PollHandlerCtx func(ctx context.Context, bot *telego.Bot, poll telego.Poll)

PollHandlerCtx handles poll that came from bot with context

type PreCheckoutQueryHandler added in v0.11.1

type PreCheckoutQueryHandler func(bot *telego.Bot, query telego.PreCheckoutQuery)

PreCheckoutQueryHandler handles pre checkout query that came from bot

type PreCheckoutQueryHandlerCtx added in v0.27.0

type PreCheckoutQueryHandlerCtx func(ctx context.Context, bot *telego.Bot, query telego.PreCheckoutQuery)

PreCheckoutQueryHandlerCtx handles pre checkout query that came from bot with context

type Predicate

type Predicate func(update telego.Update) bool

Predicate allows filtering updates for handlers Note: Predicate can't change the update, because it uses a copy, not original value

func And added in v0.27.2

func And(predicates ...Predicate) Predicate

And is true if all the predicates are true

func Any added in v0.27.2

func Any() Predicate

Any is always true

func AnyCallbackQuery added in v0.10.4

func AnyCallbackQuery() Predicate

AnyCallbackQuery is true if the callback query isn't nil

func AnyCallbackQueryWithMessage added in v0.10.4

func AnyCallbackQueryWithMessage() Predicate

AnyCallbackQueryWithMessage is true if callback query and its message isn't nil

func AnyCaptionCommand added in v0.26.1

func AnyCaptionCommand() Predicate

AnyCaptionCommand is true if the message isn't nil, and its caption matches to command regexp

func AnyChannelPost added in v0.11.1

func AnyChannelPost() Predicate

AnyChannelPost is true if channel post isn't nil

func AnyChannelPostWithCaption added in v0.26.1

func AnyChannelPostWithCaption() Predicate

AnyChannelPostWithCaption is true if channel post isn't nil and its caption is not empty

func AnyChannelPostWithText added in v0.13.0

func AnyChannelPostWithText() Predicate

AnyChannelPostWithText is true if channel post isn't nil and its text is not empty

func AnyChatJoinRequest added in v0.11.1

func AnyChatJoinRequest() Predicate

AnyChatJoinRequest is true if chat join request isn't nil

func AnyChatMember added in v0.11.1

func AnyChatMember() Predicate

AnyChatMember is true if chat member isn't nil

func AnyChosenInlineResult added in v0.11.1

func AnyChosenInlineResult() Predicate

AnyChosenInlineResult is true if the chosen inline result isn't nil

func AnyCommand added in v0.10.1

func AnyCommand() Predicate

AnyCommand is true if the message isn't nil, and it matches to command regexp

func AnyEditedChannelPost added in v0.11.1

func AnyEditedChannelPost() Predicate

AnyEditedChannelPost is true if the edited channel post isn't nil

func AnyEditedChannelPostWithCaption added in v0.26.1

func AnyEditedChannelPostWithCaption() Predicate

AnyEditedChannelPostWithCaption is true if edited channel post isn't nil and its caption is not empty

func AnyEditedChannelPostWithText added in v0.13.0

func AnyEditedChannelPostWithText() Predicate

AnyEditedChannelPostWithText is true if edited channel post isn't nil and its text is not empty

func AnyEditedMessage added in v0.11.1

func AnyEditedMessage() Predicate

AnyEditedMessage is true if the edited message isn't nil

func AnyEditedMessageWithCaption added in v0.26.1

func AnyEditedMessageWithCaption() Predicate

AnyEditedMessageWithCaption is true if the edited message isn't nil and its caption is not empty

func AnyEditedMessageWithFrom added in v0.26.0

func AnyEditedMessageWithFrom() Predicate

AnyEditedMessageWithFrom is true if the edited message isn't nil and its from (sender) is not nil

func AnyEditedMessageWithText added in v0.13.0

func AnyEditedMessageWithText() Predicate

AnyEditedMessageWithText is true if the edited message isn't nil and its text is not empty

func AnyInlineQuery added in v0.11.1

func AnyInlineQuery() Predicate

AnyInlineQuery is true if inline query isn't nil

func AnyMessage added in v0.10.3

func AnyMessage() Predicate

AnyMessage is true if the message isn't nil

func AnyMessageWithCaption added in v0.26.1

func AnyMessageWithCaption() Predicate

AnyMessageWithCaption is true if the message isn't nil and its caption is not empty

func AnyMessageWithFrom added in v0.26.0

func AnyMessageWithFrom() Predicate

AnyMessageWithFrom is true if the message isn't nil and its from (sender) is not nil

func AnyMessageWithText added in v0.13.0

func AnyMessageWithText() Predicate

AnyMessageWithText is true if the message isn't nil and its text is not empty

func AnyMyChatMember added in v0.11.1

func AnyMyChatMember() Predicate

AnyMyChatMember is true if my chat member isn't nil

func AnyPoll added in v0.11.1

func AnyPoll() Predicate

AnyPoll is true if the poll isn't nil

func AnyPollAnswer added in v0.11.1

func AnyPollAnswer() Predicate

AnyPollAnswer is true if the poll answer isn't nil

func AnyPreCheckoutQuery added in v0.11.1

func AnyPreCheckoutQuery() Predicate

AnyPreCheckoutQuery is true if the pre checkout query isn't nil

func AnyShippingQuery added in v0.11.1

func AnyShippingQuery() Predicate

AnyShippingQuery is true if shipping query isn't nil

func CallbackDataContains added in v0.10.4

func CallbackDataContains(text string) Predicate

CallbackDataContains is true if the callback query isn't nil, and its data contains specified text

func CallbackDataEqual added in v0.10.4

func CallbackDataEqual(text string) Predicate

CallbackDataEqual is true if callback query isn't nil, and its data equal to specified text

func CallbackDataEqualFold added in v0.10.4

func CallbackDataEqualFold(text string) Predicate

CallbackDataEqualFold is true if the callback query isn't nil, and its data equal fold (more general form of case-insensitivity equal) to the specified text

func CallbackDataMatches added in v0.10.4

func CallbackDataMatches(pattern *regexp.Regexp) Predicate

CallbackDataMatches is true if the callback query isn't nil, and its data matches specified regexp

func CallbackDataPrefix added in v0.10.4

func CallbackDataPrefix(prefix string) Predicate

CallbackDataPrefix is true if the callback query isn't nil, and its data has specified prefix

func CallbackDataSuffix added in v0.10.4

func CallbackDataSuffix(suffix string) Predicate

CallbackDataSuffix is true if the callback query isn't nil, and its data has specified suffix

func CaptionCommandEqual added in v0.26.1

func CaptionCommandEqual(command string) Predicate

CaptionCommandEqual is true if the message isn't nil, and its caption contains specified command

func CaptionCommandEqualArgc added in v0.26.1

func CaptionCommandEqualArgc(command string, argc int) Predicate

CaptionCommandEqualArgc is true if the message isn't nil, and its caption contains specified command with a number of args

func CaptionCommandEqualArgv added in v0.26.1

func CaptionCommandEqualArgv(command string, argv ...string) Predicate

CaptionCommandEqualArgv is true if the message isn't nil, and its caption contains specified command and args

func CaptionContains added in v0.26.1

func CaptionContains(text string) Predicate

CaptionContains is true if the message isn't nil, and its caption contains specified text

func CaptionEqual added in v0.26.1

func CaptionEqual(text string) Predicate

CaptionEqual is true if the message isn't nil, and its caption is equal to the specified text

func CaptionEqualFold added in v0.26.1

func CaptionEqualFold(text string) Predicate

CaptionEqualFold is true if the message isn't nil, and its caption equal fold (more general form of case-insensitivity equal) to the specified text

func CaptionMatches added in v0.26.1

func CaptionMatches(pattern *regexp.Regexp) Predicate

CaptionMatches is true if the message isn't nil, and its caption matches specified regexp

func CaptionPrefix added in v0.26.1

func CaptionPrefix(prefix string) Predicate

CaptionPrefix is true if the message isn't nil, and its caption has specified prefix

func CaptionSuffix added in v0.26.1

func CaptionSuffix(suffix string) Predicate

CaptionSuffix is true if the message isn't nil, and its caption has specified suffix

func CommandEqual added in v0.9.1

func CommandEqual(command string) Predicate

CommandEqual is true if the message isn't nil, and it contains specified command

func CommandEqualArgc added in v0.10.1

func CommandEqualArgc(command string, argc int) Predicate

CommandEqualArgc is true if the message isn't nil, and it contains specified command with a number of args

func CommandEqualArgv added in v0.10.1

func CommandEqualArgv(command string, argv ...string) Predicate

CommandEqualArgv is true if the message isn't nil, and it contains specified command and args

func EditedCaptionContains added in v0.26.1

func EditedCaptionContains(text string) Predicate

EditedCaptionContains is true if the edited message isn't nil, and its caption contains specified text

func EditedCaptionEqual added in v0.26.1

func EditedCaptionEqual(text string) Predicate

EditedCaptionEqual is true if the edited message isn't nil, and its caption equals to the specified text

func EditedCaptionEqualFold added in v0.26.1

func EditedCaptionEqualFold(text string) Predicate

EditedCaptionEqualFold is true if the edited message isn't nil, and its caption equal fold (more general form of case-insensitivity equal) to the specified text

func EditedCaptionMatches added in v0.26.1

func EditedCaptionMatches(pattern *regexp.Regexp) Predicate

EditedCaptionMatches is true if the edited message isn't nil, and its caption matches specified regexp

func EditedCaptionPrefix added in v0.26.1

func EditedCaptionPrefix(prefix string) Predicate

EditedCaptionPrefix is true if the edited message isn't nil, and its caption has specified prefix

func EditedCaptionSuffix added in v0.26.1

func EditedCaptionSuffix(suffix string) Predicate

EditedCaptionSuffix is true if the edited message isn't nil, and its caption has specified suffix

func EditedPostCaptionContains added in v0.26.1

func EditedPostCaptionContains(text string) Predicate

EditedPostCaptionContains is true if edited channel post isn't nil, and its caption contains specified text

func EditedPostCaptionEqual added in v0.26.1

func EditedPostCaptionEqual(text string) Predicate

EditedPostCaptionEqual is true if edited channel post isn't nil, and its caption equals to the specified text

func EditedPostCaptionEqualFold added in v0.26.1

func EditedPostCaptionEqualFold(text string) Predicate

EditedPostCaptionEqualFold is true if edited channel post isn't nil, and its caption equal fold (more general form of case-insensitivity equal) to the specified text

func EditedPostCaptionMatches added in v0.26.1

func EditedPostCaptionMatches(pattern *regexp.Regexp) Predicate

EditedPostCaptionMatches is true if edited channel post isn't nil, and its caption matches specified regexp

func EditedPostCaptionPrefix added in v0.26.1

func EditedPostCaptionPrefix(prefix string) Predicate

EditedPostCaptionPrefix is true if edited channel post isn't nil, and its caption has specified prefix

func EditedPostCaptionSuffix added in v0.26.1

func EditedPostCaptionSuffix(suffix string) Predicate

EditedPostCaptionSuffix is true if edited channel post isn't nil, and its caption has specified suffix

func EditedPostTextContains added in v0.11.1

func EditedPostTextContains(text string) Predicate

EditedPostTextContains is true if edited channel post isn't nil, and its text contains specified text

func EditedPostTextEqual added in v0.11.1

func EditedPostTextEqual(text string) Predicate

EditedPostTextEqual is true if edited channel post isn't nil, and its text equals to the specified text

func EditedPostTextEqualFold added in v0.11.1

func EditedPostTextEqualFold(text string) Predicate

EditedPostTextEqualFold is true if edited channel post isn't nil, and its text equal fold (more general form of case-insensitivity equal) to the specified text

func EditedPostTextMatches added in v0.11.1

func EditedPostTextMatches(pattern *regexp.Regexp) Predicate

EditedPostTextMatches is true if edited channel post isn't nil, and its text matches specified regexp

func EditedPostTextPrefix added in v0.11.1

func EditedPostTextPrefix(prefix string) Predicate

EditedPostTextPrefix is true if edited channel post isn't nil, and its text has specified prefix

func EditedPostTextSuffix added in v0.11.1

func EditedPostTextSuffix(suffix string) Predicate

EditedPostTextSuffix is true if edited channel post isn't nil, and its text has specified suffix

func EditedTextContains added in v0.11.1

func EditedTextContains(text string) Predicate

EditedTextContains is true if the edited message isn't nil, and its text contains specified text

func EditedTextEqual added in v0.11.1

func EditedTextEqual(text string) Predicate

EditedTextEqual is true if the edited message isn't nil, and its text equals to the specified text

func EditedTextEqualFold added in v0.11.1

func EditedTextEqualFold(text string) Predicate

EditedTextEqualFold is true if the edited message isn't nil, and its text equal fold (more general form of case-insensitivity equal) to the specified text

func EditedTextMatches added in v0.11.1

func EditedTextMatches(pattern *regexp.Regexp) Predicate

EditedTextMatches is true if the edited message isn't nil, and its text matches specified regexp

func EditedTextPrefix added in v0.11.1

func EditedTextPrefix(prefix string) Predicate

EditedTextPrefix is true if the edited message isn't nil, and its text has specified prefix

func EditedTextSuffix added in v0.11.1

func EditedTextSuffix(suffix string) Predicate

EditedTextSuffix is true if the edited message isn't nil, and its text has specified suffix

func InlineQueryContains added in v0.11.1

func InlineQueryContains(text string) Predicate

InlineQueryContains is true if inline query isn't nil, and its query contains specified text

func InlineQueryEqual added in v0.11.1

func InlineQueryEqual(text string) Predicate

InlineQueryEqual is true if inline query isn't nil, and its query equal to specified text

func InlineQueryEqualFold added in v0.11.1

func InlineQueryEqualFold(text string) Predicate

InlineQueryEqualFold is true if inline query isn't nil, and its query equal fold (more general form of case-insensitivity equal) to the specified text

func InlineQueryMatches added in v0.11.1

func InlineQueryMatches(pattern *regexp.Regexp) Predicate

InlineQueryMatches is true if inline query isn't nil, and its query matches specified regexp

func InlineQueryPrefix added in v0.11.1

func InlineQueryPrefix(prefix string) Predicate

InlineQueryPrefix is true if inline query isn't nil, and its query has specified prefix

func InlineQuerySuffix added in v0.11.1

func InlineQuerySuffix(suffix string) Predicate

InlineQuerySuffix is true if inline query isn't nil, and its query has specified suffix

func None added in v0.27.2

func None() Predicate

None is always false

func Not added in v0.8.1

func Not(predicate Predicate) Predicate

Not is true if predicate is false

func Or added in v0.27.2

func Or(predicates ...Predicate) Predicate

Or is true if at least one of the predicates is true

func PostCaptionContains added in v0.26.1

func PostCaptionContains(text string) Predicate

PostCaptionContains is true if channel post isn't nil, and its caption contains specified text

func PostCaptionEqual added in v0.26.1

func PostCaptionEqual(text string) Predicate

PostCaptionEqual is true if channel post isn't nil, and its caption equals to the specified text

func PostCaptionEqualFold added in v0.26.1

func PostCaptionEqualFold(text string) Predicate

PostCaptionEqualFold is true if channel post isn't nil, and its caption equal fold (more general form of case-insensitivity equal) to the specified text

func PostCaptionMatches added in v0.26.1

func PostCaptionMatches(pattern *regexp.Regexp) Predicate

PostCaptionMatches is true if channel post isn't nil, and its caption matches specified regexp

func PostCaptionPrefix added in v0.26.1

func PostCaptionPrefix(prefix string) Predicate

PostCaptionPrefix is true if channel post isn't nil, and its caption has specified prefix

func PostCaptionSuffix added in v0.26.1

func PostCaptionSuffix(suffix string) Predicate

PostCaptionSuffix is true if channel post isn't nil, and its caption has specified suffix

func PostTextContains added in v0.11.1

func PostTextContains(text string) Predicate

PostTextContains is true if channel post isn't nil, and its text contains specified text

func PostTextEqual added in v0.11.1

func PostTextEqual(text string) Predicate

PostTextEqual is true if channel post isn't nil, and its text equals to the specified text

func PostTextEqualFold added in v0.11.1

func PostTextEqualFold(text string) Predicate

PostTextEqualFold is true if channel post isn't nil, and its text equal fold (more general form of case-insensitivity equal) to the specified text

func PostTextMatches added in v0.11.1

func PostTextMatches(pattern *regexp.Regexp) Predicate

PostTextMatches is true if channel post isn't nil, and its text matches specified regexp

func PostTextPrefix added in v0.11.1

func PostTextPrefix(prefix string) Predicate

PostTextPrefix is true if channel post isn't nil, and its text has specified prefix

func PostTextSuffix added in v0.11.1

func PostTextSuffix(suffix string) Predicate

PostTextSuffix is true if channel post isn't nil, and its text has specified suffix

func SuccessPayment added in v0.16.0

func SuccessPayment() Predicate

SuccessPayment is true if the message isn't nil, and contains success payment

func TextContains added in v0.10.1

func TextContains(text string) Predicate

TextContains is true if the message isn't nil, and its text contains specified text

func TextEqual added in v0.8.1

func TextEqual(text string) Predicate

TextEqual is true if the message isn't nil, and its text is equal to the specified text

func TextEqualFold added in v0.8.1

func TextEqualFold(text string) Predicate

TextEqualFold is true if the message isn't nil, and its text equal fold (more general form of case-insensitivity equal) to the specified text

func TextMatches added in v0.8.1

func TextMatches(pattern *regexp.Regexp) Predicate

TextMatches is true if the message isn't nil, and its text matches specified regexp

func TextPrefix added in v0.10.1

func TextPrefix(prefix string) Predicate

TextPrefix is true if the message isn't nil, and its text has specified prefix

func TextSuffix added in v0.10.1

func TextSuffix(suffix string) Predicate

TextSuffix is true if the message isn't nil, and its text has specified suffix

func Union deprecated added in v0.8.1

func Union(predicates ...Predicate) Predicate

Union is true if at least one of the predicates is true

Deprecated: Use Or predicate instead

type ShippingQueryHandler added in v0.11.1

type ShippingQueryHandler func(bot *telego.Bot, query telego.ShippingQuery)

ShippingQueryHandler handles shipping query that came from bot

type ShippingQueryHandlerCtx added in v0.27.0

type ShippingQueryHandlerCtx func(ctx context.Context, bot *telego.Bot, query telego.ShippingQuery)

ShippingQueryHandlerCtx handles shipping query that came from bot with context

Jump to

Keyboard shortcuts

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