Refactoring and DRYing

bot-api-6.1
Gleb Sinyavsky 2015-11-20 14:06:51 +03:00
parent 13a8bd025c
commit 57a07c0c22
2 changed files with 235 additions and 179 deletions

205
bot.go
View File

@ -183,27 +183,10 @@ func (bot *BotAPI) Send(c Chattable) error {
// Requires ChatID and Text. // Requires ChatID and Text.
// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional. // DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional.
func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) { func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" {
v.Add("chat_id", config.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
v.Add("text", config.Text)
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 Message{}, err
}
v.Add("reply_markup", string(data)) if err != nil {
return Message{}, err
} }
resp, err := bot.MakeRequest("SendMessage", v) resp, err := bot.MakeRequest("SendMessage", v)
@ -226,18 +209,7 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
// //
// Requires ChatID (destination), FromChatID (source), and MessageID. // Requires ChatID (destination), FromChatID (source), and MessageID.
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
v := url.Values{} v, _:= config.Values()
if config.ChannelUsername != "" {
v.Add("chat_id", config.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
if config.FromChannelUsername != "" {
v.Add("chat_id", config.FromChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.FromChatID))
}
v.Add("message_id", strconv.Itoa(config.MessageID))
resp, err := bot.MakeRequest("forwardMessage", v) resp, err := bot.MakeRequest("forwardMessage", v)
if err != nil { if err != nil {
@ -262,26 +234,10 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) { func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
if config.UseExistingPhoto { if config.UseExistingPhoto {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" {
v.Add("chat_id", config.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data)) if err != nil {
return Message{}, err
} }
resp, err := bot.MakeRequest("SendPhoto", v) resp, err := bot.MakeRequest("SendPhoto", v)
@ -356,32 +312,9 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) { func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
if config.UseExistingAudio { if config.UseExistingAudio {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
}
if config.Performer != "" {
v.Add("performer", config.Performer)
}
if config.Title != "" {
v.Add("title", config.Title)
} }
resp, err := bot.MakeRequest("sendAudio", v) resp, err := bot.MakeRequest("sendAudio", v)
@ -457,23 +390,9 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) { func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
if config.UseExistingDocument { if config.UseExistingDocument {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
} }
resp, err := bot.MakeRequest("sendDocument", v) resp, err := bot.MakeRequest("sendDocument", v)
@ -542,26 +461,9 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
if config.UseExistingVoice { if config.UseExistingVoice {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
} }
resp, err := bot.MakeRequest("sendVoice", v) resp, err := bot.MakeRequest("sendVoice", v)
@ -631,23 +533,9 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) { func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
if config.UseExistingSticker { if config.UseExistingSticker {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
} }
resp, err := bot.MakeRequest("sendSticker", v) resp, err := bot.MakeRequest("sendSticker", v)
@ -714,29 +602,9 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
// File should be either a string, FileBytes, or FileReader. // File should be either a string, FileBytes, or FileReader.
func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) { func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
if config.UseExistingVideo { if config.UseExistingVideo {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
} }
resp, err := bot.MakeRequest("sendVideo", v) resp, err := bot.MakeRequest("sendVideo", v)
@ -801,24 +669,9 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
// Requires ChatID, Latitude, and Longitude. // Requires ChatID, Latitude, and Longitude.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) { func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
v := url.Values{} v, err := config.Values()
if config.ChannelUsername != "" { if err != nil {
v.Add("chat_id", config.ChannelUsername) return Message{}, err
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
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 Message{}, err
}
v.Add("reply_markup", string(data))
} }
resp, err := bot.MakeRequest("sendLocation", v) resp, err := bot.MakeRequest("sendLocation", v)
@ -841,13 +694,7 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
// //
// Requires ChatID and a valid Action (see Chat constants). // Requires ChatID and a valid Action (see Chat constants).
func (bot *BotAPI) SendChatAction(config ChatActionConfig) error { func (bot *BotAPI) SendChatAction(config ChatActionConfig) error {
v := url.Values{} v, _ := config.Values()
if config.ChannelUsername != "" {
v.Add("chat_id", config.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.ChatID))
}
v.Add("action", config.Action)
_, err := bot.MakeRequest("sendChatAction", v) _, err := bot.MakeRequest("sendChatAction", v)
if err != nil { if err != nil {

View File

@ -3,6 +3,8 @@ package tgbotapi
import ( import (
"io" "io"
"net/url" "net/url"
"strconv"
"encoding/json"
) )
// Telegram constants // Telegram constants
@ -42,6 +44,16 @@ type Chattable struct {
ChannelUsername string ChannelUsername string
} }
func (chattable *Chattable) Values() (url.Values, error){
v := url.Values{}
if chattable.ChannelUsername != "" {
v.Add("chat_id", chattable.ChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(chattable.ChatID))
}
return v, nil
}
// MessageConfig contains information about a SendMessage request. // MessageConfig contains information about a SendMessage request.
type MessageConfig struct { type MessageConfig struct {
Chattable Chattable
@ -52,6 +64,29 @@ type MessageConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
} }
func (config *MessageConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("text", config.Text)
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
}
// ForwardConfig contains information about a ForwardMessage request. // ForwardConfig contains information about a ForwardMessage request.
type ForwardConfig struct { type ForwardConfig struct {
Chattable Chattable
@ -60,6 +95,19 @@ type ForwardConfig struct {
MessageID int MessageID int
} }
func (config *ForwardConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
if config.FromChannelUsername != "" {
v.Add("chat_id", config.FromChannelUsername)
} else {
v.Add("chat_id", strconv.Itoa(config.FromChatID))
}
v.Add("message_id", strconv.Itoa(config.MessageID))
return v, nil
}
// PhotoConfig contains information about a SendPhoto request. // PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct { type PhotoConfig struct {
Chattable Chattable
@ -72,6 +120,28 @@ type PhotoConfig struct {
FileID string FileID string
} }
func (config *PhotoConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
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
}
// AudioConfig contains information about a SendAudio request. // AudioConfig contains information about a SendAudio request.
type AudioConfig struct { type AudioConfig struct {
Chattable Chattable
@ -86,6 +156,34 @@ type AudioConfig struct {
FileID string FileID string
} }
func (config *AudioConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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)
}
if config.Title != "" {
v.Add("title", config.Title)
}
return v, nil
}
// DocumentConfig contains information about a SendDocument request. // DocumentConfig contains information about a SendDocument request.
type DocumentConfig struct { type DocumentConfig struct {
Chattable Chattable
@ -97,6 +195,25 @@ type DocumentConfig struct {
FileID string FileID string
} }
func (config *DocumentConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
// StickerConfig contains information about a SendSticker request. // StickerConfig contains information about a SendSticker request.
type StickerConfig struct { type StickerConfig struct {
Chattable Chattable
@ -108,6 +225,25 @@ type StickerConfig struct {
FileID string FileID string
} }
func (config *StickerConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
// VideoConfig contains information about a SendVideo request. // VideoConfig contains information about a SendVideo request.
type VideoConfig struct { type VideoConfig struct {
Chattable Chattable
@ -121,6 +257,31 @@ type VideoConfig struct {
FileID string FileID string
} }
func (config *VideoConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
// VoiceConfig contains information about a SendVoice request. // VoiceConfig contains information about a SendVoice request.
type VoiceConfig struct { type VoiceConfig struct {
Chattable Chattable
@ -133,6 +294,28 @@ type VoiceConfig struct {
FileID string FileID string
} }
func (config *VoiceConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
// LocationConfig contains information about a SendLocation request. // LocationConfig contains information about a SendLocation request.
type LocationConfig struct { type LocationConfig struct {
Chattable Chattable
@ -142,12 +325,38 @@ type LocationConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
} }
func (config *LocationConfig) Values() (url.Values, error) {
v, _ := config.Chattable.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
}
// ChatActionConfig contains information about a SendChatAction request. // ChatActionConfig contains information about a SendChatAction request.
type ChatActionConfig struct { type ChatActionConfig struct {
Chattable Chattable
Action string Action string
} }
func (config *ChatActionConfig) Values() (url.Values, error) {
v, _ := config.Chattable.Values()
v.Add("action", config.Action)
return v, nil
}
// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request. // UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
type UserProfilePhotosConfig struct { type UserProfilePhotosConfig struct {
UserID int UserID int