From b393317e7e97f26b429b95c7f3f201656ec76266 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 19 Feb 2016 16:59:36 +0000 Subject: [PATCH] [tg] Refactor and little godoc fix --- tg/broker.go | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/tg/broker.go b/tg/broker.go index ddeb86f..a39aefa 100644 --- a/tg/broker.go +++ b/tg/broker.go @@ -35,29 +35,23 @@ func (b *Broker) Close() { b.Socket.Close() } -// SendTextMessage sends a HTML-styles text message to a specific chat. +// SendTextMessage sends a HTML-styles text message to a chat. // A reply_to message ID can be specified as optional parameter. func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) { - cmd := ClientCommand{ + sendCmd(ClientCommand{ Type: CmdSendTextMessage, TextMessageData: &ClientTextMessageData{ Text: text, ChatID: chat.ChatID, ReplyID: original, }, - } - // Encode command and send to broker - data, err := json.Marshal(cmd) - if err != nil { - log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error()) - } - fmt.Fprintln(b.Socket, string(data)) + }) } -// SendTextMessage sends a HTML-styles text message to a specific chat. +// SendPhoto sends a photo with an optional caption to a chat. // A reply_to message ID can be specified as optional parameter. func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original *int) { - cmd := ClientCommand{ + sendCmd(ClientCommand{ Type: CmdSendPhoto, PhotoData: &ClientPhotoData{ ChatID: chat.ChatID, @@ -65,13 +59,7 @@ func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original Caption: caption, ReplyID: original, }, - } - // Encode command and send to broker - data, err := json.Marshal(cmd) - if err != nil { - log.Printf("[SendPhoto] JSON Encode error: %s\n", err.Error()) - } - fmt.Fprintln(b.Socket, string(data)) + }) } // GetFile sends a file retrieval request to the Broker. @@ -122,6 +110,14 @@ func (b *Broker) SpliceCallback(id int) BrokerCallback { return b.Callbacks[id] } +func (b *Broker) sendCmd(cmd ClientCommand) { + data, err := json.Marshal(cmd) + if err != nil { + log.Printf("[sendCmd] JSON Encode error: %s\n", err.Error()) + } + fmt.Fprintln(b.Socket, string(data)) +} + func (b *Broker) resizeCbArray() { var i int cut := false