[tg] Refactor and little godoc fix

This commit is contained in:
Hamcha 2016-02-19 16:59:36 +00:00
parent 1f113c54bd
commit b393317e7e

View file

@ -35,29 +35,23 @@ func (b *Broker) Close() {
b.Socket.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. // A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) { func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
cmd := ClientCommand{ sendCmd(ClientCommand{
Type: CmdSendTextMessage, Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{ TextMessageData: &ClientTextMessageData{
Text: text, Text: text,
ChatID: chat.ChatID, ChatID: chat.ChatID,
ReplyID: original, 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. // A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original *int) { func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original *int) {
cmd := ClientCommand{ sendCmd(ClientCommand{
Type: CmdSendPhoto, Type: CmdSendPhoto,
PhotoData: &ClientPhotoData{ PhotoData: &ClientPhotoData{
ChatID: chat.ChatID, ChatID: chat.ChatID,
@ -65,13 +59,7 @@ func (b *Broker) SendPhoto(chat *APIChat, data []byte, caption *string, original
Caption: caption, Caption: caption,
ReplyID: original, 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. // GetFile sends a file retrieval request to the Broker.
@ -122,6 +110,14 @@ func (b *Broker) SpliceCallback(id int) BrokerCallback {
return b.Callbacks[id] 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() { func (b *Broker) resizeCbArray() {
var i int var i int
cut := false cut := false