mirror of https://github.com/tucnak/telebot
games: complete Games API
parent
9c08de946d
commit
1533291206
@ -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
|
||||
}
|
Loading…
Reference in New Issue