Skip to content

MTGo Filters

Filters narrow which updates trigger a handler. They compose with And, Or, and Not.

Using Filters

Pass filters as the second argument to OnMessage and other registration methods:

go
client.OnMessage(handler, tg.Private)
client.OnMessage(handler, tg.Private.And(tg.Text("hello")))

Built-in Filters

Chat Type

FilterMatches
PrivateDMs (positive ChatID)
GroupGroup messages (negative ChatID)
ChannelChannel posts
DirectIncoming DMs (private + incoming)
ForumMessages from forum-enabled supergroups
BusinessBusiness connection messages

Message Direction

FilterMatches
IncomingNon-outgoing messages
OutgoingMessages sent by the current user
MeOutgoing messages (alias)

Content Type

FilterMatches
MediaAny media attachment
PhotoPhoto attachments
VideoVideo attachments
AudioAudio attachments
VoiceVoice notes
VideoNoteRound video notes
StickerStickers
DocumentGeneric file documents
AnimationGIFs/animations
ContactContact cards
LocationStatic location pins
LiveLocationLive updating locations
VenueVenue info
WebPageLink previews
PollPoll messages
DiceDice/animated emoji
GameHTML5 game attachments
StoryForwarded stories
PaidMediaPaid media (Stars)
InvoicePayment invoices
MediaGroupAlbum messages
CaptionMedia with caption only

Text Matching

FilterMatches
Text("hello")Exact text match
Command("start")/start command
Regex("\\d+")Regex pattern match
AllEvery update

Sender & Chat

FilterMatches
User(123, 456)Specific user IDs
Chat(-100123)Specific chat IDs
Topic(42)Specific forum topic
SenderChat(-100)Specific sender chat
BotMessages from bots

Message Properties

FilterMatches
ReplyMessages that reply to another
ForwardedForwarded messages
MentionedMessages where you're @mentioned
ViaBotMessages sent via inline bot
PinnedPinned messages
MediaSpoilerSpoiler-hidden media
ServiceService/system messages

Service Message Filters

FilterMatches
NewChatMembersMember join events
LeftChatMemberMember leave events
NewChatTitleTitle change events
NewChatPhotoPhoto change events
DeleteChatPhotoPhoto deletion events
GroupChatCreatedGroup creation events
PinnedMessagePin notification events

Callback & Inline

FilterMatches
CallbackData("approve")Exact callback data match
CallbackRegex("^page_\\d+$")Regex on callback data
InlineQueryText("search")Exact inline query text

Composition

Filters compose with three operators:

go
// AND — both must match
tg.Private.And(tg.Text("hello"))

// OR — either matches
tg.Photo.Or(tg.Video)

// NOT — negate a filter
tg.Bot.Not() // non-bot messages

Chain multiple:

go
tg.Private.And(tg.Incoming).And(tg.Command("start"))

Custom Filters

Use Create for filters that need client access:

go
tg.Create(func(c *tg.Client, ctx *tg.Context) bool {
    return isAdmin(c, ctx.Message.FromID)
})

Custom Command Filter

NewCommand creates a command filter with custom prefixes and case sensitivity:

go
tg.NewCommand(
    []string{"start", "help"},  // commands
    []string{"!", "/"},          // prefixes
    false,                       // case-insensitive
)

Released under the Apache-2.0 License.