/
message.go
56 lines (45 loc) · 1.1 KB
/
message.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
package yammer
import (
"errors"
"fmt"
"strconv"
)
// Post a message.
func (c *Client) PostMessage(req MessageRequest) error {
u := "https://www.yammer.com/api/v1/messages.json"
params := make(map[string]string)
params["body"] = req.Body
if req.GroupId != 0 {
params["group_id"] = strconv.FormatInt(int64(req.GroupId), 10)
}
if req.ReplyTo != 0 {
params["reply_to_id"] = strconv.FormatInt(int64(req.ReplyTo), 10)
}
if req.DirectTo != 0 {
params["direct_to_id"] = strconv.FormatInt(int64(req.DirectTo), 10)
}
if req.Broadcast {
params["broadcast"] = "true"
}
res, err := c.oauth.Post(u, params)
if err != nil {
return err
}
if res.StatusCode != 201 {
return errors.New(res.Status)
}
res.Body.Close()
return nil
}
// Get messages for the given topic (by ID)
func (c *Client) MessagesByTopic(topic int) ([]Message, error) {
u := fmt.Sprintf("https://www.yammer.com/api/v1/messages/about_topic/%d.json", topic)
type rvT struct {
Messages []Message
}
rv := rvT{}
if err := decodeReq(c, u, &rv, map[string]string{}); err != nil {
return rv.Messages, err
}
return rv.Messages, nil
}