-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
88 lines (78 loc) · 2.06 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
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
package spark
import (
"bytes"
"encoding/json"
"errors"
"net/url"
"time"
)
const MessagesUrl = "https://api.ciscospark.com/v1/messages"
type Message struct {
Id string `json:"id,omitempty"`
RoomId string `json:"roomId,omitempty"`
RoomType string `json:"roomType,omitempty"`
Text string `json:"text,omitempty"`
PersonId string `json:"personId,omitempty"`
PersonEmail string `json:"personEmail,omitempty"`
Markdown string `json:"markdown,omitempty"`
Html string `json:"html,omitempty"`
Files []string `json:"files,omitempty"`
//Created time.Time `json:"created,omitempty"`
Created time.Time `json:"-"`
}
type Messages struct {
Items []Message
}
// url.Values can be found here:
// https://developer.ciscospark.com/endpoint-messages-get.html
// example:
// uv := url.Values{}
// uv.Add("type", "group")
// s.ListRooms(&uv)
func (s *Spark) ListMessages(uv *url.Values) ([]Message, error) {
var m Messages
if uv == nil {
return m.Items, errors.New("Please include query params")
}
if (*uv).Get("roomId") == "" {
return m.Items, errors.New("Please include a roomId")
}
bytes, err := s.GetRequest(MessagesUrl, uv)
if err != nil {
return m.Items, err
}
//fmt.Println(string(bytes))
err = json.Unmarshal(bytes, &m)
return m.Items, err
}
// Get a message by the message ID
func (s *Spark) GetMessage(messageId string) (Message, error) {
var m Message
if messageId == "" {
return m, errors.New("Please include message ID")
}
u := MessagesUrl + "/" + messageId
bytes, err := s.GetRequest(u, &url.Values{})
if err != nil {
return m, err
}
err = json.Unmarshal(bytes, &m)
return m, err
}
func (s *Spark) CreateMessage(m Message) (Message, error) {
var rm Message
if m.RoomId == "" {
return rm, errors.New("A Valid RoomId was not included to Create the Message")
}
if m.RoomType == "" {
m.RoomType = "group"
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(m)
bytes, err := s.PostRequest(MessagesUrl, b)
if err != nil {
return rm, err
}
err = json.Unmarshal(bytes, &rm)
return rm, err
}