Class: QbertBot::Slack

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Slack

Returns a new instance of Slack.



7
8
9
10
11
# File 'lib/qbert_bot/slack.rb', line 7

def initialize(conf)
  @hook = conf['hook']
  @default_name = conf['name']
  @default_icon = conf['icon']
end

Instance Attribute Details

#default_iconObject (readonly)

Returns the value of attribute default_icon.



6
7
8
# File 'lib/qbert_bot/slack.rb', line 6

def default_icon
  @default_icon
end

#default_nameObject (readonly)

Returns the value of attribute default_name.



6
7
8
# File 'lib/qbert_bot/slack.rb', line 6

def default_name
  @default_name
end

#hookObject (readonly)

Returns the value of attribute hook.



6
7
8
# File 'lib/qbert_bot/slack.rb', line 6

def hook
  @hook
end

Instance Method Details

#channel_or_dm(name) ⇒ Object



13
14
15
16
17
# File 'lib/qbert_bot/slack.rb', line 13

def channel_or_dm(name)
  return name if name[0] == '#'
  return name if name[0] == '@'
  "##{name}"
end

#say(to, text, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qbert_bot/slack.rb', line 20

def say(to, text, opts = {})
  payload = {
    text: text,
    channel: channel_or_dm(to),
    username: default_name,
    icon_emoji: default_icon,
  }
  payload[:icon_emoji] = opts[:icon] if opts.key?(:icon)
  payload[:icon_url] = opts[:icon_url] if opts.key?(:icon_url)
  payload[:username] = opts[:name] if opts.key?(:name)
  payload[:attachments] = opts[:attachments] if opts.key?(:attachments)

  puts("Saying: #{payload[:channel]} -> #{text}")
  Faraday.post(@hook, payload: payload.to_json)
end