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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# gitter
Gitter API in Go
https://developer.gitter.im
#### Install
`go get github.com/sromku/go-gitter`
- [Initialize](#initialize)
- [Users](#users)
- [Rooms](#rooms)
- [Messages](#messages)
- [Stream](#stream)
- [Faye (Experimental)](#faye-experimental)
- [Debug](#debug)
- [App Engine](#app-engine)
##### Initialize
``` Go
api := gitter.New("YOUR_ACCESS_TOKEN")
```
##### Users
- Get current user
``` Go
user, err := api.GetUser()
```
##### Rooms
- Get all rooms
``` Go
rooms, err := api.GetRooms()
```
- Get room by id
``` Go
room, err := api.GetRoom("roomID")
```
- Get rooms of some user
``` Go
rooms, err := api.GetRooms("userID")
```
- Join room
``` Go
room, err := api.JoinRoom("roomID", "userID")
```
- Leave room
``` Go
room, err := api.LeaveRoom("roomID", "userID")
```
- Get room id
``` Go
id, err := api.GetRoomId("room/uri")
```
- Search gitter rooms
``` Go
rooms, err := api.SearchRooms("search/string")
```
##### Messages
- Get messages of room
``` Go
messages, err := api.GetMessages("roomID", nil)
```
- Get one message
``` Go
message, err := api.GetMessage("roomID", "messageID")
```
- Send message
``` Go
err := api.SendMessage("roomID", "free chat text")
```
##### Stream
Create stream to the room and start listening to incoming messages
``` Go
stream := api.Stream(room.Id)
go api.Listen(stream)
for {
event := <-stream.Event
switch ev := event.Data.(type) {
case *gitter.MessageReceived:
fmt.Println(ev.Message.From.Username + ": " + ev.Message.Text)
case *gitter.GitterConnectionClosed:
// connection was closed
}
}
```
Close stream connection
``` Go
stream.Close()
```
##### Faye (Experimental)
``` Go
faye := api.Faye(room.ID)
go faye.Listen()
for {
event := <-faye.Event
switch ev := event.Data.(type) {
case *gitter.MessageReceived:
fmt.Println(ev.Message.From.Username + ": " + ev.Message.Text)
case *gitter.GitterConnectionClosed: //this one is never called in Faye
// connection was closed
}
}
```
##### Debug
You can print the internal errors by enabling debug to true
``` Go
api.SetDebug(true, nil)
```
You can also define your own `io.Writer` in case you want to persist the logs somewhere.
For example keeping the errors on file
``` Go
logFile, err := os.Create("gitter.log")
api.SetDebug(true, logFile)
```
##### App Engine
Initialize app engine client and continue as usual
``` Go
c := appengine.NewContext(r)
client := urlfetch.Client(c)
api := gitter.New("YOUR_ACCESS_TOKEN")
api.SetClient(client)
```
[Documentation](https://godoc.org/github.com/sromku/go-gitter)
|