Class: SlackWebhooks::Hook
- Inherits:
-
Object
- Object
- SlackWebhooks::Hook
- Defined in:
- lib/slack_webhooks.rb
Instance Attribute Summary collapse
-
#botname ⇒ Object
Returns the value of attribute botname.
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#command ⇒ Object
Returns the value of attribute command.
-
#icon_url ⇒ Object
Returns the value of attribute icon_url.
-
#text ⇒ Object
Returns the value of attribute text.
-
#trigger_word ⇒ Object
Returns the value of attribute trigger_word.
-
#usage_options ⇒ Object
Returns the value of attribute usage_options.
-
#username ⇒ Object
(also: #user_name)
Returns the value of attribute username.
-
#webhook_url ⇒ Object
Returns the value of attribute webhook_url.
Instance Method Summary collapse
- #help? ⇒ Boolean
-
#initialize(botname, body, webhook_url = nil) ⇒ Hook
constructor
botname is the name to post to the channel with.
- #send(s, options = {}) ⇒ Object
- #send_usage(extra_text = '') ⇒ Object
-
#set_usage(text, options = {}) ⇒ Object
Takes similar options as send.
Constructor Details
#initialize(botname, body, webhook_url = nil) ⇒ Hook
botname is the name to post to the channel with. body is the outgoing webhook POST body that Slack sends. webhook_url is the incoming webhook to post back to slack.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/slack_webhooks.rb', line 17 def initialize(botname, body, webhook_url=nil) self.botname = botname self.webhook_url = webhook_url parsed = URI.decode_www_form(body) parsed.each do |p| # puts "#{p[0]}=#{p[1]}" if p[0] == "command" self.command = p[1] # puts "command=#{self.command}" end if p[0] == "trigger_word" # This is for trigger words, not slash commands self.trigger_word = p[1] end if p[0] == "channel_name" self.channel = p[1] end if p[0] == "user_name" self.username = "@#{p[1]}" # puts "username #{username}" end if p[0] == "text" self.text = p[1].strip # puts "text=#{text}" end if p[0] == "response_url" && !webhook_url # we get it in the post body with more recent slack apps self.webhook_url = p[1] end end if self.channel == "directmessage" self.channel = self.username else self.channel = "\##{self.channel}" unless self.channel[0] == '#' end end |
Instance Attribute Details
#botname ⇒ Object
Returns the value of attribute botname.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def botname @botname end |
#channel ⇒ Object
Returns the value of attribute channel.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def channel @channel end |
#command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def command @command end |
#icon_url ⇒ Object
Returns the value of attribute icon_url.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def icon_url @icon_url end |
#text ⇒ Object
Returns the value of attribute text.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def text @text end |
#trigger_word ⇒ Object
Returns the value of attribute trigger_word.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def trigger_word @trigger_word end |
#usage_options ⇒ Object
Returns the value of attribute usage_options.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def @usage_options end |
#username ⇒ Object Also known as: user_name
Returns the value of attribute username.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def username @username end |
#webhook_url ⇒ Object
Returns the value of attribute webhook_url.
8 9 10 |
# File 'lib/slack_webhooks.rb', line 8 def webhook_url @webhook_url end |
Instance Method Details
#help? ⇒ Boolean
92 93 94 95 96 97 98 99 |
# File 'lib/slack_webhooks.rb', line 92 def help? if self.text == "help" # send help directly to user send_usage return true end return false end |
#send(s, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/slack_webhooks.rb', line 67 def send(s, ={}) # Now send it to back to the channel on slack s = "#{command} #{text}" if s.nil? notifier = Slack::Notifier.new webhook_url, channel: channel, username: botname # notifier.channel = channel # notifier.username = botname resp = nil = .delete(:attachment) if [:attachments] ||= [] [:attachments] << end if self.icon_url != nil [:icon_url] = self.icon_url end puts "Posting #{s} to #{channel} with options #{}" resp = notifier.ping s, p resp p resp. end |
#send_usage(extra_text = '') ⇒ Object
62 63 64 65 |
# File 'lib/slack_webhooks.rb', line 62 def send_usage(extra_text='') self.channel = self.username self.send(extra_text, self.) end |
#set_usage(text, options = {}) ⇒ Object
Takes similar options as send. options can have attachments or whatever
56 57 58 59 60 |
# File 'lib/slack_webhooks.rb', line 56 def set_usage(text, ={}) [:attachments] ||= [] [:attachments].unshift({'text' => text}) self. = end |