class Slackbotsy::Api

Public Class Methods

new(token) click to toggle source
# File lib/slackbotsy/api.rb, line 9
def initialize(token)
  @token = token
end

Public Instance Methods

channels() click to toggle source
# File lib/slackbotsy/api.rb, line 20
def channels
  @channels ||= get_objects('channels.list', 'channels')
end
get_objects(method, key) click to toggle source

get a channel, group, im or user list

# File lib/slackbotsy/api.rb, line 14
def get_objects(method, key)
  self.class.get("/#{method}", query: { token: @token }).tap do |response|
    raise "error retrieving #{key} from #{method}: #{response.fetch('error', 'unknown error')}" unless response['ok']
  end.fetch(key)
end
groups() click to toggle source
# File lib/slackbotsy/api.rb, line 24
def groups
  @groups ||= get_objects('groups.list', 'groups')
end
ims() click to toggle source
# File lib/slackbotsy/api.rb, line 28
def ims
  @ims ||= get_objects('im.list', 'ims')
end
join(channel) click to toggle source

join a channel, needed to post to channel

# File lib/slackbotsy/api.rb, line 37
def join(channel)
  self.class.post('/channels.join', body: {name: channel, token: @token}).tap do |response|
    raise "error posting message: #{response.fetch('error', 'unknown error')}" unless response['ok']
  end
end
post_message(params) click to toggle source

send message to one channel as a single post with params text, channel, as_user

# File lib/slackbotsy/api.rb, line 44
def post_message(params)
  self.class.post('/chat.postMessage', body: params.merge({token: @token})).tap do |response|
    raise "error posting message: #{response.fetch('error', 'unknown error')}" unless response['ok']
  end
end
upload(params) click to toggle source

upload a file or text snippet, with params file, filename, filetype, title, initial_comment, channels

# File lib/slackbotsy/api.rb, line 51
def upload(params)
  self.class.post('/files.upload', body: params.merge({token: @token})).tap do |response|
    raise "error uploading file: #{response.fetch('error', 'unknown error')}" unless response['ok']
  end
end
users() click to toggle source
# File lib/slackbotsy/api.rb, line 32
def users
  @users ||= get_objects('users.list', 'members')
end