Class: Slack::Poster

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/poster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url, options = {}) ⇒ Poster

Initializes a Poster instance to post messages with an incoming webhook URL. It also accepts an options hash. If no options are given then the poster uses the default configuration from Slack integration.

Examples

# Without options
Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va')

# With options using a custom username and icon avatar
Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
  username: 'Ricardo',
  icon_url: 'http://www.gravatar.com/avatar/92e00fd27c64c94d04140cef88039468.png')

# You can also use an emoji as avatar
Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
  username: 'Ricardo',
  icon_emoji: 'ghost')

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/slack/poster.rb', line 39

def initialize(webhook_url, options = {})
  @base_uri = webhook_url

  @options = options

  raise ArgumentError, 'Webhook URL is required' if webhook_url.nil?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/slack/poster.rb', line 12

def options
  @options
end

Instance Method Details

#send_message(message) ⇒ Object

Sends a message to Slack. The message can be either plain text or a Slack::Message object.

Examples

# Plain text message
poster.send_message('hello world')

# Using a message object
poster.send_message(Slack::Message.new(text: 'hello world'))

You can have messages with attachments if you build your message with a Slack::Message object and add Slack::Attachment objects.



59
60
61
62
63
64
65
66
67
# File 'lib/slack/poster.rb', line 59

def send_message(message)
  body = message.is_a?(String) ? options.merge(text: message) : options.merge(message.as_json)

  conn = Faraday.new(url: @base_uri)

  response = conn.post('', payload: body.to_json)

  response
end