Refactorings
parent
9361631c6d
commit
d3f7ac7197
181
bot.go
181
bot.go
|
@ -214,6 +214,37 @@ func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error)
|
||||||
return message, nil
|
return message, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) {
|
||||||
|
params, err := config.Params()
|
||||||
|
if err != nil {
|
||||||
|
return Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
file := config.GetFile()
|
||||||
|
|
||||||
|
resp, err := bot.UploadFile(method, params, config.Name(), file)
|
||||||
|
if err != nil {
|
||||||
|
return Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var message Message
|
||||||
|
json.Unmarshal(resp.Result, &message)
|
||||||
|
|
||||||
|
if bot.Debug {
|
||||||
|
log.Printf("%s resp: %+v\n", method, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return message, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *BotAPI) sendFile(method string, config Fileable) (Message, error) {
|
||||||
|
if config.UseExistingFile() {
|
||||||
|
return bot.sendExisting(method, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bot.uploadAndSend(method, config)
|
||||||
|
}
|
||||||
|
|
||||||
// SendMessage sends a Message to a chat.
|
// SendMessage sends a Message to a chat.
|
||||||
//
|
//
|
||||||
// Requires ChatID and Text.
|
// Requires ChatID and Text.
|
||||||
|
@ -274,30 +305,7 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
|
||||||
// Caption, ReplyToMessageID, and ReplyMarkup are optional.
|
// Caption, ReplyToMessageID, and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("SendPhoto", config)
|
||||||
return bot.sendExisting("SendPhoto", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("SendPhoto", params, "photo", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("SendPhoto resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendAudio sends or uploads an audio clip to a chat.
|
// SendAudio sends or uploads an audio clip to a chat.
|
||||||
|
@ -312,30 +320,7 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
|
||||||
// ReplyToMessageID and ReplyMarkup are optional.
|
// ReplyToMessageID and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("sendAudio", config)
|
||||||
return bot.sendExisting("sendAudio", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("sendAudio", params, "audio", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("sendAudio resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendDocument sends or uploads a document to a chat.
|
// SendDocument sends or uploads a document to a chat.
|
||||||
|
@ -344,30 +329,7 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
|
||||||
// ReplyToMessageID and ReplyMarkup are optional.
|
// ReplyToMessageID and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("sendDocument", config)
|
||||||
return bot.sendExisting("sendDocument", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("sendDocument", params, "document", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("sendDocument resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendVoice sends or uploads a playable voice to a chat.
|
// SendVoice sends or uploads a playable voice to a chat.
|
||||||
|
@ -378,30 +340,7 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
|
||||||
// ReplyToMessageID and ReplyMarkup are optional.
|
// ReplyToMessageID and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("sendVoice", config)
|
||||||
return bot.sendExisting("sendVoice", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("SendVoice", params, "voice", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("SendVoice resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSticker sends or uploads a sticker to a chat.
|
// SendSticker sends or uploads a sticker to a chat.
|
||||||
|
@ -410,30 +349,7 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
|
||||||
// ReplyToMessageID and ReplyMarkup are optional.
|
// ReplyToMessageID and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("sendSticker", config)
|
||||||
return bot.sendExisting("sendSticker", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("sendSticker", params, "sticker", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("sendSticker resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendVideo sends or uploads a video to a chat.
|
// SendVideo sends or uploads a video to a chat.
|
||||||
|
@ -442,30 +358,7 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
|
||||||
// ReplyToMessageID and ReplyMarkup are optional.
|
// ReplyToMessageID and ReplyMarkup are optional.
|
||||||
// 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.UseExisting {
|
return bot.sendFile("sendVideo", config)
|
||||||
return bot.sendExisting("sendVideo", config)
|
|
||||||
}
|
|
||||||
|
|
||||||
params, err := config.Params()
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := config.GetFile()
|
|
||||||
|
|
||||||
resp, err := bot.UploadFile("sendVideo", params, "video", file)
|
|
||||||
if err != nil {
|
|
||||||
return Message{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var message Message
|
|
||||||
json.Unmarshal(resp.Result, &message)
|
|
||||||
|
|
||||||
if bot.Debug {
|
|
||||||
log.Printf("sendVideo resp: %+v\n", message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendChatAction sets a current action in a chat.
|
// SendChatAction sets a current action in a chat.
|
||||||
|
|
30
configs.go
30
configs.go
|
@ -45,7 +45,9 @@ type Chattable interface {
|
||||||
type Fileable interface {
|
type Fileable interface {
|
||||||
Chattable
|
Chattable
|
||||||
Params() (map[string]string, error)
|
Params() (map[string]string, error)
|
||||||
|
Name() string
|
||||||
GetFile() interface{}
|
GetFile() interface{}
|
||||||
|
UseExistingFile() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base struct for all chat event(Message, Photo and so on)
|
// Base struct for all chat event(Message, Photo and so on)
|
||||||
|
@ -95,6 +97,10 @@ func (file BaseFile) GetFile() interface{} {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (file BaseFile) UseExistingFile() bool {
|
||||||
|
return file.UseExisting
|
||||||
|
}
|
||||||
|
|
||||||
// MessageConfig contains information about a SendMessage request.
|
// MessageConfig contains information about a SendMessage request.
|
||||||
type MessageConfig struct {
|
type MessageConfig struct {
|
||||||
BaseChat
|
BaseChat
|
||||||
|
@ -199,6 +205,10 @@ func (config PhotoConfig) Values() (url.Values, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config PhotoConfig) Name() string {
|
||||||
|
return "photo"
|
||||||
|
}
|
||||||
|
|
||||||
// AudioConfig contains information about a SendAudio request.
|
// AudioConfig contains information about a SendAudio request.
|
||||||
type AudioConfig struct {
|
type AudioConfig struct {
|
||||||
BaseFile
|
BaseFile
|
||||||
|
@ -264,6 +274,10 @@ func (config AudioConfig) Params() (map[string]string, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config AudioConfig) Name() string {
|
||||||
|
return "audio"
|
||||||
|
}
|
||||||
|
|
||||||
// DocumentConfig contains information about a SendDocument request.
|
// DocumentConfig contains information about a SendDocument request.
|
||||||
type DocumentConfig struct {
|
type DocumentConfig struct {
|
||||||
BaseFile
|
BaseFile
|
||||||
|
@ -308,6 +322,10 @@ func (config DocumentConfig) Params() (map[string]string, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config DocumentConfig) Name() string {
|
||||||
|
return "document"
|
||||||
|
}
|
||||||
|
|
||||||
// StickerConfig contains information about a SendSticker request.
|
// StickerConfig contains information about a SendSticker request.
|
||||||
type StickerConfig struct {
|
type StickerConfig struct {
|
||||||
BaseFile
|
BaseFile
|
||||||
|
@ -352,6 +370,10 @@ func (config StickerConfig) Params() (map[string]string, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config StickerConfig) Name() string {
|
||||||
|
return "sticker"
|
||||||
|
}
|
||||||
|
|
||||||
// VideoConfig contains information about a SendVideo request.
|
// VideoConfig contains information about a SendVideo request.
|
||||||
type VideoConfig struct {
|
type VideoConfig struct {
|
||||||
BaseFile
|
BaseFile
|
||||||
|
@ -404,6 +426,10 @@ func (config VideoConfig) Params() (map[string]string, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config VideoConfig) Name() string {
|
||||||
|
return "viceo"
|
||||||
|
}
|
||||||
|
|
||||||
// VoiceConfig contains information about a SendVoice request.
|
// VoiceConfig contains information about a SendVoice request.
|
||||||
type VoiceConfig struct {
|
type VoiceConfig struct {
|
||||||
BaseFile
|
BaseFile
|
||||||
|
@ -455,6 +481,10 @@ func (config VoiceConfig) Params() (map[string]string, error) {
|
||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config VoiceConfig) Name() string {
|
||||||
|
return "voice"
|
||||||
|
}
|
||||||
|
|
||||||
// LocationConfig contains information about a SendLocation request.
|
// LocationConfig contains information about a SendLocation request.
|
||||||
type LocationConfig struct {
|
type LocationConfig struct {
|
||||||
BaseChat
|
BaseChat
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
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},
|
||||||
Text: text,
|
Text: text,
|
||||||
DisableWebPagePreview: false,
|
DisableWebPagePreview: false,
|
||||||
ReplyToMessageID: 0,
|
ReplyToMessageID: 0,
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ func NewMessage(chatID int, text string) MessageConfig {
|
||||||
// and messageID is the ID of the original message.
|
// and messageID is the ID of the original message.
|
||||||
func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
|
func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
|
||||||
return ForwardConfig{
|
return ForwardConfig{
|
||||||
BaseChat: BaseChat{ChatID: chatID},
|
BaseChat: BaseChat{ChatID: chatID},
|
||||||
FromChatID: fromChatID,
|
FromChatID: fromChatID,
|
||||||
MessageID: messageID,
|
MessageID: messageID,
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,7 @@ 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},
|
||||||
Latitude: latitude,
|
Latitude: latitude,
|
||||||
Longitude: longitude,
|
Longitude: longitude,
|
||||||
ReplyToMessageID: 0,
|
ReplyToMessageID: 0,
|
||||||
|
@ -175,7 +175,7 @@ func NewLocation(chatID int, latitude float64, longitude float64) LocationConfig
|
||||||
func NewChatAction(chatID int, action string) ChatActionConfig {
|
func NewChatAction(chatID int, action string) ChatActionConfig {
|
||||||
return ChatActionConfig{
|
return ChatActionConfig{
|
||||||
BaseChat: BaseChat{ChatID: chatID},
|
BaseChat: BaseChat{ChatID: chatID},
|
||||||
Action: action,
|
Action: action,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue