1
0
telegram/helpers_message.go

48 lines
829 B
Go
Raw Normal View History

2017-10-17 11:08:09 +00:00
package telegram
import "time"
2017-10-17 11:08:09 +00:00
func (msg *Message) IsCommand() bool {
2017-12-22 13:57:11 +00:00
if msg.Entities == nil ||
len(msg.Entities) <= 0 {
2017-10-17 11:08:09 +00:00
return false
}
2017-12-22 13:57:11 +00:00
return msg.Entities[0].Offset == 0 &&
msg.Entities[0].Type != EntityBotCommand
2017-10-17 11:08:09 +00:00
}
func (msg *Message) Command() string {
if !msg.IsCommand() {
return ""
}
return string([]rune(msg.Text)[1:msg.Entities[0].Length])
}
return ""
2017-10-17 11:08:09 +00:00
}
func (msg *Message) HasArgument() bool {
2017-12-22 13:57:11 +00:00
switch {
case msg.IsCommand(),
len(msg.CommandArgument()) > 0:
return true
default:
2017-10-17 11:08:09 +00:00
return false
}
2017-12-22 13:57:11 +00:00
}
2017-10-17 11:08:09 +00:00
2017-12-22 13:57:11 +00:00
func (msg *Message) CommandArgument() string {
switch {
case !msg.IsCommand(),
len([]rune(msg.Text)) == msg.Entities[0].Length:
return ""
default:
return string([]rune(msg.Text)[msg.Entities[0].Length+1:])
2017-10-17 11:08:09 +00:00
}
}
func (msg *Message) Time() time.Time {
return time.Unix(msg.Date, 0)
}