filters

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 3 Imported by: 1

README

TGO Filters

TGO Filters are set of functions that can be used with routers to determine which handler should be called and which should not.

Example:

package main

import (
    ...
    "github.com/haashemi/tgo/filters"
)

func main() {
    // ...

    mr := messages.NewRouter()

    // startCommandHandler will only get called if the Command filter approves it.
    mr.Handle(filters.Command("start", botUsername), startCommandHandler)

    bot.AddRouter(mr)

    // ...
}

Built-in filters:

Logical filters:
  • filters.True()
  • filters.False()
  • filters.And(...)
  • filters.Or(...)
  • filters.Not(...)
Message filters:
  • filters.IsPrivate()
  • filters.Command(...)
  • filters.Commands(...)
General filters:

General filters are currently working with Message, CallbackQuery, and InlineQuery.

  • filters.Text(...)
  • filters.Texts(...)
  • filters.WithPrefix(...)
  • filters.WithSuffix(...)
  • filters.Regex(...)
  • filters.Whitelist(...)
Update filters:
  • filters.HasMessage()
  • filters.IsMessage()
  • filters.IsEditedMessage()
  • filters.IsChannelPost()
  • filters.IsEditedChannelPost()
  • filters.IsInlineQuery()
  • filters.IsChosenInlineResult()
  • filters.IsCallbackQuery()
  • filters.IsShippingQuery()
  • filters.IsPreCheckoutQuery()
  • filters.IsPoll()
  • filters.IsPollAnswer()
  • filters.IsMyChatMember()
  • filters.IsChatMember()
  • filters.IsChatJoinRequest()

How to implement your own filter?

It's simple! just pass your filter function to filters.NewFilter and you're done!

Here is an example:

// It can be with a variable, fastest way.
var myInlineFilter = filters.NewFilter(func(update *tgo.Update) bool {
    // your filter's logic goes here
})

// Or it can be its own separated function.
func MyFilter() *tgo.Filter {
    return filters.NewFilter(func(update *tgo.Update) bool {
        // your filter's logic goes here
    })
}

Documentation

Overview

Filters are set of functions that can be used with routers to determine which handler should be called and which should not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And(filters ...tgo.Filter) tgo.Filter

And Behaves like the && operator; returns true if all of the passes filters passes, otherwise returns false.

func Command

func Command(cmd, botUsername string) tgo.Filter

Commands tests if the message's (and only message) text or caption matches the cmd.

func Commands

func Commands(botUsername string, cmds ...string) tgo.Filter

Commands tests if the message's (and only message) text or caption matches any of the cmds.

func False

func False() tgo.Filter

False does nothing and just always returns false.

func HasMessage

func HasMessage() tgo.Filter

func IsCallbackQuery

func IsCallbackQuery() tgo.Filter

func IsChannelPost

func IsChannelPost() tgo.Filter

func IsChatJoinRequest

func IsChatJoinRequest() tgo.Filter

func IsChatMember

func IsChatMember() tgo.Filter

func IsChosenInlineResult

func IsChosenInlineResult() tgo.Filter

func IsEditedChannelPost

func IsEditedChannelPost() tgo.Filter

func IsEditedMessage

func IsEditedMessage() tgo.Filter

func IsInlineQuery

func IsInlineQuery() tgo.Filter

func IsMessage

func IsMessage() tgo.Filter

func IsMyChatMember

func IsMyChatMember() tgo.Filter

func IsPoll

func IsPoll() tgo.Filter

func IsPollAnswer

func IsPollAnswer() tgo.Filter

func IsPreCheckoutQuery

func IsPreCheckoutQuery() tgo.Filter

func IsPrivate added in v1.0.1

func IsPrivate() tgo.Filter

IsPrivate checks if the message (and only message) is inside the private chat.

func IsShippingQuery

func IsShippingQuery() tgo.Filter

func Not

func Not(filter tgo.Filter) tgo.Filter

Not Behaves like the ! operator; returns the opposite of the filter result

func Or

func Or(filters ...tgo.Filter) tgo.Filter

Or behaves like the || operator; returns true if at least one of the passed filters passes. returns false if none of them passes.

func Regex

func Regex(reg *regexp.Regexp) tgo.Filter

Regex matches the update's text with the reg.

Currently works with Message's caption and text, CallbackQuery's data, and InlineQuery's query.

func Text

func Text(text string) tgo.Filter

Text checks the update's text is equal to the text.

Currently works with Message's caption and text, CallbackQuery's data, and InlineQuery's query.

func Texts

func Texts(texts ...string) tgo.Filter

Text checks the update's text is in the texts.

Currently works with Message's caption and text, CallbackQuery's data, and InlineQuery's query.

func True

func True() tgo.Filter

True does nothing and just always returns true.

func Whitelist

func Whitelist(IDs ...int64) tgo.Filter

Whitelist checks if the update is from the whitelisted IDs.

Currently works with Message and CallbackQuery, and InlineQuery.

func WithPrefix

func WithPrefix(prefix string) tgo.Filter

WithPrefix tests whether the update's text begins with prefix.

Currently works with Message's caption and text, CallbackQuery's data, and InlineQuery's query.

func WithSuffix

func WithSuffix(suffix string) tgo.Filter

WithSuffix tests whether the update's text ends with suffix.

Currently works with Message's caption and text, CallbackQuery's data, and InlineQuery's query.

Types

type Filter

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

Filter does nothing and just holds a FilterFunc.

func NewFilter

func NewFilter(f FilterFunc) *Filter

NewFilter returns a Filter from FilterFunc f.

func (Filter) Check

func (f Filter) Check(update *tgo.Update) bool

Check calls the FilterFunc and returns the result.

type FilterFunc

type FilterFunc func(update *tgo.Update) bool

FilterFunc tests the update with its own filters.

Jump to

Keyboard shortcuts

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