class Alerty::Plugin::Slack::SlackClient::Slackbot

Slack client for Slackbot Remote Control api.slack.com/slackbot

Public Class Methods

new(endpoint, https_proxy = nil) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 153
def initialize(endpoint, https_proxy = nil)
  super
end

Public Instance Methods

api() click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 157
def api
  @api ||= WebApi.new(nil, https_proxy)
end
post_message(params = {}, opts = {}) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 161
def post_message(params = {}, opts = {})
  raise ArgumentError, "channel parameter is required" unless params['channel']
  with_channels_create(params, opts) do
    log.info { "out_slack: post_message #{filter_params(params)}" }
    post(slackbot_endpoint(params), params)
  end
end

Private Instance Methods

encode_body(params = {}) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 175
def encode_body(params = {})
  return params['text']if params['text']
  unless params['attachments']
    raise ArgumentError, "params['text'] or params['attachments'] is required"
  end
  # handle params['attachments']
  attachment = Array(params['attachments']).first # see only the first for now
  # {
  #   attachments: [{
  #     text: "HERE",
  #   }]
  # }
  text = attachment['text']
  # {
  #   attachments: [{
  #     fields: [{
  #       title: "title",
  #       value: "HERE",
  #     }]
  #   }]
  # }
  if text.nil? and attachment['fields']
    text = Array(attachment['fields']).first['value'] # see only the first for now
  end
  text
end
response_check(res, params) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 202
def response_check(res, params)
  if res.body == 'channel_not_found'
    raise ChannelNotFoundError.new(res, params)
  elsif res.body != 'ok'
    raise Error.new(res, params)
  end
end
slackbot_endpoint(params) click to toggle source
# File lib/alerty/plugin/slack/slack_client.rb, line 171
def slackbot_endpoint(params)
  endpoint.dup.tap {|e| e.query += "&channel=#{URI.encode(params['channel'])}" }
end