feat: add sendDice configs

Add sendDice config to use in Send method
as a Chattable interface.
Add NewDice and NewDiceWithEmoji helpers
Add tests

https://core.telegram.org/bots/api#senddice
bot-api-6.1
mehanizm 2020-03-31 09:02:18 +03:00
parent 2476225ecc
commit f11e1caecf
6 changed files with 103 additions and 5 deletions

5
bot.go
View File

@ -75,8 +75,9 @@ func NewBotAPIWithClient(token, apiEndpoint string, client HttpClient) (*BotAPI,
return bot, nil
}
func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
b.apiEndpoint = apiEndpoint
// SetAPIEndpoint add telegram apiEndpont to Bot
func (bot *BotAPI) SetAPIEndpoint(apiEndpoint string) {
bot.apiEndpoint = apiEndpoint
}
// MakeRequest makes a request to a specific endpoint with our token.

View File

@ -8,7 +8,7 @@ import (
"testing"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
@ -402,6 +402,32 @@ func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
}
}
func TestSendWithDice(t *testing.T) {
bot, _ := getBot(t)
msg := tgbotapi.NewDice(ChatID)
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestSendWithDiceWithEmoji(t *testing.T) {
bot, _ := getBot(t)
msg := tgbotapi.NewDiceWithEmoji(ChatID, "🏀")
_, err := bot.Send(msg)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestGetFile(t *testing.T) {
bot, _ := getBot(t)

View File

@ -1284,3 +1284,30 @@ func (config GetStickerSetConfig) values() (url.Values, error) {
v.Add("name", config.Name)
return v, nil
}
// DiceConfig contains information about a sendDice request.
type DiceConfig struct {
BaseChat
// Emoji on which the dice throw animation is based.
// Currently, must be one of “🎲”, “🎯”, or “🏀”.
// Dice can have values 1-6 for “🎲” and “🎯”, and values 1-5 for “🏀”.
// Defaults to “🎲”
Emoji string
}
// values returns a url.Values representation of DiceConfig.
func (config DiceConfig) values() (url.Values, error) {
v, err := config.BaseChat.values()
if err != nil {
return v, err
}
if config.Emoji != "" {
v.Add("emoji", config.Emoji)
}
return v, nil
}
// method returns Telegram API method name for sending Dice.
func (config DiceConfig) method() string {
return "sendDice"
}

View File

@ -18,6 +18,30 @@ func NewMessage(chatID int64, text string) MessageConfig {
}
}
// NewDice creates a new DiceConfig.
//
// chatID is where to send it
func NewDice(chatID int64) DiceConfig {
return DiceConfig{
BaseChat: BaseChat{
ChatID: chatID,
},
}
}
// NewDiceWithEmoji creates a new DiceConfig.
//
// chatID is where to send it
// emoji is type of the Dice
func NewDiceWithEmoji(chatID int64, emoji string) DiceConfig {
return DiceConfig{
BaseChat: BaseChat{
ChatID: chatID,
},
Emoji: emoji,
}
}
// NewDeleteMessage creates a request to delete a message.
func NewDeleteMessage(chatID int64, messageID int) DeleteMessageConfig {
return DeleteMessageConfig{
@ -491,7 +515,7 @@ func NewInlineQueryResultMPEG4GIF(id, url string) InlineQueryResultMPEG4GIF {
}
}
// NewInlineQueryResultCachedPhoto create a new inline query with cached photo.
// NewInlineQueryResultCachedMPEG4GIF create a new inline query with cached MPEG4 GIF.
func NewInlineQueryResultCachedMPEG4GIF(id, MPEG4GifID string) InlineQueryResultCachedMpeg4Gif {
return InlineQueryResultCachedMpeg4Gif{
Type: "mpeg4_gif",

View File

@ -1,8 +1,9 @@
package tgbotapi_test
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"testing"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func TestNewInlineQueryResultArticle(t *testing.T) {
@ -175,3 +176,21 @@ func TestNewEditMessageReplyMarkup(t *testing.T) {
}
}
func TestNewDice(t *testing.T) {
dice := tgbotapi.NewDice(42)
if dice.ChatID != 42 ||
dice.Emoji != "" {
t.Fail()
}
}
func TestNewDiceWithEmoji(t *testing.T) {
dice := tgbotapi.NewDiceWithEmoji(42, "🏀")
if dice.ChatID != 42 ||
dice.Emoji != "🏀" {
t.Fail()
}
}

View File

@ -352,6 +352,7 @@ type Sticker struct {
IsAnimated bool `json:"is_animated"` // optional
}
// StickerSet contains information about an sticker set.
type StickerSet struct {
Name string `json:"name"`
Title string `json:"title"`