Class: SmartTodo::SlackClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_todo/slack_client.rb

Overview

A simple client around the Slack API.

Examples:

Sending a message to a user.

SmartTodo::SlackClient.new.post_message('#general', 'Hello!')

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(slack_token) ⇒ SlackClient

Returns a new instance of SlackClient.

Parameters:

  • slack_token (String)


27
28
29
30
# File 'lib/smart_todo/slack_client.rb', line 27

def initialize(slack_token)
  @slack_token = slack_token
  @client = HttpClientBuilder.build("slack.com")
end

Instance Method Details

#lookup_user_by_email(email) ⇒ Hash

Retrieve the Slack ID of a user from his email

Parameters:

  • email (String)

Returns:

  • (Hash)

Raises:

  • (Net::HTTPError)

    in case the request to Slack failed

  • (SlackClient::Error)

    in case Slack returns a { ok: false } in the body

See Also:



41
42
43
44
45
# File 'lib/smart_todo/slack_client.rb', line 41

def lookup_user_by_email(email)
  headers = default_headers.merge("Content-Type" => "application/x-www-form-urlencoded")
  request = Net::HTTP::Get.new("/api/users.lookupByEmail?email=#{CGI.escape(email)}", headers)
  dispatch(request)
end

#post_message(channel, text) ⇒ Hash

Send a message to a Slack channel or to a user

Parameters:

  • channel (String)

    The Slack channel or the user ID

  • text (String)

    The message to send

Returns:

  • (Hash)

Raises:

  • (Net::HTTPError)

    in case the request to Slack failed

  • (SlackClient::Error)

    in case Slack returns a { ok: false } in the body

See Also:



57
58
59
60
61
62
# File 'lib/smart_todo/slack_client.rb', line 57

def post_message(channel, text)
  headers = default_headers
  request = Net::HTTP::Post.new("/api/chat.postMessage", headers)
  request.body = JSON.dump(channel: channel, text: text)
  dispatch(request)
end