Refactoring
parent
54b9c7e14b
commit
9361631c6d
327
bot.go
327
bot.go
|
@ -188,7 +188,7 @@ func (bot *BotAPI) GetMe() (User, error) {
|
|||
return user, nil
|
||||
}
|
||||
|
||||
func (bot *BotAPI) Send(c Chattable) error {
|
||||
func (bot *BotAPI) Send(c BaseChat) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -199,13 +199,27 @@ func (bot *BotAPI) DebugLog(context string, v url.Values, message interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) {
|
||||
v, err := config.Values()
|
||||
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest(method, v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// SendMessage sends a Message to a chat.
|
||||
//
|
||||
// Requires ChatID and Text.
|
||||
// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional.
|
||||
func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
|
||||
v, err := config.Values()
|
||||
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
@ -223,7 +237,10 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
|
|||
//
|
||||
// Requires ChatID (destination), FromChatID (source), and MessageID.
|
||||
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
|
||||
v, _ := config.Values()
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("forwardMessage", v)
|
||||
if err != nil {
|
||||
|
@ -233,54 +250,40 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
|
|||
return message, nil
|
||||
}
|
||||
|
||||
// SendLocation sends a location to a chat.
|
||||
//
|
||||
// Requires ChatID, Latitude, and Longitude.
|
||||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendLocation", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// SendPhoto sends or uploads a photo to a chat.
|
||||
//
|
||||
// Requires ChatID and FileID OR File.
|
||||
// Caption, ReplyToMessageID, and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
|
||||
if config.UseExistingPhoto {
|
||||
v, err := config.Values()
|
||||
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("SendPhoto", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("SendPhoto", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("SendPhoto", params, "photo", file)
|
||||
if err != nil {
|
||||
|
@ -309,54 +312,16 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
|
|||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
|
||||
if config.UseExistingAudio {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendAudio", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("sendAudio", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
}
|
||||
if config.Performer != "" {
|
||||
params["performer"] = config.Performer
|
||||
}
|
||||
if config.Title != "" {
|
||||
params["title"] = config.Title
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("sendAudio", params, "audio", file)
|
||||
if err != nil {
|
||||
|
@ -379,45 +344,16 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
|
|||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
|
||||
if config.UseExistingDocument {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendDocument", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("sendDocument", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("sendDocument", params, "document", file)
|
||||
if err != nil {
|
||||
|
@ -442,48 +378,16 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
|
|||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
|
||||
if config.UseExistingVoice {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendVoice", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("sendVoice", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("SendVoice", params, "voice", file)
|
||||
if err != nil {
|
||||
|
@ -506,45 +410,16 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
|
|||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
|
||||
if config.UseExistingSticker {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendSticker", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("sendSticker", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("sendSticker", params, "sticker", file)
|
||||
if err != nil {
|
||||
|
@ -567,45 +442,16 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
|
|||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
// File should be either a string, FileBytes, or FileReader.
|
||||
func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
|
||||
if config.UseExistingVideo {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendVideo", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
if config.UseExisting {
|
||||
return bot.sendExisting("sendVideo", config)
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
if config.ChannelUsername != "" {
|
||||
params["chat_id"] = config.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(config.ChatID)
|
||||
}
|
||||
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 Message{}, err
|
||||
}
|
||||
|
||||
params["reply_markup"] = string(data)
|
||||
params, err := config.Params()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
var file interface{}
|
||||
if config.FilePath == "" {
|
||||
file = config.File
|
||||
} else {
|
||||
file = config.FilePath
|
||||
}
|
||||
file := config.GetFile()
|
||||
|
||||
resp, err := bot.UploadFile("sendVideo", params, "video", file)
|
||||
if err != nil {
|
||||
|
@ -622,31 +468,16 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
|
|||
return message, nil
|
||||
}
|
||||
|
||||
// SendLocation sends a location to a chat.
|
||||
//
|
||||
// Requires ChatID, Latitude, and Longitude.
|
||||
// ReplyToMessageID and ReplyMarkup are optional.
|
||||
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
message, err := bot.MakeMessageRequest("sendLocation", v)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// SendChatAction sets a current action in a chat.
|
||||
//
|
||||
// Requires ChatID and a valid Action (see Chat constants).
|
||||
func (bot *BotAPI) SendChatAction(config ChatActionConfig) error {
|
||||
v, _ := config.Values()
|
||||
v, err := config.Values()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := bot.MakeRequest("sendChatAction", v)
|
||||
_, err = bot.MakeRequest("sendChatAction", v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
268
configs.go
268
configs.go
|
@ -38,31 +38,66 @@ const (
|
|||
ModeMarkdown = "Markdown"
|
||||
)
|
||||
|
||||
type Chattable interface {
|
||||
Values() (url.Values, error)
|
||||
}
|
||||
|
||||
type Fileable interface {
|
||||
Chattable
|
||||
Params() (map[string]string, error)
|
||||
GetFile() interface{}
|
||||
}
|
||||
|
||||
// Base struct for all chat event(Message, Photo and so on)
|
||||
type Chattable struct {
|
||||
type BaseChat struct {
|
||||
ChatID int
|
||||
ChannelUsername string
|
||||
}
|
||||
|
||||
type Fileable struct {
|
||||
FilePath string
|
||||
File interface{}
|
||||
FileID string
|
||||
}
|
||||
|
||||
func (chattable *Chattable) Values() (url.Values, error) {
|
||||
func (chat *BaseChat) Values() (url.Values, error) {
|
||||
v := url.Values{}
|
||||
if chattable.ChannelUsername != "" {
|
||||
v.Add("chat_id", chattable.ChannelUsername)
|
||||
if chat.ChannelUsername != "" {
|
||||
v.Add("chat_id", chat.ChannelUsername)
|
||||
} else {
|
||||
v.Add("chat_id", strconv.Itoa(chattable.ChatID))
|
||||
v.Add("chat_id", strconv.Itoa(chat.ChatID))
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
type BaseFile struct {
|
||||
BaseChat
|
||||
FilePath string
|
||||
File interface{}
|
||||
FileID string
|
||||
UseExisting bool
|
||||
}
|
||||
|
||||
func (file BaseFile) Params() (map[string]string, error) {
|
||||
params := make(map[string]string)
|
||||
|
||||
if file.ChannelUsername != "" {
|
||||
params["chat_id"] = file.ChannelUsername
|
||||
} else {
|
||||
params["chat_id"] = strconv.Itoa(file.ChatID)
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (file BaseFile) GetFile() interface{} {
|
||||
var result interface{}
|
||||
if file.FilePath == "" {
|
||||
result = file.File
|
||||
} else {
|
||||
result = file.FilePath
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// MessageConfig contains information about a SendMessage request.
|
||||
type MessageConfig struct {
|
||||
Chattable
|
||||
BaseChat
|
||||
Text string
|
||||
ParseMode string
|
||||
DisableWebPagePreview bool
|
||||
|
@ -70,8 +105,8 @@ type MessageConfig struct {
|
|||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
func (config *MessageConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config MessageConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
v.Add("text", config.Text)
|
||||
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
|
||||
if config.ParseMode != "" {
|
||||
|
@ -94,14 +129,14 @@ func (config *MessageConfig) Values() (url.Values, error) {
|
|||
|
||||
// ForwardConfig contains information about a ForwardMessage request.
|
||||
type ForwardConfig struct {
|
||||
Chattable
|
||||
BaseChat
|
||||
FromChatID int
|
||||
FromChannelUsername string
|
||||
MessageID int
|
||||
}
|
||||
|
||||
func (config *ForwardConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config ForwardConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
if config.FromChannelUsername != "" {
|
||||
v.Add("chat_id", config.FromChannelUsername)
|
||||
|
@ -115,16 +150,35 @@ func (config *ForwardConfig) Values() (url.Values, error) {
|
|||
|
||||
// PhotoConfig contains information about a SendPhoto request.
|
||||
type PhotoConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
BaseFile
|
||||
Caption string
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingPhoto bool
|
||||
}
|
||||
|
||||
func (config *PhotoConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config PhotoConfig) Params() (map[string]string, error) {
|
||||
params, _ := config.BaseFile.Params()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (config PhotoConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
v.Add("photo", config.FileID)
|
||||
if config.Caption != "" {
|
||||
|
@ -147,18 +201,16 @@ func (config *PhotoConfig) Values() (url.Values, error) {
|
|||
|
||||
// AudioConfig contains information about a SendAudio request.
|
||||
type AudioConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
BaseFile
|
||||
Duration int
|
||||
Performer string
|
||||
Title string
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingAudio bool
|
||||
}
|
||||
|
||||
func (config *AudioConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config AudioConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
v.Add("audio", config.FileID)
|
||||
if config.ReplyToMessageID != 0 {
|
||||
|
@ -185,17 +237,42 @@ func (config *AudioConfig) Values() (url.Values, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
// DocumentConfig contains information about a SendDocument request.
|
||||
type DocumentConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingDocument bool
|
||||
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
|
||||
}
|
||||
if config.Title != "" {
|
||||
params["title"] = config.Title
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (config *DocumentConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
// 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 {
|
||||
|
@ -213,17 +290,33 @@ func (config *DocumentConfig) Values() (url.Values, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
// StickerConfig contains information about a SendSticker request.
|
||||
type StickerConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingSticker bool
|
||||
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
|
||||
}
|
||||
|
||||
func (config *StickerConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
// 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 {
|
||||
|
@ -241,19 +334,35 @@ func (config *StickerConfig) Values() (url.Values, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// VideoConfig contains information about a SendVideo request.
|
||||
type VideoConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
BaseFile
|
||||
Duration int
|
||||
Caption string
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingVideo bool
|
||||
}
|
||||
|
||||
func (config *VideoConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config VideoConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
v.Add("video", config.FileID)
|
||||
if config.ReplyToMessageID != 0 {
|
||||
|
@ -277,18 +386,34 @@ func (config *VideoConfig) Values() (url.Values, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// VoiceConfig contains information about a SendVoice request.
|
||||
type VoiceConfig struct {
|
||||
Chattable
|
||||
Fileable
|
||||
BaseFile
|
||||
Duration int
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
UseExistingVoice bool
|
||||
}
|
||||
|
||||
func (config *VoiceConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config VoiceConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
v.Add("voice", config.FileID)
|
||||
if config.ReplyToMessageID != 0 {
|
||||
|
@ -309,17 +434,38 @@ func (config *VoiceConfig) Values() (url.Values, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// LocationConfig contains information about a SendLocation request.
|
||||
type LocationConfig struct {
|
||||
Chattable
|
||||
BaseChat
|
||||
Latitude float64
|
||||
Longitude float64
|
||||
ReplyToMessageID int
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
func (config *LocationConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config LocationConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
|
||||
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
|
||||
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
|
||||
|
@ -341,12 +487,12 @@ func (config *LocationConfig) Values() (url.Values, error) {
|
|||
|
||||
// ChatActionConfig contains information about a SendChatAction request.
|
||||
type ChatActionConfig struct {
|
||||
Chattable
|
||||
BaseChat
|
||||
Action string
|
||||
}
|
||||
|
||||
func (config *ChatActionConfig) Values() (url.Values, error) {
|
||||
v, _ := config.Chattable.Values()
|
||||
func (config ChatActionConfig) Values() (url.Values, error) {
|
||||
v, _ := config.BaseChat.Values()
|
||||
v.Add("action", config.Action)
|
||||
return v, nil
|
||||
}
|
||||
|
|
56
helpers.go
56
helpers.go
|
@ -10,7 +10,7 @@ import (
|
|||
// chatID is where to send it, text is the message text.
|
||||
func NewMessage(chatID int, text string) MessageConfig {
|
||||
return MessageConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
BaseChat: BaseChat{ChatID: chatID},
|
||||
Text: text,
|
||||
DisableWebPagePreview: false,
|
||||
ReplyToMessageID: 0,
|
||||
|
@ -23,7 +23,7 @@ func NewMessage(chatID int, text string) MessageConfig {
|
|||
// and messageID is the ID of the original message.
|
||||
func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
|
||||
return ForwardConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
BaseChat: BaseChat{ChatID: chatID},
|
||||
FromChatID: fromChatID,
|
||||
MessageID: messageID,
|
||||
}
|
||||
|
@ -36,9 +36,7 @@ func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
|
||||
return PhotoConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingPhoto: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,9 +46,7 @@ func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
|
|||
// chatID is where to send it, fileID is the ID of the file already uploaded.
|
||||
func NewPhotoShare(chatID int, fileID string) PhotoConfig {
|
||||
return PhotoConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
UseExistingPhoto: true,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,9 +57,7 @@ func NewPhotoShare(chatID int, fileID string) PhotoConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewAudioUpload(chatID int, file interface{}) AudioConfig {
|
||||
return AudioConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingAudio: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,9 +67,7 @@ func NewAudioUpload(chatID int, file interface{}) AudioConfig {
|
|||
// chatID is where to send it, fileID is the ID of the audio already uploaded.
|
||||
func NewAudioShare(chatID int, fileID string) AudioConfig {
|
||||
return AudioConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
UseExistingAudio: true,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,9 +78,7 @@ func NewAudioShare(chatID int, fileID string) AudioConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
|
||||
return DocumentConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingDocument: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,9 +88,7 @@ func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
|
|||
// chatID is where to send it, fileID is the ID of the document already uploaded.
|
||||
func NewDocumentShare(chatID int, fileID string) DocumentConfig {
|
||||
return DocumentConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
UseExistingDocument: true,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,9 +98,7 @@ func NewDocumentShare(chatID int, fileID string) DocumentConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewStickerUpload(chatID int, file interface{}) StickerConfig {
|
||||
return StickerConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingSticker: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,9 +108,7 @@ func NewStickerUpload(chatID int, file interface{}) StickerConfig {
|
|||
// chatID is where to send it, fileID is the ID of the sticker already uploaded.
|
||||
func NewStickerShare(chatID int, fileID string) StickerConfig {
|
||||
return StickerConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
UseExistingSticker: true,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,9 +119,7 @@ func NewStickerShare(chatID int, fileID string) StickerConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewVideoUpload(chatID int, file interface{}) VideoConfig {
|
||||
return VideoConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingVideo: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,9 +129,7 @@ func NewVideoUpload(chatID int, file interface{}) VideoConfig {
|
|||
// chatID is where to send it, fileID is the ID of the video already uploaded.
|
||||
func NewVideoShare(chatID int, fileID string) VideoConfig {
|
||||
return VideoConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
UseExistingVideo: true,
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,9 +140,7 @@ func NewVideoShare(chatID int, fileID string) VideoConfig {
|
|||
// chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
|
||||
func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
|
||||
return VoiceConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{File: file},
|
||||
UseExistingVoice: false,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, File: file, UseExisting: false},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,9 +150,7 @@ func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
|
|||
// chatID is where to send it, fileID is the ID of the video already uploaded.
|
||||
func NewVoiceShare(chatID int, fileID string) VoiceConfig {
|
||||
return VoiceConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
Fileable: Fileable{FileID: fileID},
|
||||
UseExistingVoice: true,
|
||||
BaseFile: BaseFile{BaseChat: BaseChat{ChatID: chatID}, FileID: fileID, UseExisting: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -184,7 +160,7 @@ func NewVoiceShare(chatID int, fileID string) VoiceConfig {
|
|||
// chatID is where to send it, latitude and longitude are coordinates.
|
||||
func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig {
|
||||
return LocationConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
BaseChat: BaseChat{ChatID: chatID},
|
||||
Latitude: latitude,
|
||||
Longitude: longitude,
|
||||
ReplyToMessageID: 0,
|
||||
|
@ -198,7 +174,7 @@ func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig
|
|||
// chatID is where to send it, action should be set via CHAT constants.
|
||||
func NewChatAction(chatID int, action string) ChatActionConfig {
|
||||
return ChatActionConfig{
|
||||
Chattable: Chattable{ChatID: chatID},
|
||||
BaseChat: BaseChat{ChatID: chatID},
|
||||
Action: action,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue