class ActionMessenger::Providers::Slack

Constants

TokenError

Attributes

client[R]

Public Class Methods

new() click to toggle source
# File lib/action_messenger/providers/slack.rb, line 11
def initialize
  raise(TokenError.new('slack api token is blank.')) if ActionMessenger.config.slack_api_token.blank?

  @client = ::Slack::Web::Client.new.tap do |client|
    client.token = ActionMessenger.config.slack_api_token
  end
end

Public Instance Methods

message(channel, options) click to toggle source

message to slack @param [String] channel @param [Hash] options

# File lib/action_messenger/providers/slack.rb, line 22
def message(channel, options)
  options = {channel: channel}.merge(message_option_to_api_hash(options))
  client.chat_postMessage(options)
end
upload_file(channels, file, options) click to toggle source

upload file to slack @param [String] channels

ex. #sample, #hoge

@param [Faraday::UploadIO] file @param [Hash] options

ex. https://api.slack.com/methods/files.upload
# File lib/action_messenger/providers/slack.rb, line 33
def upload_file(channels, file, options)
  options = {channels: channels, file: file}.merge(upload_file_option_to_api_hash(options))
  client.files_upload(options)
end

Private Instance Methods

message_option_to_api_hash(options) click to toggle source

@param options [Hash] Slack API Request Options

ex. https://api.slack.com/methods/chat.postMessage
# File lib/action_messenger/providers/slack.rb, line 42
def message_option_to_api_hash(options)
  raise ArgumentError('text is blank.') if options[:text].blank? && options[:attachments].blank?

  options.delete_if { |_, v| v.nil? }
end
upload_file_option_to_api_hash(options) click to toggle source
# File lib/action_messenger/providers/slack.rb, line 48
def upload_file_option_to_api_hash(options)
  options.delete_if { |_, v| v.nil? }
end