This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/tg/broker.go

45 lines
797 B
Go
Raw Normal View History

2016-02-09 10:01:05 +00:00
package tg
import (
"encoding/json"
"fmt"
"log"
"net"
)
type Broker struct {
Socket net.Conn
}
func ConnectToBroker(brokerAddr string) (*Broker, error) {
sock, err := net.Dial("tcp", brokerAddr)
if err != nil {
return nil, err
}
broker := new(Broker)
broker.Socket = sock
return broker, nil
}
func (b *Broker) Close() {
b.Socket.Close()
}
2016-02-09 10:33:38 +00:00
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
2016-02-09 10:01:05 +00:00
cmd := ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{
2016-02-09 10:33:38 +00:00
Text: text,
ChatID: chat.ChatID,
ReplyID: original,
2016-02-09 10:01:05 +00:00
},
}
// Encode command and send to broker
2016-02-09 10:04:27 +00:00
data, err := json.Marshal(cmd)
2016-02-09 10:01:05 +00:00
if err != nil {
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
}
2016-02-09 10:24:00 +00:00
fmt.Fprintln(b.Socket, data)
2016-02-09 10:01:05 +00:00
}