From 51566f13995a68d764ae7ba5424a35e62c209985 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 12 Feb 2018 15:46:57 +0500 Subject: [PATCH 1/5] :sparkles: Added login package For parsing User data from url.Values and validation. close #4 --- login/check_authorization.go | 37 ++++++++++++++++++++++++++++++++ login/doc.go | 3 +++ login/new.go | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 login/check_authorization.go create mode 100644 login/doc.go create mode 100644 login/new.go diff --git a/login/check_authorization.go b/login/check_authorization.go new file mode 100644 index 0000000..014c832 --- /dev/null +++ b/login/check_authorization.go @@ -0,0 +1,37 @@ +package login + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "fmt" +) + +// CheckAuthorization verify the authentication and the integrity of the data +// received by comparing the received hash parameter with the hexadecimal +// representation of the HMAC-SHA-256 signature of the data-check-string with the +// SHA256 hash of the bot's token used as a secret key. +func (user *User) CheckAuthorization(botToken string) (bool, error) { + dataCheckString := fmt.Sprint( + "auth_date=", user.AuthDate.Unix(), + "\n", "first_name=", user.FirstName, + // Eliminate 'hash' to avoid recursion and incorrect data validation. + "\n", "id=", user.ID, + ) + + // Add optional values if exist + if user.LastName != "" { + dataCheckString += fmt.Sprint("\n", "last_name=", user.LastName) + } + if user.PhotoURL != "" { + dataCheckString += fmt.Sprint("\n", "photo_url=", user.PhotoURL) + } + if user.Username != "" { + dataCheckString += fmt.Sprint("\n", "username=", user.Username) + } + + secretKey := sha256.Sum256([]byte(botToken)) + hash := hmac.New(sha256.New, secretKey[0:]) + _, err := hash.Write([]byte(dataCheckString)) + return hex.EncodeToString(hash.Sum(nil)) == user.Hash, err +} diff --git a/login/doc.go b/login/doc.go new file mode 100644 index 0000000..155d5af --- /dev/null +++ b/login/doc.go @@ -0,0 +1,3 @@ +// Package login contains methods for obtaining structure of the user data and +// its validation. +package login diff --git a/login/new.go b/login/new.go new file mode 100644 index 0000000..dcfa782 --- /dev/null +++ b/login/new.go @@ -0,0 +1,41 @@ +package login + +import ( + "net/url" + "strconv" + "time" +) + +// User contains data about authenticated user. +type User struct { + AuthDate time.Time `json:"auth_date"` + FirstName string `json:"first_name"` + Hash string `json:"hash"` + ID int `json:"id"` + LastName string `json:"last_name,omitempty"` + PhotoURL string `json:"photo_url,omitempty"` + Username string `json:"username,omitempty"` +} + +// New create User structure from input url.Values. +func New(src url.Values) (*User, error) { + authDate, err := strconv.Atoi(src.Get("auth_date")) + if err != nil { + return nil, err + } + + id, err := strconv.Atoi(src.Get("id")) + if err != nil { + return nil, err + } + + return &User{ + AuthDate: time.Unix(int64(authDate), 0), + FirstName: src.Get("first_name"), + Hash: src.Get("hash"), + ID: id, + LastName: src.Get("last_name"), + PhotoURL: src.Get("photo_url"), + Username: src.Get("username"), + }, nil +} From c879363a57673276f3d01fca9a0635dfef97f7ca Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 12 Feb 2018 15:49:41 +0500 Subject: [PATCH 2/5] :truck: Rename 'types.go' to 'models.go' --- types.go => models.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename types.go => models.go (100%) diff --git a/types.go b/models.go similarity index 100% rename from types.go rename to models.go From 9ca648be62d4127c96a3eb1ccc6bab6d2b14b948 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 14 Feb 2018 22:39:58 +0500 Subject: [PATCH 3/5] :building_construction: Added ParseMode support in media close #1 --- edit_message_caption.go | 4 +++ models.go | 60 +++++++++++++++++++++++++++++++++++++++++ send_document.go | 4 +++ send_photo.go | 4 +++ 4 files changed, 72 insertions(+) diff --git a/edit_message_caption.go b/edit_message_caption.go index 92f7ed7..33e0215 100644 --- a/edit_message_caption.go +++ b/edit_message_caption.go @@ -19,6 +19,10 @@ type EditMessageCaptionParameters struct { // New caption of the message Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // A JSON-serialized object for an inline keyboard. ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` } diff --git a/models.go b/models.go index d6e6a21..fefeafb 100644 --- a/models.go +++ b/models.go @@ -871,6 +871,10 @@ type ( // Caption of the photo to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` } // InputMediaVideo represents a video to be sent. @@ -888,6 +892,10 @@ type ( // Caption of the video to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Video width Width int `json:"width,omitempty"` @@ -1078,6 +1086,10 @@ type ( // Caption of the photo to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1117,6 +1129,10 @@ type ( // Caption of the GIF file to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1194,6 +1210,10 @@ type ( // Caption of the video to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Video width VideoWidth int `json:"video_width,omitempty"` @@ -1235,6 +1255,10 @@ type ( // Caption, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Performer Performer string `json:"performer,omitempty"` @@ -1268,6 +1292,10 @@ type ( // Caption, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Recording duration in seconds VoiceDuration int `json:"voice_duration,omitempty"` @@ -1296,6 +1324,10 @@ type ( // Caption of the document to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // A valid URL for the file DocumentURL string `json:"document_url"` @@ -1473,6 +1505,10 @@ type ( // Caption of the photo to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1501,6 +1537,10 @@ type ( // Caption of the GIF file to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1529,6 +1569,10 @@ type ( // Caption of the MPEG-4 file to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1580,6 +1624,10 @@ type ( // Caption of the document to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1611,6 +1659,10 @@ type ( // Caption of the video to be sent, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1638,6 +1690,10 @@ type ( // Caption, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` @@ -1662,6 +1718,10 @@ type ( // Caption, 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Inline keyboard attached to the message ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` diff --git a/send_document.go b/send_document.go index 7b442f0..0c54119 100644 --- a/send_document.go +++ b/send_document.go @@ -19,6 +19,10 @@ type SendDocumentParameters struct { // Document caption (may also be used when resending documents by file_id), 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Sends the message silently. Users will receive a notification with no sound. DisableNotification bool `json:"disable_notification,omitempty"` diff --git a/send_photo.go b/send_photo.go index c77e87a..63c2948 100644 --- a/send_photo.go +++ b/send_photo.go @@ -19,6 +19,10 @@ type SendPhotoParameters struct { // Photo caption (may also be used when resending photos by file_id), 0-200 characters Caption string `json:"caption,omitempty"` + // Send Markdown or HTML, if you want Telegram apps to show bold, italic, + // fixed-width text or inline URLs in the media caption. + ParseMode string `json:"parse_mode,omitempty"` + // Disables link previews for links in this message DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty"` From ce1dfd82ce8033502e7f4e7e5c04c3750cd98668 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 14 Feb 2018 22:46:41 +0500 Subject: [PATCH 4/5] :building_construction: Added ConnectedWebsite field in Message close #2 --- models.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models.go b/models.go index fefeafb..a19706c 100644 --- a/models.go +++ b/models.go @@ -371,6 +371,9 @@ type ( // Message is a service message about a successful payment, information // about the payment. SuccessfulPayment *SuccessfulPayment `json:"successful_payment,omitempty"` + + // The domain name of the website on which the user has logged in. + ConnectedWebsite string `json:"connected_website,omitempty"` } // MessageEntity represents one special entity in a text message. For From 1c25545cbb9b14809320d2617f5c42e920aec23f Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 14 Feb 2018 22:51:13 +0500 Subject: [PATCH 5/5] :building_construction: Added SupportStreaming field in InputMediaVideo close #3 --- models.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models.go b/models.go index a19706c..f0d1f5f 100644 --- a/models.go +++ b/models.go @@ -907,6 +907,9 @@ type ( // Video duration Duration int `json:"duration,omitempty"` + + // Pass true, if the uploaded video is suitable for streaming + SupportsStreaming bool `json:"supports_streaming,omitempty"` } // InputFile represents the contents of a file to be uploaded. Must be posted