Merge pull request #125 from Henner25/fix_message_command_functions
Rewrite message command methods to use entitiesbot-api-6.1
commit
846d467e14
50
types.go
50
types.go
|
@ -173,21 +173,23 @@ func (m *Message) Time() time.Time {
|
||||||
return time.Unix(int64(m.Date), 0)
|
return time.Unix(int64(m.Date), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsCommand returns true if message starts with '/'.
|
// IsCommand returns true if message starts with a "bot_command" entity.
|
||||||
func (m *Message) IsCommand() bool {
|
func (m *Message) IsCommand() bool {
|
||||||
return m.Text != "" && m.Text[0] == '/'
|
if m.Entities == nil || len(*m.Entities) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
entity := (*m.Entities)[0]
|
||||||
|
return entity.Offset == 0 && entity.Type == "bot_command"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// If the command contains the at name syntax, it is removed. Use
|
||||||
|
// CommandWithAt() if you do not want that.
|
||||||
func (m *Message) Command() string {
|
func (m *Message) Command() string {
|
||||||
if !m.IsCommand() {
|
command := m.CommandWithAt()
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
command := strings.SplitN(m.Text, " ", 2)[0][1:]
|
|
||||||
|
|
||||||
if i := strings.Index(command, "@"); i != -1 {
|
if i := strings.Index(command, "@"); i != -1 {
|
||||||
command = command[:i]
|
command = command[:i]
|
||||||
|
@ -196,20 +198,42 @@ func (m *Message) Command() string {
|
||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CommandWithAt 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 name syntax, it is not removed. Use Command()
|
||||||
|
// if you want that.
|
||||||
|
func (m *Message) CommandWithAt() string {
|
||||||
|
if !m.IsCommand() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCommand() checks that the message begins with a bot_command entity
|
||||||
|
entity := (*m.Entities)[0]
|
||||||
|
return m.Text[1:entity.Length]
|
||||||
|
}
|
||||||
|
|
||||||
// CommandArguments checks if the message was a command and if it was,
|
// CommandArguments checks if the message was a command and if it was,
|
||||||
// returns all text after the command name. If the Message was not a
|
// returns all text after the command name. If the Message was not a
|
||||||
// command, it returns an empty string.
|
// command, it returns an empty string.
|
||||||
|
//
|
||||||
|
// Note: The first character after the command name is omitted:
|
||||||
|
// - "/foo bar baz" yields "bar baz", not " bar baz"
|
||||||
|
// - "/foo-bar baz" yields "bar baz", too
|
||||||
|
// Even though the latter is not a command conforming to the spec, the API
|
||||||
|
// marks "/foo" as command entity.
|
||||||
func (m *Message) CommandArguments() string {
|
func (m *Message) CommandArguments() string {
|
||||||
if !m.IsCommand() {
|
if !m.IsCommand() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.SplitN(m.Text, " ", 2)
|
// IsCommand() checks that the message begins with a bot_command entity
|
||||||
if len(split) != 2 {
|
entity := (*m.Entities)[0]
|
||||||
return ""
|
if len(m.Text) == entity.Length {
|
||||||
|
return "" // The command makes up the whole message
|
||||||
|
} else {
|
||||||
|
return m.Text[entity.Length+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
return split[1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageEntity contains information about data in a Message.
|
// MessageEntity contains information about data in a Message.
|
||||||
|
|
|
@ -34,6 +34,7 @@ func TestMessageTime(t *testing.T) {
|
||||||
|
|
||||||
func TestMessageIsCommandWithCommand(t *testing.T) {
|
func TestMessageIsCommandWithCommand(t *testing.T) {
|
||||||
message := tgbotapi.Message{Text: "/command"}
|
message := tgbotapi.Message{Text: "/command"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
|
||||||
|
|
||||||
if message.IsCommand() != true {
|
if message.IsCommand() != true {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -58,6 +59,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"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
|
||||||
|
|
||||||
if message.Command() != "command" {
|
if message.Command() != "command" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -82,19 +84,38 @@ func TestCommandWithNonCommand(t *testing.T) {
|
||||||
|
|
||||||
func TestCommandWithBotName(t *testing.T) {
|
func TestCommandWithBotName(t *testing.T) {
|
||||||
message := tgbotapi.Message{Text: "/command@testbot"}
|
message := tgbotapi.Message{Text: "/command@testbot"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
|
||||||
|
|
||||||
if message.Command() != "command" {
|
if message.Command() != "command" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCommandWithAtWithBotName(t *testing.T) {
|
||||||
|
message := tgbotapi.Message{Text: "/command@testbot"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
|
||||||
|
|
||||||
|
if message.CommandWithAt() != "command@testbot" {
|
||||||
|
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"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
|
||||||
if message.CommandArguments() != "with arguments" {
|
if message.CommandArguments() != "with arguments" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMessageCommandArgumentsWithMalformedArguments(t *testing.T) {
|
||||||
|
message := tgbotapi.Message{Text: "/command-without argument space"}
|
||||||
|
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
|
||||||
|
if message.CommandArguments() != "without argument space" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
|
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
|
||||||
message := tgbotapi.Message{Text: "/command"}
|
message := tgbotapi.Message{Text: "/command"}
|
||||||
if message.CommandArguments() != "" {
|
if message.CommandArguments() != "" {
|
||||||
|
|
Loading…
Reference in New Issue