all repos — telegram-bot-api @ b7c9b5002015dbd29e5de7c67491ef8c9a0f4908

Golang bindings for the Telegram Bot API

Don't return slash in Command, strip bot name if needed.
Syfaro syfaro@foxpaw.in
Mon, 04 Jan 2016 11:45:46 -0600
commit

b7c9b5002015dbd29e5de7c67491ef8c9a0f4908

parent

5df7aae78f6647147f98154e618cc7af818cad26

2 files changed, 18 insertions(+), 2 deletions(-)

jump to
M types.gotypes.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.gotypes_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() } }