games: complete Games API

pull/285/head^2
Demian 4 years ago
parent 9c08de946d
commit 1533291206

@ -662,7 +662,7 @@ func (b *Bot) Forward(to Recipient, what *Message, options ...interface{}) (*Mes
func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Message, error) {
var (
method string
params = map[string]string{}
params = make(map[string]string)
)
switch v := what.(type) {
@ -703,7 +703,7 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes
// This function will panic upon nil Editable.
func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) {
msgID, chatID := msg.MessageSig()
params := map[string]string{}
params := make(map[string]string)
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID

@ -0,0 +1,107 @@
package telebot
import (
"encoding/json"
"strconv"
)
// Game object represents a game.
// Their short names acts as unique identifiers.
type Game struct {
Name string `json:"game_short_name"`
Title string `json:"title"`
Description string `json:"description"`
Photo *Photo `json:"photo"`
// (Optional)
Text string `json:"text"`
Entities []MessageEntity `json:"text_entities"`
Animation *Animation `json:"animation"`
}
// GameHighScore object represents one row
// of the high scores table for a game.
type GameHighScore struct {
User *User `json:"user"`
Position int `json:"position"`
Score int `json:"score"`
Force bool `json:"force"`
NoEdit bool `json:"disable_edit_message"`
}
// GetGameScores returns the score of the specified user
// and several of their neighbors in a game.
//
// This method will currently return scores for the target user,
// plus two of their closest neighbors on each side.
// Will also return the top three users
// if the user and his neighbors are not among them.
//
// This function will panic upon nil Editable.
func (b *Bot) GetGameScores(user Recipient, msg Editable) ([]GameHighScore, error) {
params := map[string]string{
"user_id": user.Recipient(),
}
msgID, chatID := msg.MessageSig()
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = msgID
}
data, err := b.Raw("getGameHighScores", params)
if err != nil {
return nil, err
}
var resp struct {
Result []GameHighScore
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, err
}
return resp.Result, nil
}
// SetGameScore sets the score of the specified user in a game.
//
// NOTE:
// If the message was sent by the bot, returns the edited Message,
// otherwise returns nil Message and ErrNoGameMessage.
// If you expect successful True result, you must check
// for `telebot: no game message` error.
//
func (b *Bot) SetGameScore(user Recipient, msg Editable, score GameHighScore) (*Message, error) {
params := map[string]string{
"user_id": user.Recipient(),
"score": strconv.Itoa(score.Score),
"force": strconv.FormatBool(score.Force),
"disable_edit_message": strconv.FormatBool(score.NoEdit),
}
msgID, chatID := msg.MessageSig()
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = msgID
}
data, err := b.Raw("setGameScore", params)
if err != nil {
return nil, err
}
m, err := extractMessage(data)
if err != nil {
return nil, err
}
if m == nil {
return nil, ErrNoGameMessage
}
return m, nil
}

@ -354,7 +354,7 @@ func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
return extractMessage(data)
}
// Send delivers dice through bot b to recipient
// Send delivers dice through bot b to recipient.
func (d *Dice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
@ -369,3 +369,19 @@ func (d *Dice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
return extractMessage(data)
}
// Send delivers game through bot b to recipient.
func (g *Game) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"game_short_name": g.Title,
}
embedSendOptions(params, opt)
data, err := b.Raw("sendGame", params)
if err != nil {
return nil, err
}
return extractMessage(data)
}

@ -34,6 +34,7 @@ var (
ErrBadRecipient = errors.New("telebot: recipient is nil")
ErrUnsupportedWhat = errors.New("telebot: unsupported what argument")
ErrCouldNotUpdate = errors.New("telebot: could not fetch new updates")
ErrNoGameMessage = errors.New("telebot: no game message")
)
const DefaultApiURL = "https://api.telegram.org"

Loading…
Cancel
Save