module Slack::API::Channels

Public Instance Methods

active_channels() click to toggle source
# File lib/slack-wrapper/api/channels.rb, line 44
def active_channels
  get_channels()
end
all_channels() click to toggle source
# File lib/slack-wrapper/api/channels.rb, line 41
def all_channels
  get_channels(true)
end
archive(id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
set_purpose(id, text) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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