Test added, small improvements

bot-api-6.1
Gleb Sinyavsky 2015-11-20 18:30:50 +03:00
parent da026b435e
commit d0711736ec
4 changed files with 68 additions and 196 deletions

View File

@ -21,7 +21,6 @@ func TestNewBotAPI_notoken(t *testing.T) {
_, err := tgbotapi.NewBotAPI("") _, err := tgbotapi.NewBotAPI("")
if err == nil { if err == nil {
log.Println(err.Error())
t.Fail() t.Fail()
} }
} }
@ -50,15 +49,34 @@ func TestGetUpdates(t *testing.T) {
} }
} }
func TestSendMessage(t *testing.T) { func TestSendWithMessage(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil { if err != nil {
t.Fail() t.Fail()
} }
msg := tgbotapi.NewMessage(36529758, "A test message from the test library in telegram-bot-api") msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
bot.SendMessage(msg) _, err = bot.Send(msg)
if err != nil {
t.Fail()
}
}
func TestSendWithPhoto(t *testing.T) {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
t.Fail()
}
msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
_, err = bot.Send(msg)
if err != nil {
t.Fail()
}
} }
func ExampleNewBotAPI() { func ExampleNewBotAPI() {
@ -82,6 +100,6 @@ func ExampleNewBotAPI() {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID msg.ReplyToMessageID = update.Message.MessageID
bot.SendMessage(msg) bot.Send(msg)
} }
} }

View File

@ -53,8 +53,10 @@ type Fileable interface {
// Base struct for all chat event(Message, Photo and so on) // Base struct for all chat event(Message, Photo and so on)
type BaseChat struct { type BaseChat struct {
ChatID int ChatID int
ChannelUsername string ChannelUsername string
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (chat *BaseChat) Values() (url.Values, error) { func (chat *BaseChat) Values() (url.Values, error) {
@ -64,6 +66,20 @@ func (chat *BaseChat) Values() (url.Values, error) {
} else { } else {
v.Add("chat_id", strconv.Itoa(chat.ChatID)) v.Add("chat_id", strconv.Itoa(chat.ChatID))
} }
if chat.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(chat.ReplyToMessageID))
}
if chat.ReplyMarkup != nil {
data, err := json.Marshal(chat.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -84,6 +100,19 @@ func (file BaseFile) Params() (map[string]string, error) {
params["chat_id"] = strconv.Itoa(file.ChatID) params["chat_id"] = strconv.Itoa(file.ChatID)
} }
if file.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(file.ReplyToMessageID)
}
if file.ReplyMarkup != nil {
data, err := json.Marshal(file.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
@ -108,7 +137,6 @@ type MessageConfig struct {
Text string Text string
ParseMode string ParseMode string
DisableWebPagePreview bool DisableWebPagePreview bool
ReplyToMessageID int
ReplyMarkup interface{} ReplyMarkup interface{}
} }
@ -119,17 +147,6 @@ func (config MessageConfig) Values() (url.Values, error) {
if config.ParseMode != "" { if config.ParseMode != "" {
v.Add("parse_mode", config.ParseMode) v.Add("parse_mode", config.ParseMode)
} }
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -159,9 +176,7 @@ func (config ForwardConfig) Method() string {
// PhotoConfig contains information about a SendPhoto request. // PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct { type PhotoConfig struct {
BaseFile BaseFile
Caption string Caption string
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config PhotoConfig) Params() (map[string]string, error) { func (config PhotoConfig) Params() (map[string]string, error) {
@ -170,17 +185,6 @@ func (config PhotoConfig) Params() (map[string]string, error) {
if config.Caption != "" { if config.Caption != "" {
params["caption"] = config.Caption params["caption"] = config.Caption
} }
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
@ -192,18 +196,6 @@ func (config PhotoConfig) Values() (url.Values, error) {
if config.Caption != "" { if config.Caption != "" {
v.Add("caption", config.Caption) v.Add("caption", config.Caption)
} }
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -218,31 +210,19 @@ func (config PhotoConfig) Method() string {
// AudioConfig contains information about a SendAudio request. // AudioConfig contains information about a SendAudio request.
type AudioConfig struct { type AudioConfig struct {
BaseFile BaseFile
Duration int Duration int
Performer string Performer string
Title string Title string
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config AudioConfig) Values() (url.Values, error) { func (config AudioConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("audio", config.FileID) v.Add("audio", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 { if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration)) v.Add("duration", strconv.Itoa(config.Duration))
} }
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
if config.Performer != "" { if config.Performer != "" {
v.Add("performer", config.Performer) v.Add("performer", config.Performer)
} }
@ -256,20 +236,10 @@ func (config AudioConfig) Values() (url.Values, error) {
func (config AudioConfig) Params() (map[string]string, error) { func (config AudioConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 { if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration) params["duration"] = strconv.Itoa(config.Duration)
} }
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
if config.Performer != "" { if config.Performer != "" {
params["performer"] = config.Performer params["performer"] = config.Performer
} }
@ -291,25 +261,12 @@ func (config AudioConfig) Method() string {
// DocumentConfig contains information about a SendDocument request. // DocumentConfig contains information about a SendDocument request.
type DocumentConfig struct { type DocumentConfig struct {
BaseFile BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config DocumentConfig) Values() (url.Values, error) { func (config DocumentConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("document", config.FileID) v.Add("document", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -317,18 +274,6 @@ func (config DocumentConfig) Values() (url.Values, error) {
func (config DocumentConfig) Params() (map[string]string, error) { func (config DocumentConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
@ -343,25 +288,12 @@ func (config DocumentConfig) Method() string {
// StickerConfig contains information about a SendSticker request. // StickerConfig contains information about a SendSticker request.
type StickerConfig struct { type StickerConfig struct {
BaseFile BaseFile
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config StickerConfig) Values() (url.Values, error) { func (config StickerConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("sticker", config.FileID) v.Add("sticker", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -369,18 +301,6 @@ func (config StickerConfig) Values() (url.Values, error) {
func (config StickerConfig) Params() (map[string]string, error) { func (config StickerConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
@ -395,33 +315,20 @@ func (config StickerConfig) Method() string {
// VideoConfig contains information about a SendVideo request. // VideoConfig contains information about a SendVideo request.
type VideoConfig struct { type VideoConfig struct {
BaseFile BaseFile
Duration int Duration int
Caption string Caption string
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config VideoConfig) Values() (url.Values, error) { func (config VideoConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("video", config.FileID) v.Add("video", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 { if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration)) v.Add("duration", strconv.Itoa(config.Duration))
} }
if config.Caption != "" { if config.Caption != "" {
v.Add("caption", config.Caption) v.Add("caption", config.Caption)
} }
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -429,23 +336,11 @@ func (config VideoConfig) Values() (url.Values, error) {
func (config VideoConfig) Params() (map[string]string, error) { func (config VideoConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
func (config VideoConfig) Name() string { func (config VideoConfig) Name() string {
return "viceo" return "video"
} }
func (config VideoConfig) Method() string { func (config VideoConfig) Method() string {
@ -455,29 +350,16 @@ func (config VideoConfig) Method() string {
// VoiceConfig contains information about a SendVoice request. // VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct { type VoiceConfig struct {
BaseFile BaseFile
Duration int Duration int
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config VoiceConfig) Values() (url.Values, error) { func (config VoiceConfig) Values() (url.Values, error) {
v, _ := config.BaseChat.Values() v, _ := config.BaseChat.Values()
v.Add("voice", config.FileID) v.Add("voice", config.FileID)
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.Duration != 0 { if config.Duration != 0 {
v.Add("duration", strconv.Itoa(config.Duration)) v.Add("duration", strconv.Itoa(config.Duration))
} }
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }
@ -485,20 +367,9 @@ func (config VoiceConfig) Values() (url.Values, error) {
func (config VoiceConfig) Params() (map[string]string, error) { func (config VoiceConfig) Params() (map[string]string, error) {
params, _ := config.BaseFile.Params() params, _ := config.BaseFile.Params()
if config.ReplyToMessageID != 0 {
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
}
if config.Duration != 0 { if config.Duration != 0 {
params["duration"] = strconv.Itoa(config.Duration) params["duration"] = strconv.Itoa(config.Duration)
} }
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return params, err
}
params["reply_markup"] = string(data)
}
return params, nil return params, nil
} }
@ -514,10 +385,8 @@ func (config VoiceConfig) Method() string {
// LocationConfig contains information about a SendLocation request. // LocationConfig contains information about a SendLocation request.
type LocationConfig struct { type LocationConfig struct {
BaseChat BaseChat
Latitude float64 Latitude float64
Longitude float64 Longitude float64
ReplyToMessageID int
ReplyMarkup interface{}
} }
func (config LocationConfig) Values() (url.Values, error) { func (config LocationConfig) Values() (url.Values, error) {
@ -526,18 +395,6 @@ func (config LocationConfig) Values() (url.Values, error) {
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64)) v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64)) v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
}
if config.ReplyMarkup != nil {
data, err := json.Marshal(config.ReplyMarkup)
if err != nil {
return v, err
}
v.Add("reply_markup", string(data))
}
return v, nil return v, nil
} }

View File

@ -10,10 +10,9 @@ import (
// chatID is where to send it, text is the message text. // chatID is where to send it, text is the message text.
func NewMessage(chatID int, text string) MessageConfig { func NewMessage(chatID int, text string) MessageConfig {
return MessageConfig{ return MessageConfig{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID, ReplyToMessageID: 0},
Text: text, Text: text,
DisableWebPagePreview: false, DisableWebPagePreview: false,
ReplyToMessageID: 0,
} }
} }
@ -160,11 +159,9 @@ func NewVoiceShare(chatID int, fileID string) VoiceConfig {
// chatID is where to send it, latitude and longitude are coordinates. // chatID is where to send it, latitude and longitude are coordinates.
func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig { func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
return LocationConfig{ return LocationConfig{
BaseChat: BaseChat{ChatID: chatID}, BaseChat: BaseChat{ChatID: chatID, ReplyToMessageID: 0, ReplyMarkup: nil},
Latitude: latitude, Latitude: latitude,
Longitude: longitude, Longitude: longitude,
ReplyToMessageID: 0,
ReplyMarkup: nil,
} }
} }

BIN
tests/image.jpg 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB