Class: ChatNotifier::Chatter::Slack

Inherits:
ChatNotifier::Chatter show all
Defined in:
lib/chat_notifier/chatter/slack.rb

Direct Known Subclasses

Debug

Constant Summary collapse

API_URL =
"https://slack.com/api/chat.postMessage"
REPLIES_URL =
"https://slack.com/api/conversations.replies"
UPDATE_URL =
"https://slack.com/api/chat.update"
DEFAULT_THREAD_GROUP_SIZE =
10
STATUS_EVENT_TYPE =
"chat_notifier_status"

Constants inherited from ChatNotifier::Chatter

OPEN_TIMEOUT, READ_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from ChatNotifier::Chatter

#environment, #repository, #settings, #thread_store

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ChatNotifier::Chatter

#body, handlers, handling, #http_client, #http_post, #initialize, register, #verbose?

Constructor Details

This class inherits a constructor from ChatNotifier::Chatter

Instance Attribute Details

#sleeperObject



45
46
47
# File 'lib/chat_notifier/chatter/slack.rb', line 45

def sleeper
  @sleeper ||= Kernel.method(:sleep)
end

Class Method Details

.handles?(settings) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/chat_notifier/chatter/slack.rb', line 17

def handles?(settings)
  %w[NOTIFY_SLACK_WEBHOOK_URL NOTIFY_SLACK_BOT_TOKEN].any? do |key|
    settings.fetch(key, nil)
  end
end

Instance Method Details

#api_form_post(url, params, process: method(:http_post)) ⇒ Object

Slack read APIs (conversations.history, conversations.replies) take form-encoded params rather than JSON bodies.



70
71
72
73
# File 'lib/chat_notifier/chatter/slack.rb', line 70

def api_form_post(url, params, process: method(:http_post))
  response = process.call(URI(url), URI.encode_www_form(params), form_headers)
  parsed_response(response)
end

#bot_tokenObject



28
29
30
# File 'lib/chat_notifier/chatter/slack.rb', line 28

def bot_token
  settings.fetch("NOTIFY_SLACK_BOT_TOKEN", nil)
end

#channelObject



32
33
34
# File 'lib/chat_notifier/chatter/slack.rb', line 32

def channel
  settings.fetch("NOTIFY_SLACK_NOTIFY_CHANNEL", nil)
end

#conditional_post(messenger, process: method(:http_post)) ⇒ Object



57
58
59
60
61
62
# File 'lib/chat_notifier/chatter/slack.rb', line 57

def conditional_post(messenger, process: method(:http_post))
  return super unless bot_token
  return post(messenger, process:) if messenger.failure? || verbose?

  resolve_episode(messenger, process:)
end

#default_thread_storeObject

With a bot token the channel itself can store thread metadata.



76
77
78
79
80
# File 'lib/chat_notifier/chatter/slack.rb', line 76

def default_thread_store
  return super unless bot_token

  ThreadStore::SlackMetadata.new(chatter: self)
end

#payload(data) ⇒ Object



64
65
66
# File 'lib/chat_notifier/chatter/slack.rb', line 64

def payload(data)
  super(Configuration.for(data, self).to_h)
end

#post(messenger, process: method(:http_post)) ⇒ Object

When a bot token is configured we use the Slack Web API, which can thread failures. The token path is preferred over an incoming webhook.



51
52
53
54
55
# File 'lib/chat_notifier/chatter/slack.rb', line 51

def post(messenger, process: method(:http_post))
  return super unless bot_token

  post_via_api(messenger, process:)
end

#thread_group_sizeObject



36
37
38
39
40
# File 'lib/chat_notifier/chatter/slack.rb', line 36

def thread_group_size
  Integer(settings.fetch("NOTIFY_SLACK_THREAD_GROUP_SIZE", DEFAULT_THREAD_GROUP_SIZE))
rescue ArgumentError, TypeError
  DEFAULT_THREAD_GROUP_SIZE
end

#webhook_urlObject



24
25
26
# File 'lib/chat_notifier/chatter/slack.rb', line 24

def webhook_url
  settings.fetch("NOTIFY_SLACK_WEBHOOK_URL", nil)
end