bot-api-6.1
Gleb Sinyavsky 2015-11-20 14:19:37 +03:00
parent 57a07c0c22
commit 4610c561c6
2 changed files with 25 additions and 40 deletions

59
bot.go
View File

@ -81,6 +81,17 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse,
return apiResp, nil return apiResp, nil
} }
func (bot *BotAPI) MakeMessageRequest(endpoint string, params url.Values) (Message, error) {
resp, err := bot.MakeRequest(endpoint, params)
if err != nil {
return Message{}, err
}
var message Message
json.Unmarshal(resp.Result, &message)
return message, nil
}
// 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.
@ -189,14 +200,12 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("SendMessage", v) message, err := bot.MakeMessageRequest("SendMessage", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("SendMessage req : %+v\n", v) log.Printf("SendMessage req : %+v\n", v)
log.Printf("SendMessage resp: %+v\n", message) log.Printf("SendMessage resp: %+v\n", message)
@ -209,16 +218,13 @@ func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
// //
// Requires ChatID (destination), FromChatID (source), and MessageID. // Requires ChatID (destination), FromChatID (source), and MessageID.
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) { func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
v, _:= config.Values() v, _ := config.Values()
resp, err := bot.MakeRequest("forwardMessage", v) message, err := bot.MakeMessageRequest("forwardMessage", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("forwardMessage req : %+v\n", v) log.Printf("forwardMessage req : %+v\n", v)
log.Printf("forwardMessage resp: %+v\n", message) log.Printf("forwardMessage resp: %+v\n", message)
@ -240,14 +246,11 @@ func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("SendPhoto", v) message, err := bot.MakeMessageRequest("SendPhoto", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("SendPhoto req : %+v\n", v) log.Printf("SendPhoto req : %+v\n", v)
log.Printf("SendPhoto resp: %+v\n", message) log.Printf("SendPhoto resp: %+v\n", message)
@ -317,14 +320,11 @@ func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendAudio", v) message, err := bot.MakeMessageRequest("sendAudio", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("sendAudio req : %+v\n", v) log.Printf("sendAudio req : %+v\n", v)
log.Printf("sendAudio resp: %+v\n", message) log.Printf("sendAudio resp: %+v\n", message)
@ -395,14 +395,11 @@ func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendDocument", v) message, err := bot.MakeMessageRequest("sendDocument", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("sendDocument req : %+v\n", v) log.Printf("sendDocument req : %+v\n", v)
log.Printf("sendDocument resp: %+v\n", message) log.Printf("sendDocument resp: %+v\n", message)
@ -466,14 +463,11 @@ func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendVoice", v) message, err := bot.MakeMessageRequest("sendVoice", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("SendVoice req : %+v\n", v) log.Printf("SendVoice req : %+v\n", v)
log.Printf("SendVoice resp: %+v\n", message) log.Printf("SendVoice resp: %+v\n", message)
@ -538,14 +532,11 @@ func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendSticker", v) message, err := bot.MakeMessageRequest("sendSticker", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("sendSticker req : %+v\n", v) log.Printf("sendSticker req : %+v\n", v)
log.Printf("sendSticker resp: %+v\n", message) log.Printf("sendSticker resp: %+v\n", message)
@ -607,14 +598,11 @@ func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendVideo", v) message, err := bot.MakeMessageRequest("sendVideo", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("sendVideo req : %+v\n", v) log.Printf("sendVideo req : %+v\n", v)
log.Printf("sendVideo resp: %+v\n", message) log.Printf("sendVideo resp: %+v\n", message)
@ -674,14 +662,11 @@ func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
return Message{}, err return Message{}, err
} }
resp, err := bot.MakeRequest("sendLocation", v) message, err := bot.MakeMessageRequest("sendLocation", v)
if err != nil { if err != nil {
return Message{}, err return Message{}, err
} }
var message Message
json.Unmarshal(resp.Result, &message)
if bot.Debug { if bot.Debug {
log.Printf("sendLocation req : %+v\n", v) log.Printf("sendLocation req : %+v\n", v)
log.Printf("sendLocation resp: %+v\n", message) log.Printf("sendLocation resp: %+v\n", message)

View File

@ -1,10 +1,10 @@
package tgbotapi package tgbotapi
import ( import (
"encoding/json"
"io" "io"
"net/url" "net/url"
"strconv" "strconv"
"encoding/json"
) )
// Telegram constants // Telegram constants
@ -44,7 +44,7 @@ type Chattable struct {
ChannelUsername string ChannelUsername string
} }
func (chattable *Chattable) Values() (url.Values, error){ func (chattable *Chattable) Values() (url.Values, error) {
v := url.Values{} v := url.Values{}
if chattable.ChannelUsername != "" { if chattable.ChannelUsername != "" {
v.Add("chat_id", chattable.ChannelUsername) v.Add("chat_id", chattable.ChannelUsername)
@ -83,7 +83,6 @@ func (config *MessageConfig) Values() (url.Values, error) {
v.Add("reply_markup", string(data)) v.Add("reply_markup", string(data))
} }
return v, nil return v, nil
} }
@ -330,6 +329,7 @@ func (config *LocationConfig) Values() (url.Values, error) {
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64)) v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64)) v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
if config.ReplyToMessageID != 0 { if config.ReplyToMessageID != 0 {
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
} }