Test added, small improvements
@@ -21,7 +21,6 @@ func TestNewBotAPI_notoken(t *testing.T) {
_, err := tgbotapi.NewBotAPI("") if err == nil { - log.Println(err.Error()) t.Fail() } }@@ -50,15 +49,34 @@ t.Fail()
} } -func TestSendMessage(t *testing.T) { +func TestSendWithMessage(t *testing.T) { bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN")) if err != nil { t.Fail() } - msg := tgbotapi.NewMessage(36529758, "A test message from the test library in telegram-bot-api") - bot.SendMessage(msg) + msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api") + _, 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() {@@ -82,6 +100,6 @@
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) msg.ReplyToMessageID = update.Message.MessageID - bot.SendMessage(msg) + bot.Send(msg) } }
@@ -53,8 +53,10 @@ }
// Base struct for all chat event(Message, Photo and so on) type BaseChat struct { - ChatID int - ChannelUsername string + ChatID int + ChannelUsername string + ReplyToMessageID int + ReplyMarkup interface{} } func (chat *BaseChat) Values() (url.Values, error) {@@ -64,6 +66,20 @@ v.Add("chat_id", chat.ChannelUsername)
} else { 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 }@@ -84,6 +100,19 @@ } else {
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 }@@ -108,7 +137,6 @@ BaseChat
Text string ParseMode string DisableWebPagePreview bool - ReplyToMessageID int ReplyMarkup interface{} }@@ -119,17 +147,6 @@ v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
if 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 }@@ -159,9 +176,7 @@
// PhotoConfig contains information about a SendPhoto request. type PhotoConfig struct { BaseFile - Caption string - ReplyToMessageID int - ReplyMarkup interface{} + Caption string } func (config PhotoConfig) Params() (map[string]string, error) {@@ -170,17 +185,6 @@
if 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 }@@ -192,18 +196,6 @@ v.Add("photo", config.FileID)
if 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 }@@ -218,31 +210,19 @@
// AudioConfig contains information about a SendAudio request. type AudioConfig struct { BaseFile - Duration int - Performer string - Title string - ReplyToMessageID int - ReplyMarkup interface{} + Duration int + Performer string + Title string } func (config AudioConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() v.Add("audio", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } if config.Duration != 0 { 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 != "" { v.Add("performer", config.Performer) }@@ -256,20 +236,10 @@
func (config AudioConfig) Params() (map[string]string, error) { params, _ := config.BaseFile.Params() - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } if config.Duration != 0 { 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 != "" { params["performer"] = config.Performer }@@ -291,25 +261,12 @@
// DocumentConfig contains information about a SendDocument request. type DocumentConfig struct { BaseFile - ReplyToMessageID int - ReplyMarkup interface{} } func (config DocumentConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() 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 }@@ -317,18 +274,6 @@
func (config DocumentConfig) Params() (map[string]string, error) { 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 }@@ -343,25 +288,12 @@
// StickerConfig contains information about a SendSticker request. type StickerConfig struct { BaseFile - ReplyToMessageID int - ReplyMarkup interface{} } func (config StickerConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() 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 }@@ -369,18 +301,6 @@
func (config StickerConfig) Params() (map[string]string, error) { 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 }@@ -395,33 +315,20 @@
// VideoConfig contains information about a SendVideo request. type VideoConfig struct { BaseFile - Duration int - Caption string - ReplyToMessageID int - ReplyMarkup interface{} + Duration int + Caption string } func (config VideoConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() v.Add("video", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } if config.Duration != 0 { v.Add("duration", strconv.Itoa(config.Duration)) } if 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 }@@ -429,23 +336,11 @@
func (config VideoConfig) Params() (map[string]string, error) { 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 } func (config VideoConfig) Name() string { - return "viceo" + return "video" } func (config VideoConfig) Method() string {@@ -455,29 +350,16 @@
// VoiceConfig contains information about a SendVoice request. type VoiceConfig struct { BaseFile - Duration int - ReplyToMessageID int - ReplyMarkup interface{} + Duration int } func (config VoiceConfig) Values() (url.Values, error) { v, _ := config.BaseChat.Values() v.Add("voice", config.FileID) - if config.ReplyToMessageID != 0 { - v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) - } if config.Duration != 0 { 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 }@@ -485,19 +367,8 @@
func (config VoiceConfig) Params() (map[string]string, error) { params, _ := config.BaseFile.Params() - if config.ReplyToMessageID != 0 { - params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID) - } if config.Duration != 0 { 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@@ -514,10 +385,8 @@
// LocationConfig contains information about a SendLocation request. type LocationConfig struct { BaseChat - Latitude float64 - Longitude float64 - ReplyToMessageID int - ReplyMarkup interface{} + Latitude float64 + Longitude float64 } func (config LocationConfig) Values() (url.Values, error) {@@ -525,18 +394,6 @@ v, _ := config.BaseChat.Values()
v.Add("latitude", strconv.FormatFloat(config.Latitude, '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 }
@@ -10,10 +10,9 @@ //
// chatID is where to send it, text is the message text. func NewMessage(chatID int, text string) MessageConfig { return MessageConfig{ - BaseChat: BaseChat{ChatID: chatID}, + BaseChat: BaseChat{ChatID: chatID, ReplyToMessageID: 0}, Text: text, DisableWebPagePreview: false, - ReplyToMessageID: 0, } }@@ -160,11 +159,9 @@ //
// chatID is where to send it, latitude and longitude are coordinates. func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig { return LocationConfig{ - BaseChat: BaseChat{ChatID: chatID}, - Latitude: latitude, - Longitude: longitude, - ReplyToMessageID: 0, - ReplyMarkup: nil, + BaseChat: BaseChat{ChatID: chatID, ReplyToMessageID: 0, ReplyMarkup: nil}, + Latitude: latitude, + Longitude: longitude, } }