Class: Fluent::SlackClient::WebApi

Inherits:
Base
  • Object
show all
Defined in:
lib/fluent/plugin/slack_client.rb

Overview

Slack client for Web API

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Base

#debug_dev, #log

Instance Method Summary collapse

Methods inherited from Base

#initialize, #post

Constructor Details

This class inherits a constructor from Fluent::SlackClient::Base

Instance Method Details

#channels_create(params = {}, opts = {}) ⇒ Object

Creates a channel.

NOTE: Bot user can not create a channel. Token must be issued by Normal User Account



115
116
117
118
# File 'lib/fluent/plugin/slack_client.rb', line 115

def channels_create(params = {}, opts = {})
  log.info { "out_slack: channels_create #{params.dup.tap {|p| p[:token] = '[FILTERED]' if p[:token] }}" }
  post(channels_create_endpoint, params)
end

#channels_create_endpointObject



82
83
84
# File 'lib/fluent/plugin/slack_client.rb', line 82

def channels_create_endpoint
  @channels_create_endpoint ||= URI.join(DEFAULT_ENDPOINT, "channels.create")
end

#post_message(params = {}, opts = {}) ⇒ Object

Sends a message to a channel.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/slack_client.rb', line 91

def post_message(params = {}, opts = {})
  retries = 1
  begin
    log.info { "out_slack: post_message #{params.dup.tap {|p| p[:token] = '[FILTERED]' if p[:token] }}" }
    post(post_message_endpoint, params)
  rescue ChannelNotFoundError => e
    if opts[:auto_channels_create]
      log.warn "out_slack: channel \"#{params[:channel]}\" is not found. try to create the channel, and then retry to post the message."
      channels_create({name: params[:channel], token: params[:token]})
      retry if (retries -= 1) >= 0 # one time retry
    else
      raise e
    end
  end
end

#post_message_endpointObject



78
79
80
# File 'lib/fluent/plugin/slack_client.rb', line 78

def post_message_endpoint
  @post_message_endpoint    ||= URI.join(DEFAULT_ENDPOINT, "chat.postMessage")
end