From f0dd89236c7c95ccdc3bf05c7f5663ba0c400411 Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Wed, 24 Jan 2024 22:08:57 +0000 Subject: [PATCH] Add triggers helper, to ease customising command triggers --- ext/handlers/command.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ext/handlers/command.go b/ext/handlers/command.go index 93645042..dd08a9bc 100644 --- a/ext/handlers/command.go +++ b/ext/handlers/command.go @@ -8,14 +8,21 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2/ext" ) +// Command is the go-to handler for setting up Commands in your bot. By default, it will use telegram-native commands +// that start with a forward-slash (/), but it can be customised to react to any message starting with a character. +// +// For example, a command handler on "help" with the triggers []rune("/!,") would trigger for "/help", "!help", or ",help". type Command struct { Triggers []rune AllowEdited bool AllowChannel bool - Command string // should be lowercase for case-insensitivity + Command string // set to a lowercase value for case-insensitivity Response Response } +// NewCommand creates a new case-insensitive command. +// By default, commands do not work on edited messages, or channel posts. These can be enabled by setting the +// AllowEdited and AllowChannel fields respectively. func NewCommand(c string, r Response) Command { return Command{ Triggers: []rune{'/'}, @@ -38,6 +45,12 @@ func (c Command) SetAllowChannel(allow bool) Command { return c } +// SetTriggers sets the list of triggers to be used with this command. +func (c Command) SetTriggers(triggers []rune) Command { + c.Triggers = triggers + return c +} + func (c Command) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool { if ctx.Message != nil { if ctx.Message.Text == "" && ctx.Message.Caption == "" {