all repos — telegram-bot-api @ 4610c561c6467a0884374769b29f907ec7e8d961

Golang bindings for the Telegram Bot API

Drying
Gleb Sinyavsky zhulik.gleb@gmail.com
Fri, 20 Nov 2015 14:19:37 +0300
commit

4610c561c6467a0884374769b29f907ec7e8d961

parent

57a07c0c22adbfe3e8543582b35b73cbb6a80865

2 files changed, 25 insertions(+), 40 deletions(-)

jump to
M bot.gobot.go

@@ -81,6 +81,17 @@

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. // // Requires the parameter to hold the file not be in the params.

@@ -189,14 +200,12 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("SendMessage", v) + message, err := bot.MakeMessageRequest("SendMessage", v) + if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("SendMessage req : %+v\n", v) log.Printf("SendMessage resp: %+v\n", message)

@@ -209,16 +218,13 @@ // ForwardMessage forwards a message from one chat to another.

// // Requires ChatID (destination), FromChatID (source), and MessageID. 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 { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("forwardMessage req : %+v\n", v) log.Printf("forwardMessage resp: %+v\n", message)

@@ -240,13 +246,10 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("SendPhoto", v) + message, err := bot.MakeMessageRequest("SendPhoto", v) if err != nil { return Message{}, err } - - var message Message - json.Unmarshal(resp.Result, &message) if bot.Debug { log.Printf("SendPhoto req : %+v\n", v)

@@ -317,13 +320,10 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendAudio", v) + message, err := bot.MakeMessageRequest("sendAudio", v) if err != nil { return Message{}, err } - - var message Message - json.Unmarshal(resp.Result, &message) if bot.Debug { log.Printf("sendAudio req : %+v\n", v)

@@ -395,14 +395,11 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendDocument", v) + message, err := bot.MakeMessageRequest("sendDocument", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("sendDocument req : %+v\n", v) log.Printf("sendDocument resp: %+v\n", message)

@@ -466,14 +463,11 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendVoice", v) + message, err := bot.MakeMessageRequest("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)

@@ -538,13 +532,10 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendSticker", v) + message, err := bot.MakeMessageRequest("sendSticker", v) if err != nil { return Message{}, err } - - var message Message - json.Unmarshal(resp.Result, &message) if bot.Debug { log.Printf("sendSticker req : %+v\n", v)

@@ -607,14 +598,11 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendVideo", v) + message, err := bot.MakeMessageRequest("sendVideo", v) if err != nil { return Message{}, err } - var message Message - json.Unmarshal(resp.Result, &message) - if bot.Debug { log.Printf("sendVideo req : %+v\n", v) log.Printf("sendVideo resp: %+v\n", message)

@@ -674,13 +662,10 @@ if err != nil {

return Message{}, err } - resp, err := bot.MakeRequest("sendLocation", v) + message, err := bot.MakeMessageRequest("sendLocation", v) if err != nil { return Message{}, err } - - var message Message - json.Unmarshal(resp.Result, &message) if bot.Debug { log.Printf("sendLocation req : %+v\n", v)
M configs.goconfigs.go

@@ -1,10 +1,10 @@

package tgbotapi import ( + "encoding/json" "io" "net/url" "strconv" - "encoding/json" ) // Telegram constants

@@ -44,7 +44,7 @@ ChatID int

ChannelUsername string } -func (chattable *Chattable) Values() (url.Values, error){ +func (chattable *Chattable) Values() (url.Values, error) { v := url.Values{} if chattable.ChannelUsername != "" { v.Add("chat_id", chattable.ChannelUsername)

@@ -82,7 +82,6 @@ }

v.Add("reply_markup", string(data)) } - return v, nil }

@@ -330,6 +329,7 @@ v, _ := config.Chattable.Values()

v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64)) v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64)) + if config.ReplyToMessageID != 0 { v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID)) }