Class: Slackbotsy::Api

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/slackbotsy/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Api

Returns a new instance of Api.



9
10
11
# File 'lib/slackbotsy/api.rb', line 9

def initialize(token)
  @token = token
end

Instance Method Details

#channelsObject



20
21
22
# File 'lib/slackbotsy/api.rb', line 20

def channels
  @channels ||= get_objects('channels.list', 'channels')
end

#get_objects(method, key) ⇒ Object

get a channel, group, im or user list



14
15
16
17
18
# 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

#groupsObject



24
25
26
# File 'lib/slackbotsy/api.rb', line 24

def groups
  @groups ||= get_objects('groups.list', 'groups')
end

#imsObject



28
29
30
# File 'lib/slackbotsy/api.rb', line 28

def ims
  @ims ||= get_objects('im.list', 'ims')
end

#join(channel) ⇒ Object

join a channel, needed to post to channel



37
38
39
40
41
# 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) ⇒ Object

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



44
45
46
47
48
# 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) ⇒ Object

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



51
52
53
54
55
# 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

#usersObject



32
33
34
# File 'lib/slackbotsy/api.rb', line 32

def users
  @users ||= get_objects('users.list', 'members')
end