-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
103 lines (74 loc) · 2.47 KB
/
doc.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
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
/*
Package go-sechat aims to bridge the gap between the Stack Exchange chat network and Go applications by providing a simple interface with native Go primitives.
Basic Usage
In order to make requests, create a `Conn` object:
c, err := sechat.New("[email protected]", "passw0rd", 1)
if err != nil {
// handle error
}
defer c.Close()
Since authentication and connection are done asynchronously, waiting for them to complete is highly recommended:
if c.WaitForConnected() {
// do stuff
}
Interacting with Rooms
To join an additional room, use the `Join()` method:
// Join the Ask Ubuntu General Room (#201)
if err := c.Join(201); err != nil {
// handle error
}
To leave, use the (appropriately named) `Leave()` method:
if err := c.Leave(201); err != nil {
// handle error
}
To obtain a list of users in the room, use `UsersInRoom()`:
if users, err := c.UsersInRoom(201); err == nil {
for _, u := range users {
fmt.Printf("User: %s\n", u.Name)
}
}
To obtain a list of rooms that a user is in, use `User()`:
if user, err := c.User(1345); err == nil {
for _, r := range user.Rooms {
log.Printf("Room: %s\n", r.Name)
}
}
The `NewRoom()` method can be used to create new rooms:
r, err := c.NewRoom(
"Room Name",
"Room description",
"askubuntu.com", // room host
sechat.AccessReadWrite, // access
)
if err != nil {
// handle error
}
In the example above, `r` is an `int` containing the ID of the new room that was created.
Receiving Events
To receive events from the chat server, simply receive from the `Events` channel in `Conn`:
for e := range c.Events {
// e is of type *Event
}
Interacting with Messages
To post a message, simply invoke `Send()`:
if err := c.Send(201, "Testing go-sechat..."); err != nil {
// handle error
}
If the message is in response to an earlier event, the `Reply()` method is also available:
if err := c.Reply(e, "Reply to event"); err != nil {
// handle error
}
Uploading Images
To upload an image, prepare an `io.Reader` and pass it to `Image()`:
f, err := os.Open("/path/to/image.png")
if err != nil {
// handle error
}
defer f.Close()
u, err := c.Image(f)
if err != nil {
// handle error
}
In the example above, `u` is the URL of the newly uploaded image.
*/
package sechat