Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add triggers helper method #140

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ext/handlers/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{'/'},
Expand All @@ -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 == "" {
Expand Down
Loading