2017-10-17 11:08:09 +00:00
|
|
|
package telegram
|
|
|
|
|
2018-01-25 12:09:15 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
2017-10-17 11:08:09 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsCommand checks that the current message is a bot command.
|
2018-04-19 10:56:25 +00:00
|
|
|
func (msg *Message) IsCommand() bool {
|
|
|
|
if !msg.IsText() || !msg.HasEntities() {
|
2018-01-25 12:09:15 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
entity := msg.Entities[0]
|
2018-04-19 10:56:25 +00:00
|
|
|
return entity.IsBotCommand() && entity.Offset == 0
|
|
|
|
}
|
2018-01-29 09:16:54 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsCommandEqual checks that the current message is a specific bot command.
|
2018-04-19 10:56:25 +00:00
|
|
|
func (msg *Message) IsCommandEqual(command string) bool {
|
|
|
|
return msg.IsCommand() && strings.EqualFold(msg.Command(), command)
|
2017-10-17 11:08:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// Command returns identifier of the bot command without bot username, if it was
|
|
|
|
// available
|
2017-10-17 11:08:09 +00:00
|
|
|
func (msg *Message) Command() string {
|
2018-04-19 10:56:25 +00:00
|
|
|
if !msg.IsCommand() {
|
2017-10-17 11:08:09 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-01-25 12:09:15 +00:00
|
|
|
return strings.Split(msg.RawCommand(), "@")[0]
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// RawCommand returns identifier of the bot command with bot username, if it was
|
|
|
|
// available
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) RawCommand() string {
|
2018-04-19 10:56:25 +00:00
|
|
|
if !msg.IsCommand() {
|
2018-01-25 12:09:15 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:08:09 +00:00
|
|
|
return string([]rune(msg.Text)[1:msg.Entities[0].Length])
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasCommandArgument checks that the current command message contains argument.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) HasCommandArgument() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
if !msg.IsCommand() {
|
2018-01-25 12:09:15 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
entity := msg.Entities[0]
|
|
|
|
if !entity.IsBotCommand() {
|
2017-10-17 11:08:09 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-25 12:09:15 +00:00
|
|
|
|
|
|
|
return len([]rune(msg.Text)) != entity.Length
|
2017-12-22 13:57:11 +00:00
|
|
|
}
|
2017-10-17 11:08:09 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// CommandArgument returns raw command argument.
|
2017-12-22 13:57:11 +00:00
|
|
|
func (msg *Message) CommandArgument() string {
|
2018-01-25 12:09:15 +00:00
|
|
|
if !msg.HasCommandArgument() {
|
2017-12-22 13:57:11 +00:00
|
|
|
return ""
|
2017-10-17 11:08:09 +00:00
|
|
|
}
|
2018-01-25 12:09:15 +00:00
|
|
|
|
|
|
|
return string([]rune(msg.Text)[msg.Entities[0].Length+1:])
|
2017-10-17 11:08:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsReply checks that the current message is a reply on other message.
|
2017-12-22 13:57:38 +00:00
|
|
|
func (msg *Message) IsReply() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
return msg != nil && msg.ReplyToMessage != nil
|
2017-12-22 13:57:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsForward checks that the current message is a forward of other message.
|
2017-12-22 13:59:34 +00:00
|
|
|
func (msg *Message) IsForward() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
return msg != nil && msg.ForwardFrom != nil
|
2017-12-22 13:59:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// Time parse current message Date and returns time.Time.
|
2017-10-17 11:08:09 +00:00
|
|
|
func (msg *Message) Time() time.Time {
|
2018-01-25 12:09:15 +00:00
|
|
|
if msg == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:08:09 +00:00
|
|
|
return time.Unix(msg.Date, 0)
|
|
|
|
}
|
2018-01-25 12:09:15 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// ForwardTime parse current message ForwardDate and returns time.Time.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) ForwardTime() time.Time {
|
|
|
|
if msg == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Unix(msg.ForwardDate, 0)
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// EditTime parse current message EditDate and returns time.Time.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) EditTime() time.Time {
|
2018-04-12 11:53:29 +00:00
|
|
|
var t time.Time
|
2018-04-19 10:56:25 +00:00
|
|
|
if msg == nil || !msg.HasBeenEdited() {
|
2018-04-12 11:53:29 +00:00
|
|
|
return t
|
2018-01-29 09:30:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 12:09:15 +00:00
|
|
|
return time.Unix(msg.EditDate, 0)
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasBeenEdited checks that the current message has been edited.
|
2018-01-29 09:30:01 +00:00
|
|
|
func (msg *Message) HasBeenEdited() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
return msg != nil && msg.EditDate > 0
|
2018-01-29 09:30:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsText checks that the current message is just a text message.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsText() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
return msg != nil && msg.Text != ""
|
2018-01-25 12:09:15 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsAudio checks that the current message is a audio.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsAudio() bool {
|
|
|
|
return !msg.IsText() && msg.Audio != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsDocument checks that the current message is a document.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsDocument() bool {
|
|
|
|
return !msg.IsText() && msg.Document != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsGame checks that the current message is a game.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsGame() bool {
|
|
|
|
return !msg.IsText() && msg.Game != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsPhoto checks that the current message is a photo.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsPhoto() bool {
|
|
|
|
return !msg.IsText() && len(msg.Photo) > 0
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsSticker checks that the current message is a sticker.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsSticker() bool {
|
|
|
|
return !msg.IsText() && msg.Sticker != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsVideo checks that the current message is a video.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsVideo() bool {
|
|
|
|
return !msg.IsText() && msg.Video != nil
|
|
|
|
}
|
2018-04-12 12:56:53 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsVoice checks that the current message is a voice.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsVoice() bool {
|
|
|
|
return !msg.IsText() && msg.Voice != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsVideoNote checks that the current message is a video note.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsVideoNote() bool {
|
|
|
|
return !msg.IsText() && msg.VideoNote != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsContact checks that the current message is a contact.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsContact() bool {
|
|
|
|
return !msg.IsText() && msg.Contact != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsLocation checks that the current message is a location.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsLocation() bool {
|
|
|
|
return !msg.IsText() && msg.Location != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsVenue checks that the current message is a venue.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsVenue() bool {
|
|
|
|
return !msg.IsText() && msg.Venue != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsNewChatMembersEvent checks that the current message is a event of entry of
|
|
|
|
// new members.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsNewChatMembersEvent() bool {
|
|
|
|
return !msg.IsText() && len(msg.NewChatMembers) > 0
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsLeftChatMemberEvent checks that the current message is a event of members
|
|
|
|
// exit.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsLeftChatMemberEvent() bool {
|
|
|
|
return !msg.IsText() && msg.LeftChatMember != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsNewChatTitleEvent checks that the current message is a event of setting a
|
|
|
|
// new chat title.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsNewChatTitleEvent() bool {
|
|
|
|
return !msg.IsText() && msg.NewChatTitle != ""
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsNewChatPhotoEvent checks that the current message is a event of setting a
|
|
|
|
// new chat avatar.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsNewChatPhotoEvent() bool {
|
|
|
|
return !msg.IsText() && len(msg.NewChatPhoto) > 0
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsDeleteChatPhotoEvent checks that the current message is a event of deleting
|
|
|
|
// a chat avatar.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsDeleteChatPhotoEvent() bool {
|
|
|
|
return !msg.IsText() && msg.DeleteChatPhoto
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsGroupChatCreatedEvent checks that the current message is a event of creating
|
|
|
|
// a new group.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsGroupChatCreatedEvent() bool {
|
|
|
|
return !msg.IsText() && msg.GroupChatCreated
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsSupergroupChatCreatedEvent checks that the current message is a event of
|
|
|
|
// creating a new supergroup.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsSupergroupChatCreatedEvent() bool {
|
|
|
|
return !msg.IsText() && msg.SupergroupChatCreated
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsChannelChatCreatedEvent checks that the current message is a event of
|
|
|
|
// creating a new channel.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsChannelChatCreatedEvent() bool {
|
|
|
|
return !msg.IsText() && msg.ChannelChatCreated
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsPinnedMessage checks that the current message is a event of pinning another
|
|
|
|
// message.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsPinnedMessage() bool {
|
|
|
|
return !msg.IsText() && msg.PinnedMessage != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsInvoice checks that the current message is a invoice.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsInvoice() bool {
|
|
|
|
return !msg.IsText() && msg.Invoice != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// IsSuccessfulPayment checks that the current message is a event of successful
|
|
|
|
// payment.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) IsSuccessfulPayment() bool {
|
|
|
|
return !msg.IsText() && msg.SuccessfulPayment != nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasEntities checks that the current message contains entities.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) HasEntities() bool {
|
|
|
|
return msg.IsText() && len(msg.Entities) > 0
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasCaptionEntities checks that the current media contains entities in caption.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) HasCaptionEntities() bool {
|
|
|
|
return !msg.IsText() && len(msg.CaptionEntities) > 0
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasMentions checks that the current message contains mentions.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) HasMentions() bool {
|
|
|
|
if !msg.HasEntities() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entity := range msg.Entities {
|
|
|
|
if entity.IsMention() || entity.IsTextMention() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasCaptionMentions checks that the current media contains mentions in caption.
|
2018-01-25 12:09:15 +00:00
|
|
|
func (msg *Message) HasCaptionMentions() bool {
|
|
|
|
if !msg.HasCaptionEntities() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entity := range msg.CaptionEntities {
|
|
|
|
if entity.IsMention() || entity.IsTextMention() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-01-29 08:02:15 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasCaption checks that the current media has caption.
|
2018-01-29 08:02:15 +00:00
|
|
|
func (msg *Message) HasCaption() bool {
|
|
|
|
return !msg.IsText() && msg.Caption != ""
|
|
|
|
}
|
2018-01-29 08:02:35 +00:00
|
|
|
|
2018-04-19 13:02:15 +00:00
|
|
|
// HasAuthorSignature checks that the current channel post has author signature.
|
2018-01-29 08:02:35 +00:00
|
|
|
func (msg *Message) HasAuthorSignature() bool {
|
2018-04-19 10:56:25 +00:00
|
|
|
return msg != nil && msg.AuthorSignature != ""
|
2018-01-29 08:02:35 +00:00
|
|
|
}
|