allow passing a []byte or io.Reader to upload functions, closes #20

bot-api-6.1
Syfaro 2015-09-07 11:20:43 -05:00
parent 766f7494e9
commit e48e6416e7
3 changed files with 144 additions and 49 deletions

4
bot.go
View File

@ -1,7 +1,9 @@
// Package tgbotapi has bindings for interacting with the Telegram Bot API. // Package tgbotapi has bindings for interacting with the Telegram Bot API.
package tgbotapi package tgbotapi
import "net/http" import (
"net/http"
)
// BotAPI has methods for interacting with all of Telegram's Bot API endpoints. // BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
type BotAPI struct { type BotAPI struct {

View File

@ -33,12 +33,12 @@ func NewForward(chatID int, fromChatID int, messageID int) ForwardConfig {
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// Perhaps set a ChatAction of ChatUploadPhoto while processing. // Perhaps set a ChatAction of ChatUploadPhoto while processing.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewPhotoUpload(chatID int, filename string) PhotoConfig { func NewPhotoUpload(chatID int, file interface{}) PhotoConfig {
return PhotoConfig{ return PhotoConfig{
ChatID: chatID, ChatID: chatID,
UseExistingPhoto: false, UseExistingPhoto: false,
FilePath: filename, File: file,
} }
} }
@ -58,12 +58,12 @@ func NewPhotoShare(chatID int, fileID string) PhotoConfig {
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// Perhaps set a ChatAction of ChatRecordAudio or ChatUploadAudio while processing. // Perhaps set a ChatAction of ChatRecordAudio or ChatUploadAudio while processing.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewAudioUpload(chatID int, filename string) AudioConfig { func NewAudioUpload(chatID int, file interface{}) AudioConfig {
return AudioConfig{ return AudioConfig{
ChatID: chatID, ChatID: chatID,
UseExistingAudio: false, UseExistingAudio: false,
FilePath: filename, File: file,
} }
} }
@ -83,12 +83,12 @@ func NewAudioShare(chatID int, fileID string) AudioConfig {
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// Perhaps set a ChatAction of ChatUploadDocument while processing. // Perhaps set a ChatAction of ChatUploadDocument while processing.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewDocumentUpload(chatID int, filename string) DocumentConfig { func NewDocumentUpload(chatID int, file interface{}) DocumentConfig {
return DocumentConfig{ return DocumentConfig{
ChatID: chatID, ChatID: chatID,
UseExistingDocument: false, UseExistingDocument: false,
FilePath: filename, File: file,
} }
} }
@ -107,12 +107,12 @@ func NewDocumentShare(chatID int, fileID string) DocumentConfig {
// NewStickerUpload creates a new sticker uploader. // NewStickerUpload creates a new sticker uploader.
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewStickerUpload(chatID int, filename string) StickerConfig { func NewStickerUpload(chatID int, file interface{}) StickerConfig {
return StickerConfig{ return StickerConfig{
ChatID: chatID, ChatID: chatID,
UseExistingSticker: false, UseExistingSticker: false,
FilePath: filename, File: file,
} }
} }
@ -132,12 +132,12 @@ func NewStickerShare(chatID int, fileID string) StickerConfig {
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing. // Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewVideoUpload(chatID int, filename string) VideoConfig { func NewVideoUpload(chatID int, file interface{}) VideoConfig {
return VideoConfig{ return VideoConfig{
ChatID: chatID, ChatID: chatID,
UseExistingVideo: false, UseExistingVideo: false,
FilePath: filename, File: file,
} }
} }
@ -157,12 +157,12 @@ func NewVideoShare(chatID int, fileID string) VideoConfig {
// This requires a file on the local filesystem to upload to Telegram. // This requires a file on the local filesystem to upload to Telegram.
// Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing. // Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing.
// //
// chatID is where to send it, filename is the path to the file. // chatID is where to send it, file is a string path to the file, or FileReader or FileBytes.
func NewVoiceUpload(chatID int, filename string) VoiceConfig { func NewVoiceUpload(chatID int, file interface{}) VoiceConfig {
return VoiceConfig{ return VoiceConfig{
ChatID: chatID, ChatID: chatID,
UseExistingVoice: false, UseExistingVoice: false,
FilePath: filename, File: file,
} }
} }

View File

@ -1,17 +1,18 @@
package tgbotapi package tgbotapi
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/technoweenie/multipartstreamer"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
"github.com/technoweenie/multipartstreamer"
) )
// Telegram constants // Telegram constants
@ -62,6 +63,7 @@ type PhotoConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingPhoto bool UseExistingPhoto bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -75,6 +77,7 @@ type AudioConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingAudio bool UseExistingAudio bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -85,6 +88,7 @@ type DocumentConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingDocument bool UseExistingDocument bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -95,6 +99,7 @@ type StickerConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingSticker bool UseExistingSticker bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -107,6 +112,7 @@ type VideoConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingVideo bool UseExistingVideo bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -118,6 +124,7 @@ type VoiceConfig struct {
ReplyMarkup interface{} ReplyMarkup interface{}
UseExistingVoice bool UseExistingVoice bool
FilePath string FilePath string
File interface{}
FileID string FileID string
} }
@ -156,6 +163,20 @@ type WebhookConfig struct {
URL *url.URL URL *url.URL
} }
// FileBytes contains information about a set of bytes to upload as a File.
type FileBytes struct {
Name string
Bytes []byte
}
// FileReader contains information about a reader to upload as a File.
// If Size is -1, it will read the entire Reader into memory to calculate a Size.
type FileReader struct {
Name string
Reader io.Reader
Size int64
}
// MakeRequest makes a request to a specific endpoint with our token. // MakeRequest makes a request to a specific endpoint with our token.
// All requests are POSTs because Telegram doesn't care, and it's easier. // All requests are POSTs because Telegram doesn't care, and it's easier.
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
@ -191,21 +212,45 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse,
// UploadFile makes a request to the API with a file. // UploadFile makes a request to the API with a file.
// //
// Requires the parameter to hold the file not be in the params. // Requires the parameter to hold the file not be in the params.
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, filename string) (APIResponse, error) { // File should be a string to a file path, a FileBytes struct, or a FileReader struct.
f, err := os.Open(filename) func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
if err != nil {
return APIResponse{}, err
}
defer f.Close()
fi, err := os.Stat(filename)
if err != nil {
return APIResponse{}, err
}
ms := multipartstreamer.New() ms := multipartstreamer.New()
ms.WriteFields(params) ms.WriteFields(params)
ms.WriteReader(fieldname, f.Name(), fi.Size(), f)
switch f := file.(type) {
case string:
fileHandle, err := os.Open(f)
if err != nil {
return APIResponse{}, err
}
defer fileHandle.Close()
fi, err := os.Stat(f)
if err != nil {
return APIResponse{}, err
}
ms.WriteReader(fieldname, fileHandle.Name(), fi.Size(), fileHandle)
case FileBytes:
buf := bytes.NewBuffer(f.Bytes)
ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
case FileReader:
if f.Size == -1 {
data, err := ioutil.ReadAll(f.Reader)
if err != nil {
return APIResponse{}, err
}
buf := bytes.NewBuffer(data)
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
break
}
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
default:
return APIResponse{}, errors.New("bad file type")
}
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil) req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
ms.SetupRequest(req) ms.SetupRequest(req)
@ -317,8 +362,9 @@ func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
// SendPhoto sends or uploads a photo to a chat. // SendPhoto sends or uploads a photo to a chat.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// Caption, ReplyToMessageID, and ReplyMarkup are optional. // Caption, ReplyToMessageID, and ReplyMarkup are optional.
// 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 := url.Values{}
@ -372,7 +418,14 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
resp, err := bot.UploadFile("SendPhoto", params, "photo", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("SendPhoto", params, "photo", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -390,13 +443,14 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
// SendAudio sends or uploads an audio clip to a chat. // SendAudio sends or uploads an audio clip to a chat.
// If using a file, the file must be in the .mp3 format. // If using a file, the file must be in the .mp3 format.
// //
// when the fields title and performer are both empty // When the fields title and performer are both empty and
// and the mime-type of the file to be sent is not audio/mpeg, // the mime-type of the file to be sent is not audio/mpeg,
// the file must be in an .ogg file encoded with OPUS. // the file must be an .ogg file encoded with OPUS.
// You may use the tgutils.EncodeAudio func to assist you with this, if needed. // You may use the tgutils.EncodeAudio func to assist you with this, if needed.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// 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 := url.Values{}
@ -463,7 +517,14 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
params["title"] = config.Title params["title"] = config.Title
} }
resp, err := bot.UploadFile("sendAudio", params, "audio", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendAudio", params, "audio", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -480,8 +541,9 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
// SendDocument sends or uploads a document to a chat. // SendDocument sends or uploads a document to a chat.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// 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 := url.Values{}
@ -530,7 +592,14 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
resp, err := bot.UploadFile("sendDocument", params, "document", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendDocument", params, "document", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -549,8 +618,9 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
// If using a file, the file must be encoded as an .ogg with OPUS. // If using a file, the file must be encoded as an .ogg with OPUS.
// You may use the tgutils.EncodeAudio func to assist you with this, if needed. // You may use the tgutils.EncodeAudio func to assist you with this, if needed.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// 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 := url.Values{}
@ -605,7 +675,14 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
resp, err := bot.UploadFile("SendVoice", params, "voice", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("SendVoice", params, "voice", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -622,8 +699,9 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
// SendSticker sends or uploads a sticker to a chat. // SendSticker sends or uploads a sticker to a chat.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// 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 := url.Values{}
@ -672,7 +750,14 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
resp, err := bot.UploadFile("sendSticker", params, "sticker", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendSticker", params, "sticker", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
@ -689,8 +774,9 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
// SendVideo sends or uploads a video to a chat. // SendVideo sends or uploads a video to a chat.
// //
// Requires ChatID and FileID OR FilePath. // Requires ChatID and FileID OR File.
// ReplyToMessageID and ReplyMarkup are optional. // ReplyToMessageID and ReplyMarkup are optional.
// 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 := url.Values{}
@ -745,7 +831,14 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
resp, err := bot.UploadFile("sendVideo", params, "video", config.FilePath) var file interface{}
if config.FilePath == "" {
file = config.File
} else {
file = config.FilePath
}
resp, err := bot.UploadFile("sendVideo", params, "video", file)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }