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
}
resp := new(Response)
resp, err = b.request(src, MethodEditMessageMedia)
resp, err := b.request(src, MethodEditMessageMedia)
if err != nil {
return nil, err
}

View File

@ -440,7 +440,7 @@ type (
// Unique identifier for this file
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"`
// 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 (
"fmt"
"net/url"
"path"
"strings"
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.
func New(accessToken string) (*Bot, error) {
var err error
bot := new(Bot)
bot.SetClient(defaultClient)
bot.AccessToken = accessToken
b := new(Bot)
b.SetClient(defaultClient)
b.AccessToken = accessToken
bot.User, err = b.GetMe()
return bot, err
b.User, err = b.GetMe()
return b, err
}
// 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
// current bot.
func (b *Bot) IsForwardMentionsMe(m *Message) bool {
return m.IsForward() &&
b.IsMessageMentionsMe(m)
return m.IsForward() && b.IsMessageMentionsMe(m)
}
// IsReplyMentionsMe checks that the input message mentions the current bot.
func (b *Bot) IsReplyMentionsMe(m *Message) bool {
return m.IsReply() &&
b.IsMessageMentionsMe(m.ReplyToMessage)
return m.IsReply() && b.IsMessageMentionsMe(m.ReplyToMessage)
}
// 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.
func (b *Bot) NewFileURL(filePath string) *url.URL {
if b == nil ||
b.AccessToken == "" ||
filePath == "" {
strings.EqualFold(b.AccessToken, "") ||
strings.EqualFold(filePath, "") {
return nil
}
result := defaultURI
result.Path = fmt.Sprint("/file/bot", b.AccessToken, "/", filePath)
result.Path = path.Join("file", fmt.Sprint("bot", b.AccessToken), filePath)
return result
}

View File

@ -1,25 +1,28 @@
package telegram
import "fmt"
import (
"fmt"
"strings"
)
// IsPrivate checks that the current chat is a private chat with single user.
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.
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.
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.
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.
@ -29,7 +32,7 @@ func (c *Chat) HasPinnedMessage() bool {
// HasStickerSet checks that the current chat has a sticker set.
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.
@ -52,9 +55,27 @@ func (c *Chat) FullName() string {
return ""
}
if c.LastName != "" {
if c.HasLastName() {
return fmt.Sprintln(c.FirstName, c.LastName)
}
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 (
"fmt"
"net/url"
"strings"
)
// ParseURL selects URL from entered text of message and parse it as 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
}
@ -19,7 +22,7 @@ func (e *MessageEntity) ParseURL(messageText string) *url.URL {
}
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))
}
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.

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
import "time"
import (
"strings"
"time"
)
// IsCreator checks that current member is creator.
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.
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.
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.
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.
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.
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.
func (m *ChatMember) UntilTime() time.Time {
func (m *ChatMember) UntilTime() *time.Time {
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
import (
"sort"
"strings"
"time"
)
@ -12,14 +13,12 @@ func (m *Message) IsCommand() bool {
}
entity := m.Entities[0]
return entity.IsBotCommand() &&
entity.Offset == 0
return entity.IsBotCommand() && entity.Offset == 0
}
// IsCommandEqual checks that the current message is a specific bot command.
func (m *Message) IsCommandEqual(command string) bool {
return m.IsCommand() &&
strings.EqualFold(m.Command(), command)
return m.IsCommand() && strings.EqualFold(m.Command(), command)
}
// 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.
func (m *Message) IsReply() bool {
return m != nil &&
m.ReplyToMessage != nil
return m != nil && m.ReplyToMessage != nil
}
// IsForward checks that the current message is a forward of other message.
func (m *Message) IsForward() bool {
return m != nil &&
m.ForwardFrom != nil
return m != nil && m.ForwardFrom != nil
}
// Time parse current message Date and returns time.Time.
func (m *Message) Time() time.Time {
func (m *Message) Time() *time.Time {
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.
func (m *Message) ForwardTime() time.Time {
func (m *Message) ForwardTime() *time.Time {
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.
@ -107,168 +106,142 @@ func (m *Message) EditTime() time.Time {
// HasBeenEdited checks that the current message has been edited.
func (m *Message) HasBeenEdited() bool {
return m != nil &&
m.EditDate > 0
return m != nil && m.EditDate > 0
}
// IsText checks that the current message is just a text message.
func (m *Message) IsText() bool {
return m != nil &&
m.Text != ""
return m != nil && !strings.EqualFold(m.Text, "")
}
// IsAudio checks that the current message is a audio.
func (m *Message) IsAudio() bool {
return !m.IsText() &&
m.Audio != nil
return !m.IsText() && m.Audio != nil
}
// IsDocument checks that the current message is a document.
func (m *Message) IsDocument() bool {
return !m.IsText() &&
m.Document != nil
return !m.IsText() && m.Document != nil
}
// IsGame checks that the current message is a game.
func (m *Message) IsGame() bool {
return !m.IsText() &&
m.Game != nil
return !m.IsText() && m.Game != nil
}
// IsPhoto checks that the current message is a photo.
func (m *Message) IsPhoto() bool {
return !m.IsText() &&
len(m.Photo) > 0
return !m.IsText() && len(m.Photo) > 0
}
// IsSticker checks that the current message is a sticker.
func (m *Message) IsSticker() bool {
return !m.IsText() &&
m.Sticker != nil
return !m.IsText() && m.Sticker != nil
}
// IsVideo checks that the current message is a video.
func (m *Message) IsVideo() bool {
return !m.IsText() &&
m.Video != nil
return !m.IsText() && m.Video != nil
}
// IsVoice checks that the current message is a voice.
func (m *Message) IsVoice() bool {
return !m.IsText() &&
m.Voice != nil
return !m.IsText() && m.Voice != nil
}
// IsVideoNote checks that the current message is a video note.
func (m *Message) IsVideoNote() bool {
return !m.IsText() &&
m.VideoNote != nil
return !m.IsText() && m.VideoNote != nil
}
// IsContact checks that the current message is a contact.
func (m *Message) IsContact() bool {
return !m.IsText() &&
m.Contact != nil
return !m.IsText() && m.Contact != nil
}
// IsLocation checks that the current message is a location.
func (m *Message) IsLocation() bool {
return !m.IsText() &&
m.Location != nil
return !m.IsText() && m.Location != nil
}
// IsVenue checks that the current message is a venue.
func (m *Message) IsVenue() bool {
return !m.IsText() &&
m.Venue != nil
return !m.IsText() && m.Venue != nil
}
// IsNewChatMembersEvent checks that the current message is a event of entry of
// new members.
func (m *Message) IsNewChatMembersEvent() bool {
return !m.IsText() &&
len(m.NewChatMembers) > 0
return !m.IsText() && len(m.NewChatMembers) > 0
}
// IsLeftChatMemberEvent checks that the current message is a event of members
// exit.
func (m *Message) IsLeftChatMemberEvent() bool {
return !m.IsText() &&
m.LeftChatMember != nil
return !m.IsText() && m.LeftChatMember != nil
}
// IsNewChatTitleEvent checks that the current message is a event of setting a
// new chat title.
func (m *Message) IsNewChatTitleEvent() bool {
return !m.IsText() &&
m.NewChatTitle != ""
return !m.IsText() && !strings.EqualFold(m.NewChatTitle, "")
}
// IsNewChatPhotoEvent checks that the current message is a event of setting a
// new chat avatar.
func (m *Message) IsNewChatPhotoEvent() bool {
return !m.IsText() &&
len(m.NewChatPhoto) > 0
return !m.IsText() && len(m.NewChatPhoto) > 0
}
// IsDeleteChatPhotoEvent checks that the current message is a event of deleting
// a chat avatar.
func (m *Message) IsDeleteChatPhotoEvent() bool {
return !m.IsText() &&
m.DeleteChatPhoto
return !m.IsText() && m.DeleteChatPhoto
}
// IsGroupChatCreatedEvent checks that the current message is a event of creating
// a new group.
func (m *Message) IsGroupChatCreatedEvent() bool {
return !m.IsText() &&
m.GroupChatCreated
return !m.IsText() && m.GroupChatCreated
}
// IsSupergroupChatCreatedEvent checks that the current message is a event of
// creating a new supergroup.
func (m *Message) IsSupergroupChatCreatedEvent() bool {
return !m.IsText() &&
m.SupergroupChatCreated
return !m.IsText() && m.SupergroupChatCreated
}
// IsChannelChatCreatedEvent checks that the current message is a event of
// creating a new channel.
func (m *Message) IsChannelChatCreatedEvent() bool {
return !m.IsText() &&
m.ChannelChatCreated
return !m.IsText() && m.ChannelChatCreated
}
// IsPinnedMessage checks that the current message is a event of pinning another
// message.
func (m *Message) IsPinnedMessage() bool {
return !m.IsText() &&
m.PinnedMessage != nil
return !m.IsText() && m.PinnedMessage != nil
}
// IsInvoice checks that the current message is a invoice.
func (m *Message) IsInvoice() bool {
return !m.IsText() &&
m.Invoice != nil
return !m.IsText() && m.Invoice != nil
}
// IsSuccessfulPayment checks that the current message is a event of successful
// payment.
func (m *Message) IsSuccessfulPayment() bool {
return !m.IsText() &&
m.SuccessfulPayment != nil
return !m.IsText() && m.SuccessfulPayment != nil
}
// HasEntities checks that the current message contains entities.
func (m *Message) HasEntities() bool {
return m.IsText() &&
len(m.Entities) > 0
return m.IsText() && len(m.Entities) > 0
}
// HasCaptionEntities checks that the current media contains entities in caption.
func (m *Message) HasCaptionEntities() bool {
return !m.IsText() &&
len(m.CaptionEntities) > 0
return !m.IsText() && len(m.CaptionEntities) > 0
}
// 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.
func (m *Message) HasCaption() bool {
return !m.IsText() &&
m.Caption != ""
return !m.IsText() && !strings.EqualFold(m.Caption, "")
}
// HasAuthorSignature checks that the current channel post has author signature.
func (m *Message) HasAuthorSignature() bool {
return m != nil &&
m.AuthorSignature != ""
return m != nil && !strings.EqualFold(m.AuthorSignature, "")
}
// IsEvent checks what current message is a any chat event.
@ -324,3 +295,72 @@ func (m *Message) IsEvent() bool {
m.IsSupergroupChatCreatedEvent() ||
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
import "strings"
// InSet checks that the current sticker in the stickers set.
//
// For uploaded WebP files this return false.
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.
@ -22,3 +24,11 @@ func (s *Sticker) Set(bot *Bot) *StickerSet {
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 (
"fmt"
"strings"
"golang.org/x/text/language"
)
@ -27,9 +28,19 @@ func (u *User) FullName() string {
return ""
}
if u.LastName != "" {
return fmt.Sprintln(u.FirstName, u.LastName)
if u.HasLastName() {
return fmt.Sprint(u.FirstName, " ", u.LastName)
}
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
}