class SlackWebhooks::Hook

Attributes

botname[RW]
channel[RW]
command[RW]
icon_url[RW]
text[RW]
trigger_word[RW]
usage_options[RW]
user_name[RW]
username[RW]
webhook_url[RW]

Public Class Methods

new(botname, body, webhook_url=nil) click to toggle source

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.

# 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

Public Instance Methods

help?() click to toggle source
# 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={}) click to toggle source
# File lib/slack_webhooks.rb, line 67
def send(s, options={})
  # 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
  attachment = options.delete(:attachment)
  if attachment
    options[:attachments] ||= []
    options[:attachments] << attachment
  end
  if self.icon_url != nil
    options[:icon_url] = self.icon_url
  end

  puts "Posting #{s} to #{channel} with options #{options}"

  resp = notifier.ping s, options

  p resp
  p resp.message
end
send_usage(extra_text='') click to toggle source
# File lib/slack_webhooks.rb, line 62
def send_usage(extra_text='')
  self.channel = self.username
  self.send(extra_text, self.usage_options)
end
set_usage(text, options={}) click to toggle source

Takes similar options as send. options can have attachments or whatever

# File lib/slack_webhooks.rb, line 56
def set_usage(text, options={})
  options[:attachments] ||= []
  options[:attachments].unshift({'text' => text})
  self.usage_options = options
end