From 93225440a7eab2bb91e2ee94573cabf7e2001769 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Mon, 4 Jan 2016 11:45:46 -0600 Subject: [PATCH] Don't return slash in Command, strip bot name if needed. --- types.go | 10 +++++++++- types_test.go | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) 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" {