Class: SlackBot::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_bot/api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization_token: ENV["SLACK_BOT_API_TOKEN"]) ⇒ ApiClient

Returns a new instance of ApiClient.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slack_bot/api_client.rb', line 26

def initialize(authorization_token: ENV["SLACK_BOT_API_TOKEN"])
  authorization_token_available = !authorization_token.nil? && authorization_token.is_a?(String) && !authorization_token.empty?
  raise "Slack bot API token is not set" if !authorization_token_available

  @client =
    Faraday.new do |conn|
      conn.request :url_encoded
      conn.adapter Faraday.default_adapter
      conn.url_prefix = "https://slack.com/api/"
      conn.headers["Content-Type"] = "application/json; charset=utf-8"
      conn.headers["Authorization"] = "Bearer #{authorization_token}"
    end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/slack_bot/api_client.rb', line 25

def client
  @client
end

Instance Method Details

#chat_post_ephemeral(channel:, user:, text:, as_user: nil, attachments: nil, blocks: nil, icon_emoji: nil, icon_url: nil, link_names: nil, parse: nil, thread_ts: nil, username: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/slack_bot/api_client.rb', line 73

def chat_post_ephemeral(channel:, user:, text:, as_user: nil, attachments: nil, blocks: nil, icon_emoji: nil, icon_url: nil, link_names: nil, parse: nil, thread_ts: nil, username: nil)
  args = {}
  args[:channel] = channel
  args[:user] = user
  args[:text] = text if text
  args[:as_user] = as_user if as_user
  args[:attachments] = attachments if attachments
  args[:blocks] = blocks if blocks
  args[:icon_emoji] = icon_emoji if icon_emoji
  args[:icon_url] = icon_url if icon_url
  args[:link_names] = link_names if link_names
  args[:parse] = parse if parse
  args[:thread_ts] = thread_ts if thread_ts
  args[:username] = username if username
  ApiResponse.new { client.post("chat.postEphemeral", args.to_json) }
end

#chat_post_message(channel:, text:, blocks:) ⇒ Object



48
49
50
# File 'lib/slack_bot/api_client.rb', line 48

def chat_post_message(channel:, text:, blocks:)
  ApiResponse.new { client.post("chat.postMessage", {channel: channel, text: text, blocks: blocks}.to_json) }
end

#chat_update(channel:, ts:, text:, blocks:) ⇒ Object



52
53
54
# File 'lib/slack_bot/api_client.rb', line 52

def chat_update(channel:, ts:, text:, blocks:)
  ApiResponse.new { client.post("chat.update", {channel: channel, ts: ts, text: text, blocks: blocks}.to_json) }
end

#users_info(user_id:) ⇒ Object



56
57
58
# File 'lib/slack_bot/api_client.rb', line 56

def users_info(user_id:)
  ApiResponse.new { client.post("users.info", {user: user_id}.to_json) }
end

#users_list(cursor: nil, limit: 200, include_locale: nil, team_id: nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/slack_bot/api_client.rb', line 64

def users_list(cursor: nil, limit: 200, include_locale: nil, team_id: nil)
  args = {}
  args[:cursor] = cursor if cursor
  args[:limit] = limit if limit
  args[:include_locale] = include_locale if include_locale
  args[:team_id] = team_id if team_id
  ApiResponse.new { client.post("users.list", args.to_json) }
end

#views_open(trigger_id:, view:) ⇒ Object



40
41
42
# File 'lib/slack_bot/api_client.rb', line 40

def views_open(trigger_id:, view:)
  ApiResponse.new { client.post("views.open", {trigger_id: trigger_id, view: view}.to_json) }
end

#views_publish(user_id:, view:) ⇒ Object



60
61
62
# File 'lib/slack_bot/api_client.rb', line 60

def views_publish(user_id:, view:)
  ApiResponse.new { client.post("views.publish", {user_id: user_id, view: view}.to_json) }
end

#views_update(view_id:, view:) ⇒ Object



44
45
46
# File 'lib/slack_bot/api_client.rb', line 44

def views_update(view_id:, view:)
  ApiResponse.new { client.post("views.update", {view_id: view_id, view: view}.to_json) }
end