Class: PeopleGroup::Connectors::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/peoplegroup/connectors/slack.rb

Instance Method Summary collapse

Constructor Details

#initializeSlack

Returns a new instance of Slack.



8
9
10
11
12
13
14
15
16
17
# File 'lib/peoplegroup/connectors/slack.rb', line 8

def initialize
  ::Slack::Web::Client.configure do |config|
    config.token = ENV['SLACK_API_TOKEN']

    # configure certificate file
    # fixed in slack-ruby-client 2.x
    config.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE']
  end
  @client = ::Slack::Web::Client.new
end

Instance Method Details

#bamboo_email_lookup(email) ⇒ Object Also known as: bamboo_email_lookup_with_fallback, find_user



19
20
21
22
23
# File 'lib/peoplegroup/connectors/slack.rb', line 19

def bamboo_email_lookup(email)
  @client.users_lookupByEmail(email: email)
rescue ::Slack::Web::Api::Errors::SlackError
  nil
end

#find_user_by_id(id) ⇒ Object



27
28
29
30
31
# File 'lib/peoplegroup/connectors/slack.rb', line 27

def find_user_by_id(id)
  @client.users_info(user: id)
rescue ::Slack::Web::Api::Errors::SlackError
  nil
end

#find_user_profile(id:, include_labels: false) ⇒ Object



33
34
35
36
37
# File 'lib/peoplegroup/connectors/slack.rb', line 33

def (id:, include_labels: false)
  @client.users_profile_get(user: id, include_labels: include_labels)
rescue ::Slack::Web::Api::Errors::SlackError
  nil
end

#get_message(channel:, timestamp:) ⇒ Object



92
93
94
95
# File 'lib/peoplegroup/connectors/slack.rb', line 92

def get_message(channel:, timestamp:)
  options = { channel: channel, latest: timestamp, limit: 1, inclusive: true }
  @client.conversations_history(options)&.messages&.first
end


47
48
49
# File 'lib/peoplegroup/connectors/slack.rb', line 47

def link_for_message(channel:, timestamp:)
  @client.chat_getPermalink(channel: channel, message_ts: timestamp)
end

#list_public_channelsObject



41
42
43
44
# File 'lib/peoplegroup/connectors/slack.rb', line 41

def list_public_channels
  response = @client.conversations_list(exclude_archived: true)
  response['channels'].map { |channel| channel['name'] }
end

#publish_view(user_id:, view:, **options) ⇒ Object



87
88
89
# File 'lib/peoplegroup/connectors/slack.rb', line 87

def publish_view(user_id:, view:, **options)
  @client.views_publish(user_id: user_id, view: view, **options)
end

#send_message(channel:, text: '', as_user: true, attachments: [], thread_ts: nil, reply_broadcast: false, formatted_message: []) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/peoplegroup/connectors/slack.rb', line 52

def send_message(channel:, text: '', as_user: true, attachments: [], thread_ts: nil, reply_broadcast: false, formatted_message: [])
  options = {
    channel: channel,
    text: text,
    as_user: as_user,
    attachments: attachments,
    unfurl_links: false,
    thread_ts: thread_ts,
    reply_broadcast: reply_broadcast,
    blocks: formatted_message
  }

  @client.chat_postMessage(options)
end

#send_modal_message(trigger:, view:) ⇒ Object



82
83
84
# File 'lib/peoplegroup/connectors/slack.rb', line 82

def send_modal_message(trigger:, view:)
  @client.views_open(trigger_id: trigger, view: view)
end

#update_message(channel:, timestamp:, text: '', attachments: [], formatted_message: []) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/peoplegroup/connectors/slack.rb', line 68

def update_message(channel:, timestamp:, text: '', attachments: [], formatted_message: [])
  options = {
    channel: channel,
    text: text,
    ts: timestamp,
    as_user: true,
    attachments: attachments,
    blocks: formatted_message
  }

  @client.chat_update(options)
end