Basic mod client
This commit is contained in:
parent
6c18bfb941
commit
5ffb054dfa
7 changed files with 148 additions and 22 deletions
3
Makefile
3
Makefile
|
@ -2,6 +2,7 @@ all: clessy-broker
|
|||
|
||||
clessy-broker:
|
||||
go build -o clessy-broker ./broker
|
||||
go build -o clessy-mods ./mods
|
||||
|
||||
clean:
|
||||
rm -f clessy-broker
|
||||
rm -f clessy-broker clessy-mods
|
||||
|
|
|
@ -1,26 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func webhook(rw http.ResponseWriter, req *http.Request) {
|
||||
log.Println("Received request! Details follow:")
|
||||
// Read entire request and broadcast to everyone
|
||||
data, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer req.Body.Close()
|
||||
/*
|
||||
var update tg.APIUpdate
|
||||
|
||||
err := json.NewDecoder(req.Body).Decode(&update)
|
||||
if err != nil {
|
||||
log.Println("ERR: Not JSON!")
|
||||
return
|
||||
}
|
||||
|
||||
jenc, _ := json.Marshal(update)
|
||||
log.Println(jenc)
|
||||
*/
|
||||
io.Copy(os.Stdout, req.Body)
|
||||
broadcast(string(data))
|
||||
}
|
||||
|
|
21
mods/main.go
Normal file
21
mods/main.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"../tg"
|
||||
)
|
||||
|
||||
func dispatch(broker *tg.Broker, update tg.APIMessage) {
|
||||
metafora(broker, update)
|
||||
}
|
||||
|
||||
func main() {
|
||||
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
||||
flag.Parse()
|
||||
|
||||
err := tg.CreateBrokerClient(*brokerAddr, dispatch)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
35
mods/metafora.go
Normal file
35
mods/metafora.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"../tg"
|
||||
)
|
||||
|
||||
var actions []string = []string{
|
||||
"Puppami", "Degustami", "Lucidami", "Manipolami", "Disidratami", "Irritami", "Martorizzami",
|
||||
"Lustrami", "Osannami", "Sorseggiami", "Assaporami", "Apostrofami", "Spremimi", "Dimenami",
|
||||
"Agitami", "Stimolami", "Suonami", "Strimpellami", "Stuzzicami", "Spintonami", "Sguinzagliami",
|
||||
"Modellami", "Sgrullami", "Cavalcami", "Perquotimi", "Misurami", "Sventolami", "Induriscimi",
|
||||
"Accordami", "Debuggami",
|
||||
}
|
||||
|
||||
var objects []string = []string{
|
||||
"il birillo", "il bastone", "l'ombrello", "il malloppo", "il manico", "il manganello",
|
||||
"il ferro", "la mazza", "l'archibugio", "il timone", "l'arpione", "il flauto", "la reliquia",
|
||||
"il fiorino", "lo scettro", "il campanile", "la proboscide", "il pino", "il maritozzo", "il perno",
|
||||
"il tubo da 100", "la verga", "l'idrante", "il pendolo", "la torre di Pisa", "la lancia",
|
||||
"il cilindro", "il lampione", "il joystick", "il Wiimote", "il PSMove", "l'albero maestro",
|
||||
"il trenino",
|
||||
}
|
||||
|
||||
func metafora(broker *tg.Broker, update tg.APIMessage) {
|
||||
if update.Text != nil {
|
||||
if *(update.Text) == "/metafora" {
|
||||
n := rand.Intn(len(actions))
|
||||
m := rand.Intn(len(objects))
|
||||
broker.SendTextMessage(update.Chat, actions[n]+" "+objects[m])
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
42
tg/broker.go
Normal file
42
tg/broker.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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()
|
||||
}
|
||||
|
||||
func (b *Broker) SendTextMessage(chat *APIChat, text string) {
|
||||
cmd := ClientCommand{
|
||||
Type: CmdSendTextMessage,
|
||||
TextMessageData: &ClientTextMessageData{
|
||||
Text: text,
|
||||
},
|
||||
}
|
||||
// Encode command and send to broker
|
||||
err := json.NewEncoder(b.Socket).Encode(&cmd)
|
||||
if err != nil {
|
||||
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
|
||||
}
|
||||
fmt.Fprintf(b.Socket, "\n")
|
||||
}
|
36
tg/client.go
Normal file
36
tg/client.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package tg
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
type UpdateHandler func(broker *Broker, message APIMessage)
|
||||
|
||||
func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
|
||||
broker, err := ConnectToBroker(brokerAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer broker.Close()
|
||||
|
||||
in := bufio.NewReader(broker.Socket)
|
||||
for {
|
||||
bytes, _, err := in.ReadLine()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
var update APIUpdate
|
||||
err = json.Unmarshal(bytes, &update)
|
||||
if err != nil {
|
||||
log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error())
|
||||
}
|
||||
|
||||
// Dispatch to UpdateHandler
|
||||
updateFn(broker, update.Message)
|
||||
}
|
||||
return io.EOF
|
||||
}
|
|
@ -3,14 +3,14 @@ package tg
|
|||
type ClientCommandType uint
|
||||
|
||||
const (
|
||||
CmdSendMessage ClientCommandType = 1
|
||||
CmdSendTextMessage ClientCommandType = 1
|
||||
)
|
||||
|
||||
type ClientCommandMessageData struct {
|
||||
MessageText string
|
||||
type ClientTextMessageData struct {
|
||||
Text string
|
||||
}
|
||||
|
||||
type ClientCommand struct {
|
||||
Type ClientCommandType
|
||||
MessageData *ClientCommandMessageData
|
||||
Type ClientCommandType
|
||||
TextMessageData *ClientTextMessageData
|
||||
}
|
||||
|
|
Reference in a new issue