Class: SlackBot::ApiClient

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

Constant Summary collapse

SLACK_API_BASE_URL =

Slack API base URL

"https://slack.com/api/"
REQUEST_TIMEOUT =

Request timeout in seconds

30
CONNECTION_TIMEOUT =

Connection timeout in seconds

10
TOKEN_FORMAT =
/\Axox[bpa]-/

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.



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

def initialize(authorization_token: ENV["SLACK_BOT_API_TOKEN"])
  validate_authorization_token!(authorization_token)
  @client = build_client(authorization_token)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#chat_delete(channel:, ts:) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/slack_bot/api_client.rb', line 91

def chat_delete(channel:, ts:)
  ApiResponse.new do
    client.post("chat.delete", {channel: channel, ts: ts}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#chat_delete_scheduled_message(channel:, scheduled_message_id:) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/slack_bot/api_client.rb', line 140

def chat_delete_scheduled_message(channel:, scheduled_message_id:)
  ApiResponse.new do
    client.post("chat.deleteScheduledMessage", {channel: channel, scheduled_message_id: scheduled_message_id}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end


148
149
150
151
152
153
154
# File 'lib/slack_bot/api_client.rb', line 148

def chat_get_permalink(channel:, message_ts:)
  ApiResponse.new do
    client.post("chat.getPermalink", {channel: channel, message_ts: message_ts}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/slack_bot/api_client.rb', line 181

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 = compact_payload(
    channel: channel,
    user: user,
    text: text,
    as_user: as_user,
    attachments: attachments,
    blocks: blocks,
    icon_emoji: icon_emoji,
    icon_url: icon_url,
    link_names: link_names,
    parse: parse,
    thread_ts: thread_ts,
    username: username
  )
  ApiResponse.new do
    client.post("chat.postEphemeral", args.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

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



75
76
77
78
79
80
81
# File 'lib/slack_bot/api_client.rb', line 75

def chat_post_message(channel:, text:, blocks:)
  ApiResponse.new do
    client.post("chat.postMessage", {channel: channel, text: text, blocks: blocks}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#chat_schedule_message(channel:, text:, post_at:, blocks: nil) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/slack_bot/api_client.rb', line 117

def chat_schedule_message(channel:, text:, post_at:, blocks: nil)
  ApiResponse.new do
    client.post("chat.scheduleMessage", {channel: channel, text: text, post_at: post_at, blocks: blocks}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#chat_unfurl(channel:, ts:, unfurls:, source: nil, unfurl_id: nil, user_auth_blocks: nil, user_auth_message: nil, user_auth_required: nil, user_auth_url: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/slack_bot/api_client.rb', line 99

def chat_unfurl(channel:, ts:, unfurls:, source: nil, unfurl_id: nil, user_auth_blocks: nil, user_auth_message: nil, user_auth_required: nil, user_auth_url: nil)
  ApiResponse.new do
    client.post("chat.unfurl", {
      channel: channel,
      ts: ts,
      unfurls: unfurls,
      source: source,
      unfurl_id: unfurl_id,
      user_auth_blocks: user_auth_blocks,
      user_auth_message: user_auth_message,
      user_auth_required: user_auth_required,
      user_auth_url: user_auth_url
    }.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

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



83
84
85
86
87
88
89
# File 'lib/slack_bot/api_client.rb', line 83

def chat_update(channel:, ts:, text:, blocks:)
  ApiResponse.new do
    client.post("chat.update", {channel: channel, ts: ts, text: text, blocks: blocks}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#scheduled_messages_list(channel: nil, cursor: nil, latest: nil, limit: nil, oldest: nil, team_id: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/slack_bot/api_client.rb', line 125

def scheduled_messages_list(channel: nil, cursor: nil, latest: nil, limit: nil, oldest: nil, team_id: nil)
  ApiResponse.new do
    client.post("scheduled_messages.list", {
      channel: channel,
      cursor: cursor,
      latest: latest,
      limit: limit,
      oldest: oldest,
      team_id: team_id
    }.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#users_info(user_id:) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/slack_bot/api_client.rb', line 156

def users_info(user_id:)
  ApiResponse.new do
    client.post("users.info", {user: user_id}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

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



172
173
174
175
176
177
178
179
# File 'lib/slack_bot/api_client.rb', line 172

def users_list(cursor: nil, limit: 200, include_locale: nil, team_id: nil)
  args = compact_payload(cursor: cursor, limit: limit, include_locale: include_locale, team_id: team_id)
  ApiResponse.new do
    client.post("users.list", args.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#views_open(trigger_id:, view:) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/slack_bot/api_client.rb', line 59

def views_open(trigger_id:, view:)
  ApiResponse.new do
    client.post("views.open", {trigger_id: trigger_id, view: view}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#views_publish(user_id:, view:) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/slack_bot/api_client.rb', line 164

def views_publish(user_id:, view:)
  ApiResponse.new do
    client.post("views.publish", {user_id: user_id, view: view}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end

#views_update(view_id:, view:) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/slack_bot/api_client.rb', line 67

def views_update(view_id:, view:)
  ApiResponse.new do
    client.post("views.update", {view_id: view_id, view: view}.to_json)
  rescue Faraday::Error => e
    raise SlackBot::Errors::SlackApiError.new("Network error: #{e.message}")
  end
end