Don't return slash in Command, strip bot name if needed.

bot-api-6.1
Syfaro 2016-01-04 11:45:46 -06:00
parent 5df7aae78f
commit b7c9b50020
2 changed files with 18 additions and 2 deletions

View File

@ -129,12 +129,20 @@ func (m *Message) IsCommand() bool {
// Command checks if the message was a command and if it was, returns the // 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. // 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 { func (m *Message) Command() string {
if !m.IsCommand() { if !m.IsCommand() {
return "" 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, // CommandArguments checks if the message was a command and if it was,

View File

@ -58,7 +58,7 @@ func TestIsCommandWithEmptyText(t *testing.T) {
func TestCommandWithCommand(t *testing.T) { func TestCommandWithCommand(t *testing.T) {
message := tgbotapi.Message{Text: "/command"} message := tgbotapi.Message{Text: "/command"}
if message.Command() != "/command" { if message.Command() != "command" {
t.Fail() 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) { func TestMessageCommandArgumentsWithArguments(t *testing.T) {
message := tgbotapi.Message{Text: "/command with arguments"} message := tgbotapi.Message{Text: "/command with arguments"}
if message.CommandArguments() != "with arguments" { if message.CommandArguments() != "with arguments" {