Don't return slash in Command, strip bot name if needed.
Syfaro syfaro@foxpaw.in
Mon, 04 Jan 2016 11:45:46 -0600
2 files changed,
18 insertions(+),
2 deletions(-)
M
types.go
→
types.go
@@ -129,12 +129,20 @@ }
// 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,
M
types_test.go
→
types_test.go
@@ -58,7 +58,7 @@
func TestCommandWithCommand(t *testing.T) { message := tgbotapi.Message{Text: "/command"} - if message.Command() != "/command" { + if message.Command() != "command" { t.Fail() } }@@ -75,6 +75,14 @@ func TestCommandWithNonCommand(t *testing.T) {
message := tgbotapi.Message{Text: "test text"} if message.Command() != "" { + t.Fail() + } +} + +func TestCommandWithBotName(t *testing.T) { + message := tgbotapi.Message{Text: "/command@testbot"} + + if message.Command() != "command" { t.Fail() } }