Module: Slack::API::Channels

Extended by:
Channels
Included in:
Channels
Defined in:
lib/slack-wrapper/api/channels.rb

Instance Method Summary collapse

Instance Method Details

#active_channelsObject



44
45
46
# File 'lib/slack-wrapper/api/channels.rb', line 44

def active_channels
  get_channels()
end

#all_channelsObject



41
42
43
# File 'lib/slack-wrapper/api/channels.rb', line 41

def all_channels
  get_channels(true)
end

#archive(id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/slack-wrapper/api/channels.rb', line 73

def archive(id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.archive')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#create(name, validate = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/slack-wrapper/api/channels.rb', line 55

def create(name, validate=false)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.create')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&name=#{name}&validate=#{validate}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['channel']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#get_channels(archived = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slack-wrapper/api/channels.rb', line 18

def get_channels(archived=false)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.list')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    if archived
      url = "#{uri.to_s}?token=#{Slack::Config.token}&exclude_members=true"
    else
      url = "#{uri.to_s}?token=#{Slack::Config.token}&exclude_archived=true&exclude_members=true"
    end
    req  = Net::HTTP::Post.new(url)
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['channels']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#history(id, count) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/slack-wrapper/api/channels.rb', line 127

def history(id, count)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.history')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}&count=#{count}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['messages']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#info(id) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slack-wrapper/api/channels.rb', line 109

def info(id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.info')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['channel']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#invite_user(user, channel) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/slack-wrapper/api/channels.rb', line 181

def invite_user(user, channel)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.invite')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&user=#{user}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#join(name) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/slack-wrapper/api/channels.rb', line 145

def join(name)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.join')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&name=#{name}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#kick_user(user, channel) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/slack-wrapper/api/channels.rb', line 199

def kick_user(user, channel)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.kick')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&user=#{user}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#leave(id) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/slack-wrapper/api/channels.rb', line 163

def leave(id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.join')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#rename(id, name) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/slack-wrapper/api/channels.rb', line 217

def rename(id, name)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.rename')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}&name=#{name}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#search(term, archived = false, regex = false) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/slack-wrapper/api/channels.rb', line 47

def search(term, archived=false, regex=false)
  channels = get_channels(archived)
  if regex
    channels.select{|c| c['name'] =~ /#{term}/}
  else
    channels.select{|c| c['name'] == term}.first
  end
end

#set_purpose(id, text) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/slack-wrapper/api/channels.rb', line 235

def set_purpose(id, text)
  text = URI.escape(text)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.setPurpose')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}&purpose=#{text}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#set_topic(id, text) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/slack-wrapper/api/channels.rb', line 254

def set_topic(id, text)
  text = URI.escape(text)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.setTopic')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}&topic=#{text}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#unarchive(id) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/slack-wrapper/api/channels.rb', line 91

def unarchive(id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/channels.unarchive')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end