Class: SlackBot::ApiClient

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

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: ) ⇒ ApiClient

Returns a new instance of ApiClient.

Parameters:

  • authorization_token: (String) (defaults to: )


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

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

Instance Attribute Details

#clientFaraday::Connection (readonly)

Returns the value of attribute client.

Returns:

  • (Faraday::Connection)


68
69
70
# File 'lib/slack_bot/api_client.rb', line 68

def client
  @client
end

Instance Method Details

#chat_delete(channel:, ts:) ⇒ ApiResponse

Parameters:

  • channel: (String)
  • ts: (String)

Returns:



91
92
93
# File 'lib/slack_bot/api_client.rb', line 91

def chat_delete(channel:, ts:)
  perform_request { client.post("chat.delete", {channel: channel, ts: ts}.to_json) }
end

#chat_delete_scheduled_message(channel:, scheduled_message_id:) ⇒ ApiResponse

Parameters:

  • channel: (String)
  • scheduled_message_id: (String)

Returns:



120
121
122
# File 'lib/slack_bot/api_client.rb', line 120

def chat_delete_scheduled_message(channel:, scheduled_message_id:)
  perform_request { client.post("chat.deleteScheduledMessage", {channel: channel, scheduled_message_id: scheduled_message_id}.to_json) }
end

Parameters:

  • channel: (String)
  • message_ts: (String)

Returns:



124
125
126
# File 'lib/slack_bot/api_client.rb', line 124

def chat_get_permalink(channel:, message_ts:)
  perform_request { client.post("chat.getPermalink", {channel: channel, message_ts: message_ts}.to_json) }
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) ⇒ ApiResponse

Parameters:

  • channel: (String)
  • user: (String)
  • text: (String)
  • as_user: (Boolean, nil) (defaults to: nil)
  • attachments: (Array[Hash[String, untyped]], nil) (defaults to: nil)
  • blocks: (Array[Hash[String, untyped]], nil) (defaults to: nil)
  • icon_emoji: (String, nil) (defaults to: nil)
  • icon_url: (String, nil) (defaults to: nil)
  • link_names: (Boolean, nil) (defaults to: nil)
  • parse: (String, nil) (defaults to: nil)
  • thread_ts: (String, nil) (defaults to: nil)
  • username: (String, nil) (defaults to: nil)

Returns:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/slack_bot/api_client.rb', line 141

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
  )
  perform_request { client.post("chat.postEphemeral", args.to_json) }
end

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

Parameters:

  • channel: (String)
  • text: (String)
  • blocks: (Array[Hash[String, untyped]])

Returns:



83
84
85
# File 'lib/slack_bot/api_client.rb', line 83

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

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

Parameters:

  • channel: (String)
  • text: (String)
  • post_at: (Integer)
  • blocks: (Array[Hash[String, untyped]], nil) (defaults to: nil)

Returns:



111
112
113
# File 'lib/slack_bot/api_client.rb', line 111

def chat_schedule_message(channel:, text:, post_at:, blocks: nil)
  perform_request { client.post("chat.scheduleMessage", {channel: channel, text: text, post_at: post_at, blocks: blocks}.to_json) }
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) ⇒ ApiResponse

Parameters:

  • channel: (String)
  • ts: (String)
  • unfurls: (Hash[String, untyped])
  • source: (String, nil) (defaults to: nil)
  • unfurl_id: (String, nil) (defaults to: nil)
  • user_auth_blocks: (Array[Hash[String, untyped]], nil) (defaults to: nil)
  • user_auth_message: (String, nil) (defaults to: nil)
  • user_auth_required: (Boolean, nil) (defaults to: nil)
  • user_auth_url: (String, nil) (defaults to: nil)

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/slack_bot/api_client.rb', line 95

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)
  perform_request 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)
  end
end

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

Parameters:

  • channel: (String)
  • ts: (String)
  • text: (String)
  • blocks: (Array[Hash[String, untyped]])

Returns:



87
88
89
# File 'lib/slack_bot/api_client.rb', line 87

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

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

Parameters:

  • channel: (String, nil) (defaults to: nil)
  • cursor: (String, nil) (defaults to: nil)
  • latest: (Integer, nil) (defaults to: nil)
  • limit: (Integer, nil) (defaults to: nil)
  • oldest: (Integer, nil) (defaults to: nil)
  • team_id: (String, nil) (defaults to: nil)

Returns:



115
116
117
118
# File 'lib/slack_bot/api_client.rb', line 115

def scheduled_messages_list(channel: nil, cursor: nil, latest: nil, limit: nil, oldest: nil, team_id: nil)
  args = compact_payload(channel: channel, cursor: cursor, limit: limit, latest: latest, oldest: oldest, team_id: team_id)
  perform_request { client.post("scheduled_messages.list", args.to_json) }
end

#users_info(user_id:) ⇒ ApiResponse

Parameters:

  • user_id: (String)

Returns:



128
129
130
# File 'lib/slack_bot/api_client.rb', line 128

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

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

Parameters:

  • cursor: (String, nil) (defaults to: nil)
  • limit: (Integer) (defaults to: 200)
  • include_locale: (Boolean, nil) (defaults to: nil)
  • team_id: (String, nil) (defaults to: nil)

Returns:



136
137
138
139
# File 'lib/slack_bot/api_client.rb', line 136

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)
  perform_request { client.post("users.list", args.to_json) }
end

#views_open(trigger_id:, view:) ⇒ ApiResponse

Parameters:

  • trigger_id: (String)
  • view: (Hash[String, untyped])

Returns:



75
76
77
# File 'lib/slack_bot/api_client.rb', line 75

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

#views_publish(user_id:, view:) ⇒ ApiResponse

Parameters:

  • user_id: (String)
  • view: (Hash[String, untyped])

Returns:



132
133
134
# File 'lib/slack_bot/api_client.rb', line 132

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

#views_update(view_id:, view:) ⇒ ApiResponse

Parameters:

  • view_id: (String)
  • view: (Hash[String, untyped])

Returns:



79
80
81
# File 'lib/slack_bot/api_client.rb', line 79

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