tgchain

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2023 License: MIT Imports: 2 Imported by: 0

README

tgchain

Chained wrapper for handling tg updates

Example
package main

import (
	"log"
	"context"

	"github.com/go-telegram-bot-api/telegram-bot-api"
	"github.com/egnd/go-toolbox/tg/tgchain"
)

func main() {
	api, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Panic(err)
	}

	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60
	updates, err := api.GetUpdatesChan(u)

	tgchain.NewListener(nil, nil)
		.Add(tgchain.EventMessage, 
			// @TODO: your handlers, children of IEventHandler
		)
		.Listen(context.Background(), updates)
}

Documentation

Overview

Package tgchain has chained wrapper for handling tg updates

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEventName

func GetEventName(eventID EventType) string

GetEventName return event name by it's ID.

Types

type AbstractHandler

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

AbstractHandler is a abstract event handler.

func (*AbstractHandler) Decorate

func (h *AbstractHandler) Decorate(next IEventHandler)

Decorate is a setter for next handler.

func (*AbstractHandler) GetDescription

func (h *AbstractHandler) GetDescription() (descr string)

GetDescription returns handler description.

func (*AbstractHandler) GetName

func (h *AbstractHandler) GetName() string

GetName returns handler name.

func (*AbstractHandler) Next

func (h *AbstractHandler) Next(ctx context.Context, upd tgbotapi.Update) error

Next pass update to next handler in chain.

func (*AbstractHandler) ReplyToMsg

func (h *AbstractHandler) ReplyToMsg(msg *tgbotapi.Message, text string, api ITgAPI) (err error)

ReplyToMsg sends reply to tg message.

type CtxEventKeyType

type CtxEventKeyType int

CtxEventKeyType is a type for event name in context.

const (
	// CtxEventKey context key for storing event type ID.
	CtxEventKey CtxEventKeyType = iota
)

type ErrUpd

type ErrUpd func(string, tgbotapi.Update, error)

ErrUpd is a method for logging errors.

type EventType

type EventType int

EventType variable type for event type ID.

const (
	// EventUndefined undefined event.
	EventUndefined EventType = iota
	// EventMessage receiving message event.
	EventMessage
	// EventCommand receiving command event.
	EventCommand
	// EventInlineQuery receiving inline query event.
	EventInlineQuery
	// EventCallbackQuery receiving callback query event.
	EventCallbackQuery
)

func GetEventFrom

func GetEventFrom(upd tgbotapi.Update) EventType

GetEventFrom return event type from tg update.

func GetEventFromCtx

func GetEventFromCtx(ctx context.Context) EventType

GetEventFromCtx return event ID from context struct.

type IEventHandler

type IEventHandler interface {
	GetName() string
	GetDescription() string
	Decorate(IEventHandler)
	Next(context.Context, tgbotapi.Update) error
	Handle(context.Context, tgbotapi.Update) error
}

IEventHandler is event handler interface.

type IListener

type IListener interface {
	Add(EventType, ...IEventHandler) IListener
	Listen(context.Context, tgbotapi.UpdatesChannel)
}

IListener is a tg updates listener interface.

type ITgAPI

ITgAPI is interface for Telegram API instance.

type Listener

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

Listener is struct which is listening and handling events.

func NewListener

func NewListener(warnUpd WarnUpd, errUpd ErrUpd) *Listener

NewListener constructor for Listener struct.

func (*Listener) Add

func (b *Listener) Add(event EventType, handlers ...IEventHandler) IListener

Add adds decorators for handling specific Telegram event.

func (*Listener) Listen

func (b *Listener) Listen(ctx context.Context, updChan tgbotapi.UpdatesChannel)

Listen starts listening incoming messages in channel.

type WarnUpd

type WarnUpd func(string, tgbotapi.Update)

WarnUpd is a method for logging warnings.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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