botx

package
v0.0.0-...-407f28a Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package botx provides interfaces and types to handle bot updates, with a chi-like router.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API interface {
	Updates() <-chan Request
	SendMessage(ctx context.Context, resp Response) error
}

API defines methods for an API interface to receive and send chat messages.

type Bot

type Bot struct {
	Options
	// contains filtered or unexported fields
}

Bot defines parameters for running a bot over some API.

func NewBot

func NewBot(h Handler, api API, opts ...Option) *Bot

NewBot creates a new Bot.

func (*Bot) Run

func (b *Bot) Run(ctx context.Context)

Run starts updates listener.

type Chat

type Chat struct {
	ID       string
	Username string
}

Chat contains chat information.

type Handler

type Handler func(ctx context.Context, req Request) ([]Response, error)

Handler handles requests.

func (Handler) With

func (h Handler) With(mvs ...func(Handler) Handler) Handler

With returns a new handler with middleware applied.

type Middleware

type Middleware func(Handler) Handler

Middleware specifies a basic middleware for bot handler.

type Option

type Option func(*Options)

Option defines a function that configures Bot.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger to use.

func WithWorkers

func WithWorkers(workers int) Option

WithWorkers sets the number of workers to run.

type Options

type Options struct {
	Workers int
	Logger  *slog.Logger
}

Options defines options for Bot.

type Request

type Request struct {
	MessageID string
	Chat      Chat
	Text      string
}

Request is a request for handler.

type Response

type Response struct {
	ReplyToMessageID string
	ChatID           string
	Text             string
}

Response is a response from handler.

func NotFound

func NotFound(_ context.Context, req Request) ([]Response, error)

NotFound is a default handler for not found commands.

type Router

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

Router returns a multiplexer for handlers.

func NewRouter

func NewRouter() *Router

NewRouter returns a multiplexer for handlers.

func (*Router) Add

func (r *Router) Add(prefix string, h Handler)

Add adds a handler to the router.

func (*Router) Clone

func (r *Router) Clone() *Router

Clone returns a copy of the router.

func (*Router) Group

func (r *Router) Group(f func(rtr *Router))

Group groups handlers.

func (*Router) Handle

func (r *Router) Handle(ctx context.Context, req Request) ([]Response, error)

Handle handles request.

func (*Router) NotFound

func (r *Router) NotFound(h Handler)

NotFound sets a not found handler to the router.

func (*Router) Use

func (r *Router) Use(mvs ...Middleware) *Router

Use applies middleware to all handlers.

func (*Router) With

func (r *Router) With(mvs ...Middleware) *Router

With returns a new router with middleware applied.

Directories

Path Synopsis
Package botapi contains implementations of bot API interfaces.
Package botapi contains implementations of bot API interfaces.
Package botmw provides middlewares for bot handler.
Package botmw provides middlewares for bot handler.

Jump to

Keyboard shortcuts

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