1
0
telegram/utils_message.go

283 lines
7.5 KiB
Go
Raw Normal View History

2017-10-17 11:08:09 +00:00
package telegram
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() {
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 ""
}
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
func (msg *Message) RawCommand() string {
2018-04-19 10:56:25 +00:00
if !msg.IsCommand() {
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.
func (msg *Message) HasCommandArgument() bool {
2018-04-19 10:56:25 +00:00
if !msg.IsCommand() {
return false
}
entity := msg.Entities[0]
if !entity.IsBotCommand() {
2017-10-17 11:08:09 +00:00
return false
}
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 {
if !msg.HasCommandArgument() {
2017-12-22 13:57:11 +00:00
return ""
2017-10-17 11:08:09 +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.
func (msg *Message) IsReply() bool {
2018-04-19 10:56:25 +00:00
return msg != nil && msg.ReplyToMessage != nil
}
2018-04-19 13:02:15 +00:00
// IsForward checks that the current message is a forward of other message.
func (msg *Message) IsForward() bool {
2018-04-19 10:56:25 +00:00
return msg != nil && msg.ForwardFrom != nil
}
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 {
if msg == nil {
return time.Time{}
}
2017-10-17 11:08:09 +00:00
return time.Unix(msg.Date, 0)
}
2018-04-19 13:02:15 +00:00
// ForwardTime parse current message ForwardDate and returns time.Time.
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.
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
}
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.
func (msg *Message) IsText() bool {
2018-04-19 10:56:25 +00:00
return msg != nil && msg.Text != ""
}
2018-04-19 13:02:15 +00:00
// IsAudio checks that the current message is a audio.
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.
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.
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.
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.
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.
func (msg *Message) IsVideo() bool {
return !msg.IsText() && msg.Video != nil
}
2018-04-19 13:02:15 +00:00
// IsVoice checks that the current message is a voice.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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-04-19 13:02:15 +00:00
// HasAuthorSignature checks that the current channel post has author signature.
func (msg *Message) HasAuthorSignature() bool {
2018-04-19 10:56:25 +00:00
return msg != nil && msg.AuthorSignature != ""
}