Package list golang-github-nlopes-slack / 05742bd
Add support for inclusive and unread_count_display to channels.History, groups.History and im.History Norberto Lopes 6 years ago
6 changed file(s) with 80 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
3131
3232 // Channel contains information about the channel
3333 type Channel struct {
34 Id string `json:"id"`
35 Name string `json:"name"`
36 IsChannel bool `json:"is_channel"`
37 Created JSONTime `json:"created"`
38 Creator string `json:"creator"`
39 IsArchived bool `json:"is_archived"`
40 IsGeneral bool `json:"is_general"`
41 IsGroup bool `json:"is_group"`
42 IsStarred bool `json:"is_starred"`
43 Members []string `json:"members"`
44 Topic ChannelTopic `json:"topic"`
45 Purpose ChannelPurpose `json:"purpose"`
46 IsMember bool `json:"is_member"`
47 LastRead string `json:"last_read,omitempty"`
48 Latest Message `json:"latest,omitempty"`
49 UnreadCount int `json:"unread_count,omitempty"`
50 NumMembers int `json:"num_members,omitempty"`
34 Id string `json:"id"`
35 Name string `json:"name"`
36 IsChannel bool `json:"is_channel"`
37 Created JSONTime `json:"created"`
38 Creator string `json:"creator"`
39 IsArchived bool `json:"is_archived"`
40 IsGeneral bool `json:"is_general"`
41 IsGroup bool `json:"is_group"`
42 IsStarred bool `json:"is_starred"`
43 Members []string `json:"members"`
44 Topic ChannelTopic `json:"topic"`
45 Purpose ChannelPurpose `json:"purpose"`
46 IsMember bool `json:"is_member"`
47 LastRead string `json:"last_read,omitempty"`
48 Latest Message `json:"latest,omitempty"`
49 UnreadCount int `json:"unread_count,omitempty"`
50 NumMembers int `json:"num_members,omitempty"`
51 UnreadCountDisplay int `json:"unread_count_display,omitempty"`
5152 }
5253
5354 func channelRequest(path string, values url.Values, debug bool) (*channelResponseFull, error) {
116117 if params.Count != DEFAULT_HISTORY_COUNT {
117118 values.Add("count", strconv.Itoa(params.Count))
118119 }
120 if params.Inclusive != DEFAULT_HISTORY_INCLUSIVE {
121 if params.Inclusive {
122 values.Add("inclusive", "1")
123 } else {
124 values.Add("inclusive", "0")
125 }
126 }
119127 response, err := channelRequest("channels.history", values, api.debug)
120128 if err != nil {
121129 return nil, err
122130 }
123 channelHistory := response.History
124 return &channelHistory, nil
131 return &response.History, nil
125132 }
126133
127134 // GetChannelInfo retrieves the given channel
115115 }
116116
117117 // PostMessage sends a message to a channel
118 // Message is automatically escaped according to https://api.slack.com/docs/formatting
118119 func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (channel string, timestamp string, err error) {
119120 values := url.Values{
120121 "token": {api.config.token},
2121
2222 // IM contains information related to the Direct Message channel
2323 type IM struct {
24 Id string `json:"id"`
25 IsIM bool `json:"is_im"`
26 UserId string `json:"user"`
27 Created JSONTime `json:"created"`
28 IsUserDeleted bool `json:"is_user_deleted"`
24 Id string `json:"id"`
25 IsIM bool `json:"is_im"`
26 UserId string `json:"user"`
27 Created JSONTime `json:"created"`
28 IsUserDeleted bool `json:"is_user_deleted"`
29 UnreadCount int `json:"unread_count,omitempty"`
30 UnreadCountDisplay int `json:"unread_count_display,omitempty"`
2931 }
3032
3133 func imRequest(path string, values url.Values, debug bool) (*imResponseFull, error) {
9698 if params.Count != DEFAULT_HISTORY_COUNT {
9799 values.Add("count", strconv.Itoa(params.Count))
98100 }
101 if params.Inclusive != DEFAULT_HISTORY_INCLUSIVE {
102 if params.Inclusive {
103 values.Add("inclusive", "1")
104 } else {
105 values.Add("inclusive", "0")
106 }
107 }
99108 response, err := imRequest("im.history", values, api.debug)
100109 if err != nil {
101110 return nil, err
3535 // Ignore hello
3636 case *slack.MessageEvent:
3737 a := msg.Data.(*slack.MessageEvent)
38 fmt.Printf("%v\n", a)
38 fmt.Printf("Message: %v\n", a)
3939 case *slack.PresenceChangeEvent:
4040 a := msg.Data.(*slack.PresenceChangeEvent)
41 fmt.Printf("%v\n", a)
41 fmt.Printf("Presence Change: %v\n", a)
4242 case slack.LatencyReport:
4343 a := msg.Data.(slack.LatencyReport)
4444 fmt.Printf("Current latency: %v\n", a.Value)
4545 default:
46 fmt.Printf("Unknown: %v\n", msg.Data)
46 fmt.Printf("Unexpected: %v\n", msg.Data)
4747 }
4848 }
4949 }
77
88 // Group contains all the information for a group
99 type Group struct {
10 Id string `json:"id"`
11 Name string `json:"name"`
12 IsGroup bool `json:"is_group"`
13 Created JSONTime `json:"created"`
14 Creator string `json:"creator"`
15 IsArchived bool `json:"is_archived"`
16 IsGeneral bool `json:"is_general"`
17 IsOpen bool `json:"is_open,omitempty"`
18 Members []string `json:"members"`
19 Topic ChannelTopic `json:"topic"`
20 Purpose ChannelPurpose `json:"purpose"`
21 LastRead string `json:"last_read,omitempty"`
22 Latest Message `json:"latest,omitempty"`
23 UnreadCount int `json:"unread_count,omitempty"`
24 NumMembers int `json:"num_members,omitempty"`
10 Id string `json:"id"`
11 Name string `json:"name"`
12 IsGroup bool `json:"is_group"`
13 Created JSONTime `json:"created"`
14 Creator string `json:"creator"`
15 IsArchived bool `json:"is_archived"`
16 IsGeneral bool `json:"is_general"`
17 IsOpen bool `json:"is_open,omitempty"`
18 Members []string `json:"members"`
19 Topic ChannelTopic `json:"topic"`
20 Purpose ChannelPurpose `json:"purpose"`
21 LastRead string `json:"last_read,omitempty"`
22 Latest Message `json:"latest,omitempty"`
23 UnreadCount int `json:"unread_count,omitempty"`
24 NumMembers int `json:"num_members,omitempty"`
25 UnreadCountDisplay int `json:"unread_count_display,omitempty"`
2526
2627 // XXX: does this exist for a group too?
2728 IsMember bool `json:"is_member"`
139140 if params.Count != DEFAULT_HISTORY_COUNT {
140141 values.Add("count", strconv.Itoa(params.Count))
141142 }
143 if params.Inclusive != DEFAULT_HISTORY_INCLUSIVE {
144 if params.Inclusive {
145 values.Add("inclusive", "1")
146 } else {
147 values.Add("inclusive", "0")
148 }
149 }
142150 response, err := groupRequest("groups.history", values, api.debug)
143151 if err != nil {
144152 return nil, err
145153 }
146 groupHistory := response.History
147 return &groupHistory, nil
154 return &response.History, nil
148155 }
149156
150157 // InviteUserToGroup invites a user to a group
00 package slack
11
22 const (
3 DEFAULT_HISTORY_LATEST = ""
4 DEFAULT_HISTORY_OLDEST = "0"
5 DEFAULT_HISTORY_COUNT = 100
3 DEFAULT_HISTORY_LATEST = ""
4 DEFAULT_HISTORY_OLDEST = "0"
5 DEFAULT_HISTORY_COUNT = 100
6 DEFAULT_HISTORY_INCLUSIVE = false
67 )
78
89 // HistoryParameters contains all the necessary information to help in the retrieval of history for Channels/Groups/DMs
910 type HistoryParameters struct {
10 Latest string
11 Oldest string
12 Count int
11 Latest string
12 Oldest string
13 Count int
14 Inclusive bool
1315 }
1416
1517 // History contains message history information needed to navigate a Channel / Group / DM history
2224 // NewHistoryParameters provides an instance of HistoryParameters with all the sane default values set
2325 func NewHistoryParameters() HistoryParameters {
2426 return HistoryParameters{
25 Latest: DEFAULT_HISTORY_LATEST,
26 Oldest: DEFAULT_HISTORY_OLDEST,
27 Count: DEFAULT_HISTORY_COUNT,
27 Latest: DEFAULT_HISTORY_LATEST,
28 Oldest: DEFAULT_HISTORY_OLDEST,
29 Count: DEFAULT_HISTORY_COUNT,
30 Inclusive: DEFAULT_HISTORY_INCLUSIVE,
2831 }
2932 }