Class: Slack::Poster
- Inherits:
-
Object
- Object
- Slack::Poster
- Defined in:
- lib/slack/poster.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(webhook_url, options = {}) ⇒ Poster
constructor
Initializes a Poster instance to post messages with an incoming webhook URL.
-
#send_message(message) ⇒ Object
Sends a message to Slack.
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')
39 40 41 42 43 44 45 |
# File 'lib/slack/poster.rb', line 39 def initialize(webhook_url, = {}) @base_uri = webhook_url @options = raise ArgumentError, 'Webhook URL is required' if webhook_url.nil? end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/slack/poster.rb', line 12 def @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.('hello world')
# Using a message object
poster.(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 () body = .is_a?(String) ? .merge(text: ) : .merge(.as_json) conn = Faraday.new(url: @base_uri) response = conn.post('', payload: body.to_json) response end |