From 8eb1037a02a6212480dc7ebfd85701f0cce917c8 Mon Sep 17 00:00:00 2001 From: AliMVP Date: Thu, 14 Jun 2018 20:49:26 +0430 Subject: [PATCH] - Added Sticker Operations --- bot.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ configs.go | 41 ++++++++++++++++++++ types.go | 28 ++++++++++++++ 3 files changed, 177 insertions(+) diff --git a/bot.go b/bot.go index 8fb6200..197602a 100644 --- a/bot.go +++ b/bot.go @@ -950,3 +950,111 @@ func (bot *BotAPI) DeleteChatPhoto(config DeleteChatPhotoConfig) (APIResponse, e return bot.MakeRequest(config.method(), v) } + +// GetStickerSet is used to get a sticker set. +func (bot *BotAPI) GetStickerSet(name string) (StickerSet, error) { + v := url.Values{} + + v.Add("name", name) + resp, err := bot.MakeRequest("getStickerSet", v) + if err != nil { + return StickerSet{}, err + } + + var stickerSet StickerSet + err = json.Unmarshal(resp.Result, &stickerSet) + + return stickerSet, err +} + +// UploadStickerFile uploads a .png file with a sticker for later use in +// createNewStickerSet and addStickerToSet methods (can be used multiple times). +// +// File should be a string to a file path, a FileBytes struct, or a FileReader +// struct. +func (bot *BotAPI) UploadStickerFile(userID int, file interface{}) (APIResponse, File, error) { + switch file.(type) { + case url.URL: + return APIResponse{}, File{}, errors.New(ErrBadFileType) + } + + params := make(map[string]string) + params["user_id"] = strconv.Itoa(userID) + + resp, err := bot.UploadFile("uploadStickerFile", params, "png_sticker", file) + if err != nil { + return resp, File{}, err + } + + returnFile := File{} + err = json.Unmarshal(resp.Result, &returnFile) + if err != nil { + return resp, File{}, err + } + + return resp, returnFile, nil +} + +// CreateNewStickerSet creates a new sticker set owned by a user. The bot will +// be able to edit the created sticker set. +func (bot *BotAPI) CreateNewStickerSet(config CreateNewStickerSetConfig) (APIResponse, error) { + params := make(map[string]string) + + params["user_id"] = strconv.Itoa(config.UserID) + params["name"] = config.Name + params["title"] = config.Title + params["emojis"] = config.Emojis + + if config.ContainsMasks { + params["contains_masks"] = strconv.FormatBool(config.ContainsMasks) + } + + if config.MaskPosition != nil { + maskPosition, err := json.Marshal(&config.MaskPosition) + if err != nil { + return APIResponse{}, err + } + + params["mask_position"] = string(maskPosition) + } + + return bot.UploadFile("createNewStickerSet", params, "png_sticker", config.PNGSticker) +} + +// AddStickerToSet adds a new sticker to a set created by the bot. +func (bot *BotAPI) AddStickerToSet(config AddStickerToSetConfig) (APIResponse, error) { + params := make(map[string]string) + + params["user_id"] = strconv.Itoa(config.UserID) + params["name"] = config.Name + params["emojis"] = config.Emojis + + if config.MaskPosition != nil { + maskPosition, err := json.Marshal(&config.MaskPosition) + if err != nil { + return APIResponse{}, err + } + + params["mask_position"] = string(maskPosition) + } + + return bot.UploadFile("addStickerToSet", params, "png_sticker", config.PNGSticker) +} + +// SetStickerPositionInSet moves a sticker in a set created by the bot to a specific position. +func (bot *BotAPI) SetStickerPositionInSet(config SetStickerPositionInSetConfig) (APIResponse, error) { + v, _ := config.values() + + bot.debugLog(config.method(), v, nil) + + return bot.MakeRequest(config.method(), v) +} + +// DeleteStickerFromSet deletes a sticker from a set created by the bot. +func (bot *BotAPI) DeleteStickerFromSet(sticker string) (APIResponse, error) { + v := url.Values{} + + v.Add("sticker", sticker) + + return bot.MakeRequest("deleteStickerFromSet", v) +} diff --git a/configs.go b/configs.go index ff286c1..c4b7b6d 100644 --- a/configs.go +++ b/configs.go @@ -1200,3 +1200,44 @@ type InputMediaVideo struct { Duration int `json:"duration"` SupportsStreaming bool `json:"supports_streaming"` } + +// CreateNewStickerSetConfig contains information about a CreateNewStickerSet +// request. +type CreateNewStickerSetConfig struct { + UserID int // required + Name string // required + Title string // required + PNGSticker interface{} // required + Emojis string // required + ContainsMasks bool + MaskPosition *MaskPosition +} + +// AddStickerToSetConfig contains information about a AddToStickerSet request. +type AddStickerToSetConfig struct { + UserID int // required + Name string // required + PNGSticker interface{} // required + Emojis string // required + MaskPosition *MaskPosition +} + +// SetStickerPositionInSetConfig contains information about a +// SetStickerPositionInSet request. +type SetStickerPositionInSetConfig struct { + Sticker string // required + Position int64 // required +} + +func (config SetStickerPositionInSetConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("sticker", config.Sticker) + v.Add("position", strconv.FormatInt(config.Position, 10)) + + return v, nil +} + +func (config SetStickerPositionInSetConfig) method() string { + return "setStickerPositionInSet" +} diff --git a/types.go b/types.go index 0843ab9..f87e53c 100644 --- a/types.go +++ b/types.go @@ -773,6 +773,34 @@ type PreCheckoutQuery struct { OrderInfo *OrderInfo `json:"order_info,omitempty"` } +// Sticker contains information about a sticker. +type Sticker struct { + FileID string `json:"file_id"` + Width int `json:"width"` + Height int `json:"height"` + Thumbnail *PhotoSize `json:"thumb"` // optional + Emoji string `json:"emoji"` // optional + SetName string `json:"set_name"` // optional + MaskPosition *MaskPosition `json:"mask_position"` // optional + FileSize int `json:"file_size"` // optional +} + +// StickerSet represents a sticker set. +type StickerSet struct { + Name string `json:"name"` + Title string `json:"title"` + ContainsMasks bool `json:"contains_masks"` + Stickers *[]Sticker `json:"stickers"` +} + +// MaskPosition describes the position on faces where a mask should be placed by default. +type MaskPosition struct { + Point string `json:"point"` + XShift float64 `json:"x_shift"` + YShift float64 `json:"y_shift"` + scale float64 `json:"scale"` +} + // Error is an error containing extra information returned by the Telegram API. type Error struct { Message string