Merge pull request #16 from jqs7/master update to latest telegram bot api
Syfaro syfaro@foxpaw.in
Mon, 17 Aug 2015 21:48:30 -0500
3 files changed,
152 insertions(+),
7 deletions(-)
M
helpers.go
→
helpers.go
@@ -153,6 +153,31 @@ FileID: fileID,
} } +// NewVoiceUpload creates a new voice uploader. +// This requires a file on the local filesystem to upload to Telegram. +// Perhaps set a ChatAction of ChatRecordVideo or ChatUploadVideo while processing. +// +// chatID is where to send it, filename is the path to the file. +func NewVoiceUpload(chatID int, filename string) VoiceConfig { + return VoiceConfig{ + ChatID: chatID, + UseExistingVoice: false, + FilePath: filename, + } +} + +// NewVoiceShare shares an existing voice. +// You may use this to reshare an existing voice without reuploading it. +// +// chatID is where to send it, fileID is the ID of the video already uploaded. +func NewVoiceShare(chatID int, fileID string) VoiceConfig { + return VoiceConfig{ + ChatID: chatID, + UseExistingVoice: true, + FileID: fileID, + } +} + // NewLocation shares your location. // Perhaps set a ChatAction of ChatFindLocation while processing. //
M
methods.go
→
methods.go
@@ -4,13 +4,14 @@ import (
"encoding/json" "errors" "fmt" - "github.com/technoweenie/multipartstreamer" "io/ioutil" "log" "net/http" "net/url" "os" "strconv" + + "github.com/technoweenie/multipartstreamer" ) // Telegram constants@@ -68,6 +69,8 @@ // AudioConfig contains information about a SendAudio request.
type AudioConfig struct { ChatID int Duration int + Performer string + Title string ReplyToMessageID int ReplyMarkup interface{} UseExistingAudio bool@@ -103,6 +106,17 @@ Caption string
ReplyToMessageID int ReplyMarkup interface{} UseExistingVideo bool + FilePath string + FileID string +} + +// VoiceConfig contains information about a SendVoice request. +type VoiceConfig struct { + ChatID int + Duration int + ReplyToMessageID int + ReplyMarkup interface{} + UseExistingVoice bool FilePath string FileID string }@@ -374,7 +388,11 @@ return message, nil
} // SendAudio sends or uploads an audio clip to a chat. -// If using a file, the file must be encoded as an .ogg with OPUS. +// If using a file, the file must be in the .mp3 format. +// +// when the fields title and performer are both empty +// and the mime-type of the file to be sent is not audio/mpeg, +// the file must be in an .ogg file encoded with OPUS. // You may use the tgutils.EncodeAudio func to assist you with this, if needed. // // Requires ChatID and FileID OR FilePath.@@ -397,6 +415,12 @@ 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)@@ -421,6 +445,9 @@ 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 {@@ -428,6 +455,12 @@ return Message{}, err
} params["reply_markup"] = string(data) + } + if config.Performer != "" { + params["performer"] = config.Performer + } + if config.Title != "" { + params["title"] = config.Title } resp, err := bot.UploadFile("sendAudio", params, "audio", config.FilePath)@@ -507,6 +540,81 @@ 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. +// 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. +// +// Requires ChatID and FileID OR FilePath. +// ReplyToMessageID and ReplyMarkup are optional. +func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) { + if config.UseExistingVoice { + v := url.Values{} + 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) + if err != nil { + return Message{}, err + } + + var message Message + json.Unmarshal(resp.Result, &message) + + if bot.Debug { + log.Printf("SendVoice req : %+v\n", v) + log.Printf("SendVoice resp: %+v\n", message) + } + + return message, nil + } + + params := make(map[string]string) + + 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) + } + + resp, err := bot.UploadFile("SendVoice", params, "voice", config.FilePath) + 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
M
types.go
→
types.go
@@ -73,6 +73,7 @@ Document Document `json:"document"`
Photo []PhotoSize `json:"photo"` Sticker Sticker `json:"sticker"` Video Video `json:"video"` + Voice Voice `json:"voice"` Caption string `json:"caption"` Contact Contact `json:"contact"` Location Location `json:"location"`@@ -102,12 +103,15 @@ Height int `json:"height"`
FileSize int `json:"file_size"` } -// Audio contains information about audio, including ID and Duration. +// Audio contains information about audio, +// including ID, Duration, Performer and Title. type Audio struct { - FileID string `json:"file_id"` - Duration int `json:"duration"` - MimeType string `json:"mime_type"` - FileSize int `json:"file_size"` + FileID string `json:"file_id"` + Duration int `json:"duration"` + Performer string `json:"performer"` + Title string `json:"title"` + MimeType string `json:"mime_type"` + FileSize int `json:"file_size"` } // Document contains information about a document, including ID and a Thumbnail.@@ -137,6 +141,14 @@ Duration int `json:"duration"`
Thumbnail PhotoSize `json:"thumb"` MimeType string `json:"mime_type"` FileSize int `json:"file_size"` +} + +// Voice contains information about a voice, including ID and duration. +type Voice struct { + FileID string `json:"file_id"` + Duration int `json:"duration"` + MimeType string `json:"mime_type"` + FileSize int `json:"file_size"` } // Contact contains information about a contact, such as PhoneNumber and UserId.