class DistributionWrappers::SlackWrapper

Public Instance Methods

get_contacts() click to toggle source
# File lib/distribution_wrappers/slack/slack.rb, line 11
def get_contacts
  url = "https://slack.com/api/channels.list?token=#{@auth[:key]}"
  channels = JSON.parse(RestClient.get(url))['channels']

  url = "https://slack.com/api/users.list?token=#{@auth[:key]}"
  members = JSON.parse(RestClient.get(url))["members"]

  return nil if (channels.nil? || channels.length == 0) && (members.nil? || members.length == 0)

  csv_sting = ""

  # Loop through the channels just grabbing the names of them (that's all we need for the user)
  channels.each_with_index do |channel, index|
    csv_sting += CSV.generate_line [channel["name"], channel["name"], '{"url": null}', 'channel', @params[:channel_id]]

    if index % 100 == 0
      @params[:temp_file].write(csv_sting)
      csv_sting = ""
    end
  end

  @params[:temp_file].write(csv_sting)

  csv_sting = ""

  members.each_with_index do |member, index|
    icon_url = member['profile']['image_192'] ? member['profile']['image_192'] : "null"
    csv_sting += CSV.generate_line [member["real_name"], member["id"], "{\"url\": #{icon_url}}", 'user', @params[:channel_id]]

    if index % 100 == 0
      @params[:temp_file].write(csv_sting)
      csv_sting = ""
    end
  end

  @params[:temp_file].write(csv_sting)

  return true
end
send_message(recipient) click to toggle source
Calls superclass method DistributionWrappers::Base#send_message
# File lib/distribution_wrappers/slack/slack.rb, line 4
def send_message(recipient)
  super
  client = get_access(@auth[:key])

  client.chat_postMessage(channel: recipient['identifier'], attachments: [@message], as_user: true)
end

Private Instance Methods

get_access(key) click to toggle source
# File lib/distribution_wrappers/slack/slack.rb, line 79
def get_access(key)
  Slack.configure do |config|
    config.token = key
  end

  return Slack::Web::Client.new
end
prepare(identifier=nil) click to toggle source
# File lib/distribution_wrappers/slack/slack.rb, line 53
def prepare(identifier=nil)
  base_url = ""
  if ENV['ENVIRONMENT'] == "development"
    base_url = "http://localhost:3000"
  else
    base_url = "https://studio.backstit.ch"
  end

  params = "contact_id=#{@version_contact.id}&version_id=#{@version_contact.studio_post_version_id}&organization_id=#{@params[:organization].id}"

  return {
    title: @params[:post].title,
    title_link: "#{base_url}/posts/#{@params[:post].id}?#{params}",
    author_name: @params[:user].display_name,
    color: @params[:organization].highlight_color,
    fallback: @params[:post].title,
    image_url: @params[:post].screenshot_url,
    fields: [
      {
        value: "#{TextHelper.clean_text(@params[:post].draft_content)[0..280]}...",
        short: false
      }
    ]
  }
end