1
0

Added new various utils

This commit is contained in:
Maxim Lebedev 2018-08-15 18:26:07 +05:00
parent e777b439ec
commit b48f9bab2f
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
16 changed files with 293 additions and 118 deletions

View File

@ -32,8 +32,7 @@ func (b *Bot) EditMessageMedia(emmp *EditMessageMediaParameters) (m *Message, er
return nil, err return nil, err
} }
resp := new(Response) resp, err := b.request(src, MethodEditMessageMedia)
resp, err = b.request(src, MethodEditMessageMedia)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -440,7 +440,7 @@ type (
// Unique identifier for this file // Unique identifier for this file
FileID string `json:"file_id"` FileID string `json:"file_id"`
// Video width and height as defined by sender // Video width and height (diameter of the video message) as defined by sender
Length int `json:"length"` Length int `json:"length"`
// Duration of the video in seconds as defined by sender // Duration of the video in seconds as defined by sender

30
utils_audio.go Normal file
View File

@ -0,0 +1,30 @@
package telegram
import (
"strings"
)
func (a *Audio) FullName(sep string) (name string) {
if !a.HasTitle() {
return
}
if a.HasPerformer() {
name += a.Performer + sep
}
name += a.Title
return
}
func (a *Audio) HasPerformer() bool {
return a != nil && !strings.EqualFold(a.Performer, "")
}
func (a *Audio) HasTitle() bool {
return a != nil && !strings.EqualFold(a.Title, "")
}
func (a *Audio) HasThumb() bool {
return a != nil && a.Thumb != nil
}

View File

@ -3,6 +3,7 @@ package telegram
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"path"
"strings" "strings"
http "github.com/valyala/fasthttp" http "github.com/valyala/fasthttp"
@ -24,12 +25,12 @@ func (b *Bot) SetClient(newClient *http.Client) {
// New creates a new default Bot structure based on the input access token. // New creates a new default Bot structure based on the input access token.
func New(accessToken string) (*Bot, error) { func New(accessToken string) (*Bot, error) {
var err error var err error
bot := new(Bot) b := new(Bot)
bot.SetClient(defaultClient) b.SetClient(defaultClient)
bot.AccessToken = accessToken b.AccessToken = accessToken
bot.User, err = b.GetMe() b.User, err = b.GetMe()
return bot, err return b, err
} }
// IsMessageFromMe checks that the input message is a message from the current // IsMessageFromMe checks that the input message is a message from the current
@ -109,14 +110,12 @@ func (b *Bot) IsMessageMentionsMe(m *Message) bool {
// IsForwardMentionsMe checks that the input forwarded message mentions the // IsForwardMentionsMe checks that the input forwarded message mentions the
// current bot. // current bot.
func (b *Bot) IsForwardMentionsMe(m *Message) bool { func (b *Bot) IsForwardMentionsMe(m *Message) bool {
return m.IsForward() && return m.IsForward() && b.IsMessageMentionsMe(m)
b.IsMessageMentionsMe(m)
} }
// IsReplyMentionsMe checks that the input message mentions the current bot. // IsReplyMentionsMe checks that the input message mentions the current bot.
func (b *Bot) IsReplyMentionsMe(m *Message) bool { func (b *Bot) IsReplyMentionsMe(m *Message) bool {
return m.IsReply() && return m.IsReply() && b.IsMessageMentionsMe(m.ReplyToMessage)
b.IsMessageMentionsMe(m.ReplyToMessage)
} }
// IsMessageToMe checks that the input message is addressed to the current bot. // IsMessageToMe checks that the input message is addressed to the current bot.
@ -138,13 +137,13 @@ func (b *Bot) IsMessageToMe(m *Message) bool {
// NewFileURL creates a url.URL to file with path getted from GetFile method. // NewFileURL creates a url.URL to file with path getted from GetFile method.
func (b *Bot) NewFileURL(filePath string) *url.URL { func (b *Bot) NewFileURL(filePath string) *url.URL {
if b == nil || if b == nil ||
b.AccessToken == "" || strings.EqualFold(b.AccessToken, "") ||
filePath == "" { strings.EqualFold(filePath, "") {
return nil return nil
} }
result := defaultURI result := defaultURI
result.Path = fmt.Sprint("/file/bot", b.AccessToken, "/", filePath) result.Path = path.Join("file", fmt.Sprint("bot", b.AccessToken), filePath)
return result return result
} }

View File

@ -1,25 +1,28 @@
package telegram package telegram
import "fmt" import (
"fmt"
"strings"
)
// IsPrivate checks that the current chat is a private chat with single user. // IsPrivate checks that the current chat is a private chat with single user.
func (c *Chat) IsPrivate() bool { func (c *Chat) IsPrivate() bool {
return c != nil && c.Type == ChatPrivate return c != nil && strings.EqualFold(c.Type, ChatPrivate)
} }
// IsGroup checks that the current chat is a group. // IsGroup checks that the current chat is a group.
func (c *Chat) IsGroup() bool { func (c *Chat) IsGroup() bool {
return c != nil && c.Type == ChatGroup return c != nil && strings.EqualFold(c.Type, ChatGroup)
} }
// IsSuperGroup checks that the current chat is a supergroup. // IsSuperGroup checks that the current chat is a supergroup.
func (c *Chat) IsSuperGroup() bool { func (c *Chat) IsSuperGroup() bool {
return c != nil && c.Type == ChatSuperGroup return c != nil && strings.EqualFold(c.Type, ChatSuperGroup)
} }
// IsChannel checks that the current chat is a channel. // IsChannel checks that the current chat is a channel.
func (c *Chat) IsChannel() bool { func (c *Chat) IsChannel() bool {
return c != nil && c.Type == ChatChannel return c != nil && strings.EqualFold(c.Type, ChatChannel)
} }
// HasPinnedMessage checks that the current chat has a pinned message. // HasPinnedMessage checks that the current chat has a pinned message.
@ -29,7 +32,7 @@ func (c *Chat) HasPinnedMessage() bool {
// HasStickerSet checks that the current chat has a sticker set. // HasStickerSet checks that the current chat has a sticker set.
func (c *Chat) HasStickerSet() bool { func (c *Chat) HasStickerSet() bool {
return c != nil && c.StickerSetName != "" return c != nil && !strings.EqualFold(c.StickerSetName, "")
} }
// StickerSet return StickerSet structure if StickerSetName is available. // StickerSet return StickerSet structure if StickerSetName is available.
@ -52,9 +55,27 @@ func (c *Chat) FullName() string {
return "" return ""
} }
if c.LastName != "" { if c.HasLastName() {
return fmt.Sprintln(c.FirstName, c.LastName) return fmt.Sprintln(c.FirstName, c.LastName)
} }
return c.FirstName return c.FirstName
} }
// HaveLastName checks what the current user has a LastName.
func (c *Chat) HasLastName() bool {
return c != nil && !strings.EqualFold(c.LastName, "")
}
// HaveUsername checks what the current user has a username.
func (c *Chat) HasUsername() bool {
return c != nil && !strings.EqualFold(c.Username, "")
}
func (c *Chat) HasDescription() bool {
return c != nil && !strings.EqualFold(c.Description, "")
}
func (c *Chat) HasInviteLink() bool {
return c != nil && !strings.EqualFold(c.InviteLink, "")
}

View File

@ -0,0 +1,5 @@
package telegram
func (cir *ChosenInlineResult) HasLocation() bool {
return cir != nil && cir.Location != nil
}

33
utils_contact.go Normal file
View File

@ -0,0 +1,33 @@
package telegram
import (
"fmt"
"strings"
)
// FullName returns the full name of contact or FirstName if LastName is not
// available.
func (c *Contact) FullName() string {
if c == nil {
return ""
}
if c.HasLastName() {
return fmt.Sprint(c.FirstName, " ", c.LastName)
}
return c.FirstName
}
// HaveLastName checks what the current contact has a LastName.
func (c *Contact) HasLastName() bool {
return c != nil && !strings.EqualFold(c.LastName, "")
}
func (c *Contact) HasTelegram() bool {
return c != nil && c.UserID != 0
}
func (c *Contact) HasVCard() bool {
return c != nil && !strings.EqualFold(c.VCard, "")
}

5
utils_document.go Normal file
View File

@ -0,0 +1,5 @@
package telegram
func (d *Document) HasThumb() bool {
return d != nil && d.Thumb != nil
}

View File

@ -3,11 +3,14 @@ package telegram
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"strings"
) )
// ParseURL selects URL from entered text of message and parse it as url.URL. // ParseURL selects URL from entered text of message and parse it as url.URL.
func (e *MessageEntity) ParseURL(messageText string) *url.URL { func (e *MessageEntity) ParseURL(messageText string) *url.URL {
if e == nil || !e.IsURL() || messageText == "" { if e == nil ||
!e.IsURL() ||
strings.EqualFold(messageText, "") {
return nil return nil
} }
@ -19,7 +22,7 @@ func (e *MessageEntity) ParseURL(messageText string) *url.URL {
} }
link, err := url.Parse(string(text[from:to])) link, err := url.Parse(string(text[from:to]))
if err == nil && link.Scheme == "" { if err == nil && strings.EqualFold(link.Scheme, "") {
link, err = url.Parse(fmt.Sprint("http://", link)) link, err = url.Parse(fmt.Sprint("http://", link))
} }
if err != nil { if err != nil {
@ -31,57 +34,57 @@ func (e *MessageEntity) ParseURL(messageText string) *url.URL {
// IsBold checks that the current entity is a bold tag. // IsBold checks that the current entity is a bold tag.
func (e *MessageEntity) IsBold() bool { func (e *MessageEntity) IsBold() bool {
return e != nil && e.Type == EntityBold return e != nil && strings.EqualFold(e.Type, EntityBold)
} }
// IsBotCommand checks that the current entity is a bot command. // IsBotCommand checks that the current entity is a bot command.
func (e *MessageEntity) IsBotCommand() bool { func (e *MessageEntity) IsBotCommand() bool {
return e != nil && e.Type == EntityBotCommand return e != nil && strings.EqualFold(e.Type, EntityBotCommand)
} }
// IsCode checks that the current entity is a code tag. // IsCode checks that the current entity is a code tag.
func (e *MessageEntity) IsCode() bool { func (e *MessageEntity) IsCode() bool {
return e != nil && e.Type == EntityCode return e != nil && strings.EqualFold(e.Type, EntityCode)
} }
// IsEmail checks that the current entity is a email. // IsEmail checks that the current entity is a email.
func (e *MessageEntity) IsEmail() bool { func (e *MessageEntity) IsEmail() bool {
return e != nil && e.Type == EntityEmail return e != nil && strings.EqualFold(e.Type, EntityEmail)
} }
// IsHashtag checks that the current entity is a hashtag. // IsHashtag checks that the current entity is a hashtag.
func (e *MessageEntity) IsHashtag() bool { func (e *MessageEntity) IsHashtag() bool {
return e != nil && e.Type == EntityHashtag return e != nil && strings.EqualFold(e.Type, EntityHashtag)
} }
// IsItalic checks that the current entity is a italic tag. // IsItalic checks that the current entity is a italic tag.
func (e *MessageEntity) IsItalic() bool { func (e *MessageEntity) IsItalic() bool {
return e != nil && e.Type == EntityItalic return e != nil && strings.EqualFold(e.Type, EntityItalic)
} }
// IsMention checks that the current entity is a username mention. // IsMention checks that the current entity is a username mention.
func (e *MessageEntity) IsMention() bool { func (e *MessageEntity) IsMention() bool {
return e != nil && e.Type == EntityMention return e != nil && strings.EqualFold(e.Type, EntityMention)
} }
// IsPre checks that the current entity is a pre tag. // IsPre checks that the current entity is a pre tag.
func (e *MessageEntity) IsPre() bool { func (e *MessageEntity) IsPre() bool {
return e != nil && e.Type == EntityPre return e != nil && strings.EqualFold(e.Type, EntityPre)
} }
// IsTextLink checks that the current entity is a text link. // IsTextLink checks that the current entity is a text link.
func (e *MessageEntity) IsTextLink() bool { func (e *MessageEntity) IsTextLink() bool {
return e != nil && e.Type == EntityTextLink return e != nil && strings.EqualFold(e.Type, EntityTextLink)
} }
// IsTextMention checks that the current entity is a mention without username. // IsTextMention checks that the current entity is a mention without username.
func (e *MessageEntity) IsTextMention() bool { func (e *MessageEntity) IsTextMention() bool {
return e != nil && e.Type == EntityTextMention return e != nil && strings.EqualFold(e.Type, EntityTextMention)
} }
// IsURL checks that the current entity is a URL // IsURL checks that the current entity is a URL
func (e *MessageEntity) IsURL() bool { func (e *MessageEntity) IsURL() bool {
return e != nil && e.Type == EntityURL return e != nil && strings.EqualFold(e.Type, EntityURL)
} }
// TextLink parse current text link entity as url.URL. // TextLink parse current text link entity as url.URL.

5
utils_inline_query.go Normal file
View File

@ -0,0 +1,5 @@
package telegram
func (iq *InlineQuery) HasLocation() bool {
return iq != nil && iq.Location != nil
}

View File

@ -1,42 +1,46 @@
package telegram package telegram
import "time" import (
"strings"
"time"
)
// IsCreator checks that current member is creator. // IsCreator checks that current member is creator.
func (m *ChatMember) IsCreator() bool { func (m *ChatMember) IsCreator() bool {
return m != nil && m.Status == StatusCreator return m != nil && strings.EqualFold(m.Status, StatusCreator)
} }
// IsAdministrator checks that current member is administrator. // IsAdministrator checks that current member is administrator.
func (m *ChatMember) IsAdministrator() bool { func (m *ChatMember) IsAdministrator() bool {
return m != nil && m.Status == StatusAdministrator return m != nil && strings.EqualFold(m.Status, StatusAdministrator)
} }
// IsMember checks that current member is a m. // IsMember checks that current member is a m.
func (m *ChatMember) IsMember() bool { func (m *ChatMember) IsMember() bool {
return m != nil && m.Status == StatusMember return m != nil && strings.EqualFold(m.Status, StatusMember)
} }
// IsRestricted checks that current member has been restricted. // IsRestricted checks that current member has been restricted.
func (m *ChatMember) IsRestricted() bool { func (m *ChatMember) IsRestricted() bool {
return m != nil && m.Status == StatusRestricted return m != nil && strings.EqualFold(m.Status, StatusRestricted)
} }
// IsLeft checks that current member has left the chat. // IsLeft checks that current member has left the chat.
func (m *ChatMember) IsLeft() bool { func (m *ChatMember) IsLeft() bool {
return m != nil && m.Status == StatusLeft return m != nil && strings.EqualFold(m.Status, StatusLeft)
} }
// IsKicked checks that current member has been kicked. // IsKicked checks that current member has been kicked.
func (m *ChatMember) IsKicked() bool { func (m *ChatMember) IsKicked() bool {
return m != nil && m.Status == StatusKicked return m != nil && strings.EqualFold(m.Status, StatusKicked)
} }
// UntilTime parse UntilDate of restrictions and returns time.Time. // UntilTime parse UntilDate of restrictions and returns time.Time.
func (m *ChatMember) UntilTime() time.Time { func (m *ChatMember) UntilTime() *time.Time {
if m == nil { if m == nil {
return time.Time{} return nil
} }
return time.Unix(m.UntilDate, 0) ut := time.Unix(m.UntilDate, 0)
return &ut
} }

View File

@ -1,6 +1,7 @@
package telegram package telegram
import ( import (
"sort"
"strings" "strings"
"time" "time"
) )
@ -12,14 +13,12 @@ func (m *Message) IsCommand() bool {
} }
entity := m.Entities[0] entity := m.Entities[0]
return entity.IsBotCommand() && return entity.IsBotCommand() && entity.Offset == 0
entity.Offset == 0
} }
// IsCommandEqual checks that the current message is a specific bot command. // IsCommandEqual checks that the current message is a specific bot command.
func (m *Message) IsCommandEqual(command string) bool { func (m *Message) IsCommandEqual(command string) bool {
return m.IsCommand() && return m.IsCommand() && strings.EqualFold(m.Command(), command)
strings.EqualFold(m.Command(), command)
} }
// Command returns identifier of the bot command without bot username, if it was // Command returns identifier of the bot command without bot username, if it was
@ -67,32 +66,32 @@ func (m *Message) CommandArgument() string {
// IsReply checks that the current message is a reply on other message. // IsReply checks that the current message is a reply on other message.
func (m *Message) IsReply() bool { func (m *Message) IsReply() bool {
return m != nil && return m != nil && m.ReplyToMessage != nil
m.ReplyToMessage != nil
} }
// IsForward checks that the current message is a forward of other message. // IsForward checks that the current message is a forward of other message.
func (m *Message) IsForward() bool { func (m *Message) IsForward() bool {
return m != nil && return m != nil && m.ForwardFrom != nil
m.ForwardFrom != nil
} }
// Time parse current message Date and returns time.Time. // Time parse current message Date and returns time.Time.
func (m *Message) Time() time.Time { func (m *Message) Time() *time.Time {
if m == nil { if m == nil {
return time.Time{} return nil
} }
return time.Unix(m.Date, 0) t := time.Unix(m.Date, 0)
return &t
} }
// ForwardTime parse current message ForwardDate and returns time.Time. // ForwardTime parse current message ForwardDate and returns time.Time.
func (m *Message) ForwardTime() time.Time { func (m *Message) ForwardTime() *time.Time {
if m == nil { if m == nil {
return time.Time{} return nil
} }
return time.Unix(m.ForwardDate, 0) ft := time.Unix(m.ForwardDate, 0)
return &ft
} }
// EditTime parse current message EditDate and returns time.Time. // EditTime parse current message EditDate and returns time.Time.
@ -107,168 +106,142 @@ func (m *Message) EditTime() time.Time {
// HasBeenEdited checks that the current message has been edited. // HasBeenEdited checks that the current message has been edited.
func (m *Message) HasBeenEdited() bool { func (m *Message) HasBeenEdited() bool {
return m != nil && return m != nil && m.EditDate > 0
m.EditDate > 0
} }
// IsText checks that the current message is just a text message. // IsText checks that the current message is just a text message.
func (m *Message) IsText() bool { func (m *Message) IsText() bool {
return m != nil && return m != nil && !strings.EqualFold(m.Text, "")
m.Text != ""
} }
// IsAudio checks that the current message is a audio. // IsAudio checks that the current message is a audio.
func (m *Message) IsAudio() bool { func (m *Message) IsAudio() bool {
return !m.IsText() && return !m.IsText() && m.Audio != nil
m.Audio != nil
} }
// IsDocument checks that the current message is a document. // IsDocument checks that the current message is a document.
func (m *Message) IsDocument() bool { func (m *Message) IsDocument() bool {
return !m.IsText() && return !m.IsText() && m.Document != nil
m.Document != nil
} }
// IsGame checks that the current message is a game. // IsGame checks that the current message is a game.
func (m *Message) IsGame() bool { func (m *Message) IsGame() bool {
return !m.IsText() && return !m.IsText() && m.Game != nil
m.Game != nil
} }
// IsPhoto checks that the current message is a photo. // IsPhoto checks that the current message is a photo.
func (m *Message) IsPhoto() bool { func (m *Message) IsPhoto() bool {
return !m.IsText() && return !m.IsText() && len(m.Photo) > 0
len(m.Photo) > 0
} }
// IsSticker checks that the current message is a sticker. // IsSticker checks that the current message is a sticker.
func (m *Message) IsSticker() bool { func (m *Message) IsSticker() bool {
return !m.IsText() && return !m.IsText() && m.Sticker != nil
m.Sticker != nil
} }
// IsVideo checks that the current message is a video. // IsVideo checks that the current message is a video.
func (m *Message) IsVideo() bool { func (m *Message) IsVideo() bool {
return !m.IsText() && return !m.IsText() && m.Video != nil
m.Video != nil
} }
// IsVoice checks that the current message is a voice. // IsVoice checks that the current message is a voice.
func (m *Message) IsVoice() bool { func (m *Message) IsVoice() bool {
return !m.IsText() && return !m.IsText() && m.Voice != nil
m.Voice != nil
} }
// IsVideoNote checks that the current message is a video note. // IsVideoNote checks that the current message is a video note.
func (m *Message) IsVideoNote() bool { func (m *Message) IsVideoNote() bool {
return !m.IsText() && return !m.IsText() && m.VideoNote != nil
m.VideoNote != nil
} }
// IsContact checks that the current message is a contact. // IsContact checks that the current message is a contact.
func (m *Message) IsContact() bool { func (m *Message) IsContact() bool {
return !m.IsText() && return !m.IsText() && m.Contact != nil
m.Contact != nil
} }
// IsLocation checks that the current message is a location. // IsLocation checks that the current message is a location.
func (m *Message) IsLocation() bool { func (m *Message) IsLocation() bool {
return !m.IsText() && return !m.IsText() && m.Location != nil
m.Location != nil
} }
// IsVenue checks that the current message is a venue. // IsVenue checks that the current message is a venue.
func (m *Message) IsVenue() bool { func (m *Message) IsVenue() bool {
return !m.IsText() && return !m.IsText() && m.Venue != nil
m.Venue != nil
} }
// IsNewChatMembersEvent checks that the current message is a event of entry of // IsNewChatMembersEvent checks that the current message is a event of entry of
// new members. // new members.
func (m *Message) IsNewChatMembersEvent() bool { func (m *Message) IsNewChatMembersEvent() bool {
return !m.IsText() && return !m.IsText() && len(m.NewChatMembers) > 0
len(m.NewChatMembers) > 0
} }
// IsLeftChatMemberEvent checks that the current message is a event of members // IsLeftChatMemberEvent checks that the current message is a event of members
// exit. // exit.
func (m *Message) IsLeftChatMemberEvent() bool { func (m *Message) IsLeftChatMemberEvent() bool {
return !m.IsText() && return !m.IsText() && m.LeftChatMember != nil
m.LeftChatMember != nil
} }
// IsNewChatTitleEvent checks that the current message is a event of setting a // IsNewChatTitleEvent checks that the current message is a event of setting a
// new chat title. // new chat title.
func (m *Message) IsNewChatTitleEvent() bool { func (m *Message) IsNewChatTitleEvent() bool {
return !m.IsText() && return !m.IsText() && !strings.EqualFold(m.NewChatTitle, "")
m.NewChatTitle != ""
} }
// IsNewChatPhotoEvent checks that the current message is a event of setting a // IsNewChatPhotoEvent checks that the current message is a event of setting a
// new chat avatar. // new chat avatar.
func (m *Message) IsNewChatPhotoEvent() bool { func (m *Message) IsNewChatPhotoEvent() bool {
return !m.IsText() && return !m.IsText() && len(m.NewChatPhoto) > 0
len(m.NewChatPhoto) > 0
} }
// IsDeleteChatPhotoEvent checks that the current message is a event of deleting // IsDeleteChatPhotoEvent checks that the current message is a event of deleting
// a chat avatar. // a chat avatar.
func (m *Message) IsDeleteChatPhotoEvent() bool { func (m *Message) IsDeleteChatPhotoEvent() bool {
return !m.IsText() && return !m.IsText() && m.DeleteChatPhoto
m.DeleteChatPhoto
} }
// IsGroupChatCreatedEvent checks that the current message is a event of creating // IsGroupChatCreatedEvent checks that the current message is a event of creating
// a new group. // a new group.
func (m *Message) IsGroupChatCreatedEvent() bool { func (m *Message) IsGroupChatCreatedEvent() bool {
return !m.IsText() && return !m.IsText() && m.GroupChatCreated
m.GroupChatCreated
} }
// IsSupergroupChatCreatedEvent checks that the current message is a event of // IsSupergroupChatCreatedEvent checks that the current message is a event of
// creating a new supergroup. // creating a new supergroup.
func (m *Message) IsSupergroupChatCreatedEvent() bool { func (m *Message) IsSupergroupChatCreatedEvent() bool {
return !m.IsText() && return !m.IsText() && m.SupergroupChatCreated
m.SupergroupChatCreated
} }
// IsChannelChatCreatedEvent checks that the current message is a event of // IsChannelChatCreatedEvent checks that the current message is a event of
// creating a new channel. // creating a new channel.
func (m *Message) IsChannelChatCreatedEvent() bool { func (m *Message) IsChannelChatCreatedEvent() bool {
return !m.IsText() && return !m.IsText() && m.ChannelChatCreated
m.ChannelChatCreated
} }
// IsPinnedMessage checks that the current message is a event of pinning another // IsPinnedMessage checks that the current message is a event of pinning another
// message. // message.
func (m *Message) IsPinnedMessage() bool { func (m *Message) IsPinnedMessage() bool {
return !m.IsText() && return !m.IsText() && m.PinnedMessage != nil
m.PinnedMessage != nil
} }
// IsInvoice checks that the current message is a invoice. // IsInvoice checks that the current message is a invoice.
func (m *Message) IsInvoice() bool { func (m *Message) IsInvoice() bool {
return !m.IsText() && return !m.IsText() && m.Invoice != nil
m.Invoice != nil
} }
// IsSuccessfulPayment checks that the current message is a event of successful // IsSuccessfulPayment checks that the current message is a event of successful
// payment. // payment.
func (m *Message) IsSuccessfulPayment() bool { func (m *Message) IsSuccessfulPayment() bool {
return !m.IsText() && return !m.IsText() && m.SuccessfulPayment != nil
m.SuccessfulPayment != nil
} }
// HasEntities checks that the current message contains entities. // HasEntities checks that the current message contains entities.
func (m *Message) HasEntities() bool { func (m *Message) HasEntities() bool {
return m.IsText() && return m.IsText() && len(m.Entities) > 0
len(m.Entities) > 0
} }
// HasCaptionEntities checks that the current media contains entities in caption. // HasCaptionEntities checks that the current media contains entities in caption.
func (m *Message) HasCaptionEntities() bool { func (m *Message) HasCaptionEntities() bool {
return !m.IsText() && return !m.IsText() && len(m.CaptionEntities) > 0
len(m.CaptionEntities) > 0
} }
// HasMentions checks that the current message contains mentions. // HasMentions checks that the current message contains mentions.
@ -303,14 +276,12 @@ func (m *Message) HasCaptionMentions() bool {
// HasCaption checks that the current media has caption. // HasCaption checks that the current media has caption.
func (m *Message) HasCaption() bool { func (m *Message) HasCaption() bool {
return !m.IsText() && return !m.IsText() && !strings.EqualFold(m.Caption, "")
m.Caption != ""
} }
// HasAuthorSignature checks that the current channel post has author signature. // HasAuthorSignature checks that the current channel post has author signature.
func (m *Message) HasAuthorSignature() bool { func (m *Message) HasAuthorSignature() bool {
return m != nil && return m != nil && !strings.EqualFold(m.AuthorSignature, "")
m.AuthorSignature != ""
} }
// IsEvent checks what current message is a any chat event. // IsEvent checks what current message is a any chat event.
@ -324,3 +295,72 @@ func (m *Message) IsEvent() bool {
m.IsSupergroupChatCreatedEvent() || m.IsSupergroupChatCreatedEvent() ||
m.IsNewChatPhotoEvent() m.IsNewChatPhotoEvent()
} }
func sortPhotos(ps []PhotoSize, reverse bool) []PhotoSize {
buf := make([]PhotoSize, len(ps))
copy(buf, ps)
sort.Slice(buf, func(i, j int) bool {
if reverse {
return buf[i].Width > buf[j].Width &&
buf[i].Height > buf[j].Height
}
return buf[i].Width < buf[j].Width &&
buf[i].Height < buf[j].Height
})
return buf
}
func (m *Message) BigPhoto() *PhotoSize {
if m == nil || !m.IsPhoto() {
return nil
}
if len(m.Photo) == 1 {
return &m.Photo[0]
}
sp := sortPhotos(m.Photo, true)
return &sp[0]
}
func (m *Message) SmallPhoto() *PhotoSize {
if m == nil || !m.IsPhoto() {
return nil
}
if len(m.Photo) == 1 {
return &m.Photo[0]
}
sp := sortPhotos(m.Photo, false)
return &sp[0]
}
func (m *Message) BigChatPhoto() *PhotoSize {
if m == nil || !m.IsNewChatPhotoEvent() {
return nil
}
if len(m.NewChatPhoto) == 1 {
return &m.NewChatPhoto[0]
}
sp := sortPhotos(m.NewChatPhoto, true)
return &sp[0]
}
func (m *Message) SmallChatPhoto() *PhotoSize {
if m == nil || !m.IsNewChatPhotoEvent() {
return nil
}
if len(m.NewChatPhoto) == 1 {
return &m.NewChatPhoto[0]
}
sp := sortPhotos(m.NewChatPhoto, false)
return &sp[0]
}

View File

@ -1,10 +1,12 @@
package telegram package telegram
import "strings"
// InSet checks that the current sticker in the stickers set. // InSet checks that the current sticker in the stickers set.
// //
// For uploaded WebP files this return false. // For uploaded WebP files this return false.
func (s *Sticker) InSet() bool { func (s *Sticker) InSet() bool {
return s != nil && s.SetName != "" return s != nil && !strings.EqualFold(s.SetName, "")
} }
// Set use bot for getting parent StickerSet if SetName is present. // Set use bot for getting parent StickerSet if SetName is present.
@ -22,3 +24,11 @@ func (s *Sticker) Set(bot *Bot) *StickerSet {
return set return set
} }
func (s *Sticker) HasThumb() bool {
return s != nil && s.Thumb != nil
}
func (s *Sticker) IsMask() bool {
return s != nil && s.MaskPosition != nil
}

View File

@ -2,6 +2,7 @@ package telegram
import ( import (
"fmt" "fmt"
"strings"
"golang.org/x/text/language" "golang.org/x/text/language"
) )
@ -27,9 +28,19 @@ func (u *User) FullName() string {
return "" return ""
} }
if u.LastName != "" { if u.HasLastName() {
return fmt.Sprintln(u.FirstName, u.LastName) return fmt.Sprint(u.FirstName, " ", u.LastName)
} }
return u.FirstName return u.FirstName
} }
// HaveLastName checks what the current user has a LastName.
func (u *User) HasLastName() bool {
return u != nil && !strings.EqualFold(u.LastName, "")
}
// HaveUsername checks what the current user has a username.
func (u *User) HasUsername() bool {
return u != nil && !strings.EqualFold(u.Username, "")
}

5
utils_video.go Normal file
View File

@ -0,0 +1,5 @@
package telegram
func (v *Video) HasThumb() bool {
return v != nil && v.Thumb != nil
}

5
utils_video_note.go Normal file
View File

@ -0,0 +1,5 @@
package telegram
func (vn *VideoNote) HasThumb() bool {
return vn != nil && vn.Thumb != nil
}