From 90a7dec4be3edc3de230489ee3a707380b5e0519 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Thu, 12 Apr 2018 17:56:53 +0500 Subject: [PATCH] :recycle: Added new stuff, maked small fixes --- constants.go | 16 +++++++++++++++- set_webhook.go | 3 ++- utils.go | 13 +++++-------- utils_bot.go | 6 +++--- utils_chat.go | 39 ++++++++++++++++++++++++++++++++++++++ utils_entity.go | 49 ++++++++++++++++++++++++++++-------------------- utils_message.go | 6 +++++- utils_user.go | 2 +- 8 files changed, 99 insertions(+), 35 deletions(-) diff --git a/constants.go b/constants.go index 48e2b11..25108b4 100644 --- a/constants.go +++ b/constants.go @@ -35,10 +35,12 @@ const ( ) const ( + MethodAddStickerToSet = "addStickerToSet" MethodAnswerCallbackQuery = "answerCallbackQuery" MethodAnswerInlineQuery = "answerInlineQuery" MethodAnswerPreCheckoutQuery = "answerPreCheckoutQuery" MethodAnswerShippingQuery = "answerShippingQuery" + MethodCreateNewStickerSet = "createNewStickerSet" MethodDeleteChatPhoto = "deleteChatPhoto" MethodDeleteChatStickerSet = "deleteChatStickerSet" MethodDeleteMessage = "deleteMessage" @@ -64,6 +66,9 @@ const ( MethodKickChatMember = "kickChatMember" MethodLeaveChat = "leaveChat" MethodPinChatMessage = "pinChatMessage" + MethodPromoteChatMember = "promoteChatMember" + MethodRestrictChatMember = "restrictChatMember" + MethodSendAudio = "sendAudio" MethodSendChatAction = "sendChatAction" MethodSendContact = "sendContact" MethodSendDocument = "sendDocument" @@ -73,7 +78,11 @@ const ( MethodSendMediaGroup = "sendMediaGroup" MethodSendMessage = "sendMessage" MethodSendPhoto = "sendPhoto" + MethodSendSticker = "sendSticker" MethodSendVenue = "sendVenue" + MethodSendVideo = "sendVideo" + MethodSendVideoNote = "sendVideoNote" + MethodSendVoice = "sendVoice" MethodSetChatDescription = "setChatDescription" MethodSetChatPhoto = "setChatPhoto" MethodSetChatStickerSet = "setChatStickerSet" @@ -81,8 +90,10 @@ const ( MethodSetGameScore = "setGameScore" MethodSetStickerPositionInSet = "setStickerPositionInSet" MethodSetWebhook = "setWebhook" + MethodStopMessageLiveLocation = "stopMessageLiveLocation" MethodUnbanChatMember = "unbanChatMember" MethodUnpinChatMessage = "unpinChatMessage" + MethodUploadStickerFile = "uploadStickerFile" ) const ( @@ -97,7 +108,10 @@ const ( MimeZIP = "application/zip" ) -const PrefixAttach = "attach://" +const ( + SchemeAttach = "attach" + SchemeTelegram = "tg" +) const ( StatusAdministrator = "administrator" diff --git a/set_webhook.go b/set_webhook.go index 29892c3..d912613 100644 --- a/set_webhook.go +++ b/set_webhook.go @@ -72,7 +72,8 @@ func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) { if params.Certificate != nil { resp, err = bot.Upload(MethodSetWebhook, "certificate", "cert.pem", params.Certificate, args) } else { - dst, err := json.Marshal(params) + var dst []byte + dst, err = json.Marshal(params) if err != nil { return false, err } diff --git a/utils.go b/utils.go index a1308bb..6b27183 100644 --- a/utils.go +++ b/utils.go @@ -5,21 +5,18 @@ import ( "strconv" ) -func NewForceReply(selective bool) *ForceReply { - return &ForceReply{ - ForceReply: true, - Selective: selective, - } +func NewForceReply() *ForceReply { + return &ForceReply{ForceReply: true} } -func NewInlineMentionURL(id int) *url.URL { +func NewInlineMentionURL(userID int) *url.URL { link := &url.URL{ - Scheme: "tg", + Scheme: SchemeTelegram, Path: "user", } q := link.Query() - q.Add("id", strconv.Itoa(id)) + q.Add("id", strconv.Itoa(userID)) link.RawQuery = q.Encode() return link diff --git a/utils_bot.go b/utils_bot.go index c94266d..e2de5c2 100644 --- a/utils_bot.go +++ b/utils_bot.go @@ -28,7 +28,7 @@ func (bot *Bot) IsMessageFromMe(msg *Message) bool { return false } - return msg.From.ID == bot.User.ID + return msg.From.ID == bot.ID } func (bot *Bot) IsForwardFromMe(msg *Message) bool { @@ -44,7 +44,7 @@ func (bot *Bot) IsForwardFromMe(msg *Message) bool { return false } - return msg.ForwardFrom.ID == bot.User.ID + return msg.ForwardFrom.ID == bot.ID } func (bot *Bot) IsReplyToMe(msg *Message) bool { @@ -99,7 +99,7 @@ func (bot *Bot) IsMessageMentionsMe(msg *Message) bool { for _, entity := range entities { if entity.IsMention() { - if bot.User.ID == entity.User.ID { + if bot.ID == entity.User.ID { return true } } diff --git a/utils_chat.go b/utils_chat.go index 2369991..331137e 100644 --- a/utils_chat.go +++ b/utils_chat.go @@ -1,5 +1,7 @@ package telegram +import "fmt" + func (chat *Chat) IsPrivate() bool { if chat == nil { return false @@ -39,3 +41,40 @@ func (chat *Chat) HasPinnedMessage() bool { return chat.PinnedMessage != nil } + +func (chat *Chat) HasStickerSet() bool { + if chat == nil { + return false + } + + return chat.StickerSetName != "" +} + +func (chat *Chat) StickerSet(bot *Bot) *StickerSet { + if !chat.HasStickerSet() { + return nil + } + + if bot == nil { + return nil + } + + set, err := bot.GetStickerSet(chat.StickerSetName) + if err != nil { + return nil + } + + return set +} + +func (chat *Chat) FullName() string { + if chat == nil { + return "" + } + + if chat.LastName != "" { + return fmt.Sprintln(chat.FirstName, chat.LastName) + } + + return chat.FirstName +} diff --git a/utils_entity.go b/utils_entity.go index 3d6ad9e..d13516f 100644 --- a/utils_entity.go +++ b/utils_entity.go @@ -10,28 +10,24 @@ func (entity *MessageEntity) ParseURL(messageText string) *url.URL { return nil } - var err error - link := new(url.URL) - switch { - case entity.IsTextLink(): - link, err = url.Parse(entity.URL) - case entity.IsURL(): - if messageText == "" { - return nil - } + if !entity.IsURL() { + return nil + } - rawMessageText := []rune(messageText) - if len(rawMessageText) < (entity.Offset + entity.Length) { - return nil - } + if messageText == "" { + return nil + } - from := entity.Offset - to := from + entity.Length - rawURL := string([]rune(messageText)[from:to]) - link, err = url.Parse(rawURL) - if err == nil && link.Scheme == "" { - link, err = url.Parse(fmt.Sprint("http://", link)) - } + from := entity.Offset + to := from + entity.Length + text := []rune(messageText) + if len(text) < to { + return nil + } + + link, err := url.Parse(string(text[from:to])) + if err == nil && link.Scheme == "" { + link, err = url.Parse(fmt.Sprint("http://", link)) } if err != nil { return nil @@ -127,3 +123,16 @@ func (entity *MessageEntity) IsURL() bool { return entity.Type == EntityURL } + +func (entity *MessageEntity) TextLink() *url.URL { + if entity == nil { + return nil + } + + link, err := url.Parse(entity.URL) + if err != nil { + return nil + } + + return link +} diff --git a/utils_message.go b/utils_message.go index 29aa0e7..db3143a 100644 --- a/utils_message.go +++ b/utils_message.go @@ -15,7 +15,6 @@ func (msg *Message) IsCommand(command string) bool { } entity := msg.Entities[0] - isBotCommand := entity.IsBotCommand() && entity.Offset == 0 if command != "" { return isBotCommand && strings.EqualFold(msg.Command(), command) @@ -107,6 +106,10 @@ func (msg *Message) EditTime() time.Time { } func (msg *Message) HasBeenEdited() bool { + if msg == nil { + return false + } + return msg.EditDate > 0 } @@ -141,6 +144,7 @@ func (msg *Message) IsSticker() bool { func (msg *Message) IsVideo() bool { return !msg.IsText() && msg.Video != nil } + func (msg *Message) IsVoice() bool { return !msg.IsText() && msg.Voice != nil } diff --git a/utils_user.go b/utils_user.go index 60cc8cb..5ebf53d 100644 --- a/utils_user.go +++ b/utils_user.go @@ -25,7 +25,7 @@ func (user *User) FullName() string { } if user.LastName != "" { - return fmt.Sprint(user.FirstName, " ", user.LastName) + return fmt.Sprintln(user.FirstName, user.LastName) } return user.FirstName