Updates for Bot API 4.7.
parent
6ce4ec747d
commit
75e27e1380
27
bot.go
27
bot.go
|
@ -62,8 +62,9 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
|
||||||
return bot, nil
|
return bot, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
|
// SetAPIEndpoint changes the Telegram Bot API endpoint used by the instance.
|
||||||
b.apiEndpoint = apiEndpoint
|
func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
|
||||||
|
bot.apiEndpoint = apiEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildParams(in Params) (out url.Values) {
|
func buildParams(in Params) (out url.Values) {
|
||||||
|
@ -473,7 +474,7 @@ func WriteToHTTPResponse(w http.ResponseWriter, c Chattable) error {
|
||||||
|
|
||||||
if t, ok := c.(Fileable); ok {
|
if t, ok := c.(Fileable); ok {
|
||||||
if !t.useExistingFile() {
|
if !t.useExistingFile() {
|
||||||
return errors.New("Can't use HTTP Response to upload files.")
|
return errors.New("unable to use http response to upload files")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -610,3 +611,23 @@ func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) {
|
||||||
|
|
||||||
return poll, err
|
return poll, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMyCommands gets the currently registered commands.
|
||||||
|
func (bot *BotAPI) GetMyCommands() ([]BotCommand, error) {
|
||||||
|
config := GetMyCommandsConfig{}
|
||||||
|
|
||||||
|
params, err := config.params()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := bot.MakeRequest(config.method(), params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var commands []BotCommand
|
||||||
|
err = json.Unmarshal(resp.Result, &commands)
|
||||||
|
|
||||||
|
return commands, err
|
||||||
|
}
|
||||||
|
|
41
bot_test.go
41
bot_test.go
|
@ -733,3 +733,44 @@ func TestPolls(t *testing.T) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendDice(t *testing.T) {
|
||||||
|
bot, _ := getBot(t)
|
||||||
|
|
||||||
|
dice := NewSendDice(ChatID)
|
||||||
|
|
||||||
|
msg, err := bot.Send(dice)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unable to send dice roll")
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Dice == nil {
|
||||||
|
t.Error("Dice roll was not received")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetCommands(t *testing.T) {
|
||||||
|
bot, _ := getBot(t)
|
||||||
|
|
||||||
|
setCommands := NewSetMyCommands(BotCommand{
|
||||||
|
Command: "test",
|
||||||
|
Description: "a test command",
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := bot.Request(setCommands); err != nil {
|
||||||
|
t.Error("Unable to set commands")
|
||||||
|
}
|
||||||
|
|
||||||
|
commands, err := bot.GetMyCommands()
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unable to get commands")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(commands) != 1 {
|
||||||
|
t.Error("Incorrect number of commands returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
if commands[0].Command != "test" || commands[0].Description != "a test command" {
|
||||||
|
t.Error("Commands were incorrectly set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
98
configs.go
98
configs.go
|
@ -1417,11 +1417,14 @@ func (config UploadStickerConfig) useExistingFile() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStickerSetConfig allows creating a new sticker set.
|
// NewStickerSetConfig allows creating a new sticker set.
|
||||||
|
//
|
||||||
|
// You must set either PNGSticker or TGSSticker.
|
||||||
type NewStickerSetConfig struct {
|
type NewStickerSetConfig struct {
|
||||||
UserID int64
|
UserID int64
|
||||||
Name string
|
Name string
|
||||||
Title string
|
Title string
|
||||||
PNGSticker interface{}
|
PNGSticker interface{}
|
||||||
|
TGSSticker interface{}
|
||||||
Emojis string
|
Emojis string
|
||||||
ContainsMasks bool
|
ContainsMasks bool
|
||||||
MaskPosition *MaskPosition
|
MaskPosition *MaskPosition
|
||||||
|
@ -1440,6 +1443,8 @@ func (config NewStickerSetConfig) params() (Params, error) {
|
||||||
|
|
||||||
if sticker, ok := config.PNGSticker.(string); ok {
|
if sticker, ok := config.PNGSticker.(string); ok {
|
||||||
params[config.name()] = sticker
|
params[config.name()] = sticker
|
||||||
|
} else if sticker, ok := config.TGSSticker.(string); ok {
|
||||||
|
params[config.name()] = sticker
|
||||||
}
|
}
|
||||||
|
|
||||||
params["emojis"] = config.Emojis
|
params["emojis"] = config.Emojis
|
||||||
|
@ -1460,9 +1465,17 @@ func (config NewStickerSetConfig) name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config NewStickerSetConfig) useExistingFile() bool {
|
func (config NewStickerSetConfig) useExistingFile() bool {
|
||||||
_, ok := config.PNGSticker.(string)
|
if config.PNGSticker != nil {
|
||||||
|
_, ok := config.PNGSticker.(string)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
return ok
|
if config.TGSSticker != nil {
|
||||||
|
_, ok := config.TGSSticker.(string)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("NewStickerSetConfig had nil PNGSticker and TGSSticker")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddStickerConfig allows you to add a sticker to a set.
|
// AddStickerConfig allows you to add a sticker to a set.
|
||||||
|
@ -1470,6 +1483,7 @@ type AddStickerConfig struct {
|
||||||
UserID int64
|
UserID int64
|
||||||
Name string
|
Name string
|
||||||
PNGSticker interface{}
|
PNGSticker interface{}
|
||||||
|
TGSSticker interface{}
|
||||||
Emojis string
|
Emojis string
|
||||||
MaskPosition *MaskPosition
|
MaskPosition *MaskPosition
|
||||||
}
|
}
|
||||||
|
@ -1487,6 +1501,8 @@ func (config AddStickerConfig) params() (Params, error) {
|
||||||
|
|
||||||
if sticker, ok := config.PNGSticker.(string); ok {
|
if sticker, ok := config.PNGSticker.(string); ok {
|
||||||
params[config.name()] = sticker
|
params[config.name()] = sticker
|
||||||
|
} else if sticker, ok := config.TGSSticker.(string); ok {
|
||||||
|
params[config.name()] = sticker
|
||||||
}
|
}
|
||||||
|
|
||||||
err := params.AddInterface("mask_position", config.MaskPosition)
|
err := params.AddInterface("mask_position", config.MaskPosition)
|
||||||
|
@ -1542,6 +1558,43 @@ func (config DeleteStickerConfig) params() (Params, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStickerSetThumbConfig allows you to set the thumbnail for a sticker set.
|
||||||
|
type SetStickerSetThumbConfig struct {
|
||||||
|
Name string
|
||||||
|
UserID int
|
||||||
|
Thumb interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetStickerSetThumbConfig) method() string {
|
||||||
|
return "setStickerSetThumb"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetStickerSetThumbConfig) params() (Params, error) {
|
||||||
|
params := make(Params)
|
||||||
|
|
||||||
|
params["name"] = config.Name
|
||||||
|
params.AddNonZero("user_id", config.UserID)
|
||||||
|
|
||||||
|
if thumb, ok := config.Thumb.(string); ok {
|
||||||
|
params["thumb"] = thumb
|
||||||
|
}
|
||||||
|
|
||||||
|
return params, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetStickerSetThumbConfig) name() string {
|
||||||
|
return "thumb"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetStickerSetThumbConfig) getFile() interface{} {
|
||||||
|
return config.Thumb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetStickerSetThumbConfig) useExistingFile() bool {
|
||||||
|
_, ok := config.Thumb.(string)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
|
// SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
|
||||||
type SetChatStickerSetConfig struct {
|
type SetChatStickerSetConfig struct {
|
||||||
ChatID int64
|
ChatID int64
|
||||||
|
@ -1609,3 +1662,44 @@ func (config MediaGroupConfig) params() (Params, error) {
|
||||||
|
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiceConfig allows you to send a random dice roll to Telegram.
|
||||||
|
type DiceConfig struct {
|
||||||
|
BaseChat
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config DiceConfig) method() string {
|
||||||
|
return "sendDice"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config DiceConfig) params() (Params, error) {
|
||||||
|
return config.BaseChat.params()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMyCommandsConfig gets a list of the currently registered commands.
|
||||||
|
type GetMyCommandsConfig struct{}
|
||||||
|
|
||||||
|
func (config GetMyCommandsConfig) method() string {
|
||||||
|
return "getMyCommands"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config GetMyCommandsConfig) params() (Params, error) {
|
||||||
|
return make(Params), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMyCommandsConfig sets a list of commands the bot understands.
|
||||||
|
type SetMyCommandsConfig struct {
|
||||||
|
commands []BotCommand
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetMyCommandsConfig) method() string {
|
||||||
|
return "setMyCommands"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SetMyCommandsConfig) params() (Params, error) {
|
||||||
|
params := make(Params)
|
||||||
|
|
||||||
|
err := params.AddInterface("commands", config.commands)
|
||||||
|
|
||||||
|
return params, err
|
||||||
|
}
|
||||||
|
|
16
helpers.go
16
helpers.go
|
@ -509,7 +509,7 @@ func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInlineQueryResultCachedPhoto create a new inline query with cached photo.
|
// NewInlineQueryResultCachedMPEG4GIF create a new inline query with cached photo.
|
||||||
func NewInlineQueryResultCachedMPEG4GIF(id, MPEG4GifID string) InlineQueryResultCachedMpeg4Gif {
|
func NewInlineQueryResultCachedMPEG4GIF(id, MPEG4GifID string) InlineQueryResultCachedMpeg4Gif {
|
||||||
return InlineQueryResultCachedMpeg4Gif{
|
return InlineQueryResultCachedMpeg4Gif{
|
||||||
Type: "mpeg4_gif",
|
Type: "mpeg4_gif",
|
||||||
|
@ -914,3 +914,17 @@ func NewStopPoll(chatID int64, messageID int) StopPollConfig {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSendDice allows you to send a random dice roll.
|
||||||
|
func NewSendDice(chatID int64) DiceConfig {
|
||||||
|
return DiceConfig{
|
||||||
|
BaseChat{
|
||||||
|
ChatID: chatID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSetMyCommands allows you to set the registered commands.
|
||||||
|
func NewSetMyCommands(commands ...BotCommand) SetMyCommandsConfig {
|
||||||
|
return SetMyCommandsConfig{commands: commands}
|
||||||
|
}
|
||||||
|
|
23
types.go
23
types.go
|
@ -186,6 +186,7 @@ type Message struct {
|
||||||
Location *Location `json:"location"` // optional
|
Location *Location `json:"location"` // optional
|
||||||
Venue *Venue `json:"venue"` // optional
|
Venue *Venue `json:"venue"` // optional
|
||||||
Poll *Poll `json:"poll"` // optional
|
Poll *Poll `json:"poll"` // optional
|
||||||
|
Dice *Dice `json:"dice"` // optional
|
||||||
NewChatMembers []User `json:"new_chat_members"` // optional
|
NewChatMembers []User `json:"new_chat_members"` // optional
|
||||||
LeftChatMember *User `json:"left_chat_member"` // optional
|
LeftChatMember *User `json:"left_chat_member"` // optional
|
||||||
NewChatTitle string `json:"new_chat_title"` // optional
|
NewChatTitle string `json:"new_chat_title"` // optional
|
||||||
|
@ -495,6 +496,11 @@ type Poll struct {
|
||||||
CorrectOptionID int `json:"correct_option_id"` // optional
|
CorrectOptionID int `json:"correct_option_id"` // optional
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dice represents a single dice value.
|
||||||
|
type Dice struct {
|
||||||
|
Value int `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
// UserProfilePhotos contains a set of user profile photos.
|
// UserProfilePhotos contains a set of user profile photos.
|
||||||
type UserProfilePhotos struct {
|
type UserProfilePhotos struct {
|
||||||
TotalCount int `json:"total_count"`
|
TotalCount int `json:"total_count"`
|
||||||
|
@ -1067,11 +1073,18 @@ type PreCheckoutQuery struct {
|
||||||
|
|
||||||
// StickerSet is a collection of stickers.
|
// StickerSet is a collection of stickers.
|
||||||
type StickerSet struct {
|
type StickerSet struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
IsAnimated bool `json:"is_animated"`
|
IsAnimated bool `json:"is_animated"`
|
||||||
ContainsMasks bool `json:"contains_masks"`
|
ContainsMasks bool `json:"contains_masks"`
|
||||||
Stickers []Sticker `json:"stickers"`
|
Stickers []Sticker `json:"stickers"`
|
||||||
|
Thumb *PhotoSize `json:"thumb"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BotCommand represents Telegram's understanding of a command.
|
||||||
|
type BotCommand struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BaseInputMedia is a base type for the InputMedia types.
|
// BaseInputMedia is a base type for the InputMedia types.
|
||||||
|
|
Loading…
Reference in New Issue