1
0

🔀 Merge branch 'develop'

close #6
This commit is contained in:
Maxim Lebedev 2018-02-14 22:58:31 +05:00
commit aa6057b861
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
7 changed files with 159 additions and 0 deletions

View File

@ -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"`
}

View File

@ -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
}

3
login/doc.go Normal file
View File

@ -0,0 +1,3 @@
// Package login contains methods for obtaining structure of the user data and
// its validation.
package login

41
login/new.go Normal file
View File

@ -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
}

View File

@ -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
@ -871,6 +874,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 +895,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"`
@ -896,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
@ -1078,6 +1092,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 +1135,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 +1216,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 +1261,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 +1298,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 +1330,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 +1511,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 +1543,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 +1575,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 +1630,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 +1665,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 +1696,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 +1724,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"`

View File

@ -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"`

View File

@ -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"`