update to latest telegram bot api
Jqs7 7@jqs7.com
Tue, 18 Aug 2015 09:40:42 +0800
3 files changed,
149 insertions(+),
6 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
@@ -70,6 +70,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@@ -105,6 +107,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 }@@ -390,7 +403,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. // // Requires ChatID and FileID OR FilePath. // ReplyToMessageID and ReplyMarkup are optional.@@ -413,6 +430,12 @@ }
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) if err != nil {@@ -436,6 +459,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 {@@ -443,6 +469,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)@@ -522,6 +554,80 @@ 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. +// +// 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.