Class: CollavreSlack::SlackClient

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre_slack/slack_client.rb

Constant Summary collapse

BASE_URL =
"https://slack.com/api".freeze

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil) ⇒ SlackClient

Returns a new instance of SlackClient.



5
6
7
# File 'app/services/collavre_slack/slack_client.rb', line 5

def initialize(access_token: nil)
  @access_token = access_token
end

Instance Method Details

#add_reaction(channel:, timestamp:, name:) ⇒ Object



84
85
86
# File 'app/services/collavre_slack/slack_client.rb', line 84

def add_reaction(channel:, timestamp:, name:)
  post("reactions.add", { channel: channel, timestamp: timestamp, name: name })
end

#delete_message(channel:, timestamp:) ⇒ Object



68
69
70
# File 'app/services/collavre_slack/slack_client.rb', line 68

def delete_message(channel:, timestamp:)
  post("chat.delete", { channel: channel, ts: timestamp })
end

#get_user_display_name(user_id:) ⇒ Object



76
77
78
79
80
81
82
# File 'app/services/collavre_slack/slack_client.rb', line 76

def get_user_display_name(user_id:)
  response = (user_id: user_id)
  return nil unless response[:ok]

  profile = response.dig(:user, :profile) || {}
  profile[:display_name].presence || profile[:real_name].presence || response.dig(:user, :name)
end

#get_user_info(user_id:) ⇒ Object



72
73
74
# File 'app/services/collavre_slack/slack_client.rb', line 72

def (user_id:)
  get("users.info", user: user_id)
end

#list_all_channelsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/collavre_slack/slack_client.rb', line 37

def list_all_channels
  channels = []
  cursor = nil

  loop do
    response = list_channels(cursor: cursor)
    break unless response[:ok]

    channels.concat(Array(response[:channels]))
    cursor = response.dig(:response_metadata, :next_cursor)
    break if cursor.blank?
  end

  channels
end

#list_channels(cursor: nil) ⇒ Object



31
32
33
34
35
# File 'app/services/collavre_slack/slack_client.rb', line 31

def list_channels(cursor: nil)
  params = { limit: 200, types: "public_channel,private_channel" }
  params[:cursor] = cursor if cursor.present?
  get("conversations.list", params)
end

#list_messages(channel:, oldest: nil, cursor: nil) ⇒ Object



53
54
55
56
57
58
# File 'app/services/collavre_slack/slack_client.rb', line 53

def list_messages(channel:, oldest: nil, cursor: nil)
  params = { channel: channel, limit: 200 }
  params[:oldest] = oldest if oldest.present?
  params[:cursor] = cursor if cursor.present?
  get("conversations.history", params)
end

#oauth_access(code:, redirect_uri:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'app/services/collavre_slack/slack_client.rb', line 20

def oauth_access(code:, redirect_uri:)
  response = connection.post("oauth.v2.access", {
    client_id: client_id,
    client_secret: client_secret,
    code: code,
    redirect_uri: redirect_uri
  })

  parse_response(response)
end

#oauth_authorize_url(state: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/services/collavre_slack/slack_client.rb', line 9

def oauth_authorize_url(state: nil)
  query = {
    client_id: client_id,
    scope: default_scopes,
    redirect_uri: redirect_uri,
    state: state
  }.compact

  "https://slack.com/oauth/v2/authorize?#{query.to_query}"
end

#post_message(channel:, text:) ⇒ Object



60
61
62
# File 'app/services/collavre_slack/slack_client.rb', line 60

def post_message(channel:, text:)
  post("chat.postMessage", { channel: channel, text: text })
end

#redirect_uriObject



92
93
94
# File 'app/services/collavre_slack/slack_client.rb', line 92

def redirect_uri
  CollavreSlack.config.redirect_uri
end

#remove_reaction(channel:, timestamp:, name:) ⇒ Object



88
89
90
# File 'app/services/collavre_slack/slack_client.rb', line 88

def remove_reaction(channel:, timestamp:, name:)
  post("reactions.remove", { channel: channel, timestamp: timestamp, name: name })
end

#update_message(channel:, timestamp:, text:) ⇒ Object



64
65
66
# File 'app/services/collavre_slack/slack_client.rb', line 64

def update_message(channel:, timestamp:, text:)
  post("chat.update", { channel: channel, ts: timestamp, text: text })
end