Class: SmartTodo::Dispatchers::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_todo/dispatchers/slack.rb

Overview

Dispatcher that sends TODO reminders on Slack. Assignees can be either individual (using the associated slack email address) or a channel.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

class_for, #initialize

Constructor Details

This class inherits a constructor from SmartTodo::Dispatchers::Base

Class Method Details

.validate_options!(options) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/smart_todo/dispatchers/slack.rb', line 9

def validate_options!(options)
  options[:slack_token] ||= ENV.fetch("SMART_TODO_SLACK_TOKEN", "")
  raise(ArgumentError, "Missing :slack_token") if options[:slack_token].empty?

  options.fetch(:fallback_channel) { raise(ArgumentError, "Missing :fallback_channel") }
end

Instance Method Details

#dispatchArray

Make a Slack API call to dispatch the message to each assignee

Returns:

  • (Array)

    Slack response for each assignee a message was sent to

Raises:

  • (SlackClient::Error)

    in case the Slack API returns an error other than ‘users_not_found`



23
24
25
26
27
# File 'lib/smart_todo/dispatchers/slack.rb', line 23

def dispatch
  @assignees.each do |assignee|
    dispatch_one(assignee)
  end
end

#dispatch_one(assignee) ⇒ Hash

Make a Slack API call to dispatch the message to the user or channel

Parameters:

  • the (String)

    assignee handle string

Returns:

  • (Hash)

    the Slack response

Raises:

  • (SlackClient::Error)

    in case the Slack API returns an error other than ‘users_not_found`



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/smart_todo/dispatchers/slack.rb', line 36

def dispatch_one(assignee)
  user = slack_user_or_channel(assignee)

  return unless user

  begin
    client.post_message(user.dig("user", "id"), slack_message(user, assignee))
  rescue SlackClient::Error => error
    user = handle_slack_error(error, "Error dispatching message")
    retry
  rescue Net::HTTPError => error
    $stderr.puts "Error dispatching message: #{error.message}"
    $stderr.puts "Response: #{error.response.body}"
  end
end