Class: SlackWebhooks::Hook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#botnameObject

Returns the value of attribute botname.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def botname
  @botname
end

#channelObject

Returns the value of attribute channel.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def channel
  @channel
end

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def command
  @command
end

#icon_urlObject

Returns the value of attribute icon_url.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def icon_url
  @icon_url
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def text
  @text
end

#trigger_wordObject

Returns the value of attribute trigger_word.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def trigger_word
  @trigger_word
end

#usage_optionsObject

Returns the value of attribute usage_options.



8
9
10
# File 'lib/slack_webhooks.rb', line 8

def usage_options
  @usage_options
end

#usernameObject 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_urlObject

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

Returns:

  • (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, 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 = '') ⇒ 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.usage_options)
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, options={})
  options[:attachments] ||= []
  options[:attachments].unshift({'text' => text})
  self.usage_options = options
end