Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: implement 6.6, 6.7 features #655

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,9 @@ func (b *Bot) Close() (bool, error) {

// BotInfo represents a single object of BotName, BotDescription, BotShortDescription instances.
type BotInfo struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ShortDesc string `json:"short_description,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ShortDescription string `json:"short_description,omitempty"`
}

// SetMyName change's the bot name.
Expand Down Expand Up @@ -1194,10 +1194,10 @@ func (b *Bot) MyDescription(language string) (*BotInfo, error) {

// SetMyShortDescription change's the bot short description, which is shown on
// the bot's profile page and is sent together with the link when users share the bot.
func (b *Bot) SetMyShortDescription(sdesc, lc string) error {
func (b *Bot) SetMyShortDescription(desc, language string) error {
params := map[string]string{
"short_description": sdesc,
"language_code": lc,
"short_description": desc,
"language_code": language,
}

_, err := b.Raw("setMyShortDescription", params)
Expand Down
15 changes: 12 additions & 3 deletions inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ type QueryResponse struct {
// pagination. Offset length can’t exceed 64 bytes.
NextOffset string `json:"next_offset"`

// (Optional) If passed, clients will display a button with specified
// text that switches the user to a private chat with the bot and sends
// the bot a start message with the parameter switch_pm_parameter.
SwitchPMText string `json:"switch_pm_text,omitempty"`

// (Optional) Parameter for the start message sent to the bot when user
// presses the switch button.
SwitchPMParameter string `json:"switch_pm_parameter,omitempty"`

// (Optional) A JSON-serialized object describing a button to be shown
// above inline query results.
Button *QueryResponseButton `json:"button"`
Button *QueryResponseButton `json:"button,omitempty"`
}

// QueryResponseButton represents a button to be shown above inline query results.
Expand All @@ -74,9 +83,9 @@ type QueryResponseButton struct {
Start string `json:"start_parameter"`
}

// InlineQueryChosenChat represents an inline button that switches the current
// SwitchInlineQuery represents an inline button that switches the current
// user to inline mode in a chosen chat, with an optional default inline query.
type InlineQueryChosenChat struct {
type SwitchInlineQuery struct {
// (Optional) The default inline query to be inserted in the input field.
// If left empty, only the bot's username will be inserted.
Query string `json:"query"`
Expand Down
16 changes: 8 additions & 8 deletions markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,14 @@ type InlineButton struct {
// It will be used as a callback endpoint.
Unique string `json:"unique,omitempty"`

Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
InlineQueryChat string `json:"switch_inline_query_current_chat"`
InlineQueryChosenChat *InlineQueryChosenChat `json:"switch_inline_query_chosen_chat"`
Login *Login `json:"login_url,omitempty"`
WebApp *WebApp `json:"web_app,omitempty"`
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
InlineQueryChat string `json:"switch_inline_query_current_chat"`
InlineQueryChosenChat *SwitchInlineQuery `json:"switch_inline_query_chosen_chat,omitempty"`
Login *Login `json:"login_url,omitempty"`
WebApp *WebApp `json:"web_app,omitempty"`
}

// MarshalJSON implements json.Marshaler interface.
Expand Down
5 changes: 1 addition & 4 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ type Sticker struct {
Repaint bool `json:"needs_repainting"`
Emojis []string `json:"emoji_list"`
Keywords []string `json:"keywords"`
Format string `json:"sticker_format,omitempty"`
}

func (s *Sticker) MediaType() string {
Expand All @@ -307,9 +306,7 @@ func (s *Sticker) MediaFile() *File {

func (s *Sticker) InputMedia() InputMedia {
return InputMedia{
Type: s.Type,
Width: s.Width,
Height: s.Height,
Type: s.MediaType(),
}
}

Expand Down
156 changes: 59 additions & 97 deletions stickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telebot

import (
"encoding/json"
"fmt"
"strconv"
)

Expand All @@ -13,22 +14,29 @@ const (
StickerCustomEmoji = "custom_emoji"
)

type StickerSetFormat = string

const (
StickerStatic = "static"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding a context to these constants:

	StickerStatic   StickerSetFormat = "static"
	StickerAnimated StickerSetFormat = "animated"
	StickerVideo    StickerSetFormat = "video"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do same for the StickerSetType above.

StickerAnimated = "animated"
StickerVideo = "video"
)

// StickerSet represents a sticker set.
type StickerSet struct {
Type StickerSetType `json:"sticker_type"`
Name string `json:"name"`
Title string `json:"title"`
Animated bool `json:"is_animated"`
Video bool `json:"is_video"`
Stickers []Sticker `json:"stickers"`
Thumbnail *Photo `json:"thumbnail"`
PNG *File `json:"png_sticker"`
TGS *File `json:"tgs_sticker"`
WebM *File `json:"webm_sticker"`
Emojis string `json:"emojis"`
ContainsMasks bool `json:"contains_masks"` // FIXME: can be removed
MaskPosition *MaskPosition `json:"mask_position"`
Repaint bool `json:"needs_repainting"`
Type StickerSetType `json:"sticker_type"`
Format StickerSetFormat `json:"sticker_format"`
Name string `json:"name"`
Title string `json:"title"`
Animated bool `json:"is_animated"`
Video bool `json:"is_video"`
Stickers []Sticker `json:"stickers"`
Sticker Sticker `json:"sticker"`
Thumbnail *Photo `json:"thumbnail"`
Emojis string `json:"emojis"`
ContainsMasks bool `json:"contains_masks"` // FIXME: can be removed
MaskPosition *MaskPosition `json:"mask_position"`
Repaint bool `json:"needs_repainting"`
}

// MaskPosition describes the position on faces where
Expand All @@ -51,32 +59,9 @@ const (
)

// UploadSticker uploads a PNG file with a sticker for later use.
func (b *Bot) UploadSticker(to Recipient, png *File) (*File, error) {
func (b *Bot) UploadSticker(to Recipient, s StickerSet) (*File, error) {
files := map[string]File{
"png_sticker": *png,
}
params := map[string]string{
"user_id": to.Recipient(),
}

data, err := b.sendFiles("uploadStickerFile", files, params)
if err != nil {
return nil, err
}

var resp struct {
Result File
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return &resp.Result, nil
}

// UploadStickerFile uploads a PNG file with a sticker for later use.
func (b *Bot) UploadStickerFile(to Recipient, s Sticker) (*File, error) {
files := map[string]File{
"sticker": s.File,
"sticker": s.Sticker.File,
}

params := map[string]string{
Expand Down Expand Up @@ -117,81 +102,53 @@ func (b *Bot) StickerSet(name string) (*StickerSet, error) {
// CreateStickerSet creates a new sticker set.
func (b *Bot) CreateStickerSet(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["png_sticker"] = *s.PNG
for i, sticker := range s.Stickers {
key := fmt.Sprint("sticker", i)
files[key] = sticker.File
}
if s.TGS != nil {
files["tgs_sticker"] = *s.TGS
}
if s.WebM != nil {
files["webm_sticker"] = *s.WebM

data, err := json.Marshal(s.Stickers)
if err != nil {
return err
}

params := map[string]string{
"user_id": to.Recipient(),
"sticker_type": s.Type,
"name": s.Name,
"title": s.Title,
"emojis": s.Emojis,
"sticker_type": s.Type,
"sticker_format": s.Format,
"stickers": string(data),
"needs_repainting": strconv.FormatBool(s.Repaint),
"contains_masks": strconv.FormatBool(s.ContainsMasks),
}

if s.MaskPosition != nil {
data, _ := json.Marshal(&s.MaskPosition)
params["mask_position"] = string(data)
}

_, err := b.sendFiles("createNewStickerSet", files, params)
return err
}

// AddSticker adds a new sticker to the existing sticker set.
func (b *Bot) AddSticker(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["png_sticker"] = *s.PNG
} else if s.TGS != nil {
files["tgs_sticker"] = *s.TGS
} else if s.WebM != nil {
files["webm_sticker"] = *s.WebM
}

params := map[string]string{
"user_id": to.Recipient(),
"name": s.Name,
"emojis": s.Emojis,
}

if s.MaskPosition != nil {
data, _ := json.Marshal(&s.MaskPosition)
params["mask_position"] = string(data)
}

_, err := b.sendFiles("addStickerToSet", files, params)
_, err = b.sendFiles("createNewStickerSet", files, params)
return err
}

// AddStickerToSet adds a new sticker to the existing sticker set.
func (b *Bot) AddStickerToSet(to Recipient, name string, s Sticker) error {
files := make(map[string]File)
files["sticker"] = s.File
func (b *Bot) AddStickerToSet(to Recipient, s StickerSet) error {
var (
files = make(map[string]File)
sticker = s.Sticker
)
files["sticker"] = sticker.File

params := map[string]string{
"user_id": to.Recipient(),
"name": name,
"name": s.Name,
}

if s.Emojis != nil {
if sticker.Emojis != nil {
data, _ := json.Marshal(s.Emojis)
params["emoji_list"] = string(data)
}
if s.MaskPosition != nil {
data, _ := json.Marshal(&s.MaskPosition)
data, _ := json.Marshal(s.MaskPosition)
params["mask_position"] = string(data)
}
if s.Keywords != nil {
data, _ := json.Marshal(s.Keywords)
if sticker.Keywords != nil {
data, _ := json.Marshal(sticker.Keywords)
params["keywords"] = string(data)
}

Expand Down Expand Up @@ -226,19 +183,24 @@ func (b *Bot) DeleteSticker(sticker string) error {
//
// Animated sticker set thumbnail can't be uploaded via HTTP URL.
func (b *Bot) SetStickerSetThumb(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["thumbnail"] = *s.PNG
} else if s.TGS != nil {
files["thumbnail"] = *s.TGS
var (
sticker = s.Sticker
files = make(map[string]File)
)
files["thumbnail"] = sticker.File

data, err := json.Marshal(sticker.File)
if err != nil {
return err
}

params := map[string]string{
"name": s.Name,
"user_id": to.Recipient(),
"name": s.Name,
"user_id": to.Recipient(),
"thumbnail": string(data),
}

_, err := b.sendFiles("setStickerSetThumbnail", files, params)
_, err = b.sendFiles("setStickerSetThumbnail", files, params)
return err
}

Expand Down
Loading