-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo.go
77 lines (62 loc) · 1.49 KB
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"log"
"os"
"github.com/dysfn/wasb/wasb"
"golang.org/x/net/websocket"
)
const defaultConfigFile = "config.json"
var configFile string
type Echo struct {
conn *websocket.Conn
}
func (bot *Echo) ReceiveMessage() (*wasb.Msg, error) {
var m wasb.Msg
err := websocket.JSON.Receive(bot.conn, &m)
if err != nil {
return nil, err
}
return &m, nil
}
func (bot *Echo) IsValidMessage(m *wasb.Msg) bool {
return m.Type == "message" && m.Text != ""
}
func (bot *Echo) SendMessage(m *wasb.Msg) error {
err := websocket.JSON.Send(bot.conn, m)
return err
}
func (bot *Echo) TearDown() error {
log.Printf("Closing websocket connection...")
err := bot.conn.Close()
return err
}
func main() {
flags := flag.NewFlagSet("echo", flag.ExitOnError)
flags.StringVar(&configFile, "config", defaultConfigFile, "")
err := flags.Parse(os.Args[1:])
if err != nil {
log.Fatalln(err)
}
log.Printf("Loading config (filename: %s)...", configFile)
cfg, err := wasb.GetCfg(configFile)
if err != nil {
log.Fatalln(err)
}
log.Printf("Config loaded")
log.Printf("Starting RTM...")
respRTMStart, err := wasb.StartRTM(cfg.APIToken)
if err != nil {
log.Fatalln(err)
}
log.Printf("RTM started")
log.Printf("Establishing Websocket connection...")
conn, err := wasb.GetWSConn(respRTMStart.URL)
if err != nil {
log.Fatalln(err)
}
log.Printf("Websocket connection established")
log.Printf("Launching the bot...")
echoBot := &Echo{conn: conn}
wasb.Start(echoBot, cfg.Workers)
}