-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (53 loc) · 1.19 KB
/
main.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
package main
import (
"fmt"
"time"
"github.com/Gitter-Robot/config"
"github.com/Gitter-Robot/robot"
gitter "github.com/go-gitter"
)
const (
chatRoom = "MultiVAC/MTV-Dev"
)
func main() {
// Load config
cfg := config.LoadConfig()
roomMap := make(map[string]gitter.Room)
api := gitter.New(cfg.GitterToken)
user, err := api.GetUser()
if err != nil {
fmt.Println(err)
}
rooms, err := api.GetRooms()
for _, v := range rooms {
fmt.Println(v.Name)
roomMap[v.Name] = v
}
multiVAC := roomMap[chatRoom].ID
stream := api.Stream(multiVAC)
go api.Listen(stream)
// Roboat instance
rob := robot.NewRobot("qingyun")
for {
event := <-stream.Event
switch ev := event.Data.(type) {
case *gitter.MessageReceived:
// log
fmt.Println(ev.Message.From.Username + ": " + ev.Message.Text)
// Ask turing
res, err := rob.Ask(ev.Message.Text)
if err != nil {
fmt.Println("Ask error: ", err)
}
// Other people
if ev.Message.From.ID != user.ID {
api.SendMessage(multiVAC, res)
} else {
api.SendMessage(multiVAC, "我的主人说:"+ev.Message.Text)
}
time.Sleep(time.Second * 3)
case *gitter.GitterConnectionClosed:
// connection was closed
}
}
}