diff --git a/types.go b/types.go index 8e49ad4..a8a9c7e 100644 --- a/types.go +++ b/types.go @@ -129,12 +129,20 @@ func (m *Message) IsCommand() bool { // Command checks if the message was a command and if it was, returns the // command. If the Message was not a command, it returns an empty string. +// +// If the command contains the at bot syntax, it removes the bot name. func (m *Message) Command() string { if !m.IsCommand() { return "" } - return strings.SplitN(m.Text, " ", 2)[0] + command := strings.SplitN(m.Text, " ", 2)[0][1:] + + if i := strings.Index(command, "@"); i != -1 { + command = command[:i] + } + + return command } // CommandArguments checks if the message was a command and if it was, diff --git a/types_test.go b/types_test.go index 8e10e22..6588251 100644 --- a/types_test.go +++ b/types_test.go @@ -58,7 +58,7 @@ func TestIsCommandWithEmptyText(t *testing.T) { func TestCommandWithCommand(t *testing.T) { message := tgbotapi.Message{Text: "/command"} - if message.Command() != "/command" { + if message.Command() != "command" { t.Fail() } } @@ -79,6 +79,14 @@ func TestCommandWithNonCommand(t *testing.T) { } } +func TestCommandWithBotName(t *testing.T) { + message := tgbotapi.Message{Text: "/command@testbot"} + + if message.Command() != "command" { + t.Fail() + } +} + func TestMessageCommandArgumentsWithArguments(t *testing.T) { message := tgbotapi.Message{Text: "/command with arguments"} if message.CommandArguments() != "with arguments" {