Skip to content

Commit

Permalink
Messages are posted with as_user: true by default, closes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jul 19, 2015
1 parent 0baaaca commit 84b6465
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.3.0 (Next)

* [#4](https://github.com/dblock/slack-ruby-bot/issues/4): Messages are posted with `as_user: true` by default - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.2.0 (7/10/2015)
Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def client
rescue StandardError => e
logger.error e
begin
Slack.chat_postMessage(channel: data['channel'], text: e.message) if data.key?('channel')
Slack.chat_postMessage(channel: data['channel'], text: e.message, as_user: true) if data.key?('channel')
rescue
# ignore
end
Expand Down
11 changes: 6 additions & 5 deletions lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ class Base
class_attribute :operators
class_attribute :commands

def self.send_message(channel, text)
def self.send_message(channel, text, options = { as_user: true })
if text && text.length > 0
Slack.chat_postMessage(channel: channel, text: text)
message = { channel: channel, text: text }.merge(options)
Slack.chat_postMessage(message)
else
send_message_with_gif channel, 'Nothing to see here.', 'nothing'
send_message_with_gif channel, 'Nothing to see here.', 'nothing', options
end
end

def self.send_message_with_gif(channel, text, keywords)
def self.send_message_with_gif(channel, text, keywords, options = { as_user: true })
gif = begin
Giphy.random(keywords)
rescue StandardError => e
logger.warn "Giphy.random: #{e.message}"
nil
end
text = text + "\n" + gif.image_url.to_s if gif
send_message channel, text
send_message channel, text, options
end

def self.logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
match do |actual|
channel, user, message = parse(actual)
allow(Giphy).to receive(:random)
expect(SlackRubyBot::Commands::Base).to receive(:send_message).with(channel, expected)
expect(SlackRubyBot::Commands::Base).to receive(:send_message).with(channel, expected, as_user: true)
app.send(:message, text: message, channel: channel, user: user)
true
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-ruby-bot/commands/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
command 'saybye'

def self.call(data, command, arguments)
send_message data.channel, "#{command}: #{arguments}"
send_message data.channel, "#{command}: #{arguments}", as_user: true
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-ruby-bot/commands/empty_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def app
end
it 'sends default text' do
allow(Giphy).to receive(:random)
expect(SlackRubyBot::Commands::Base).to receive(:send_message_with_gif).with('channel', 'Nothing to see here.', 'nothing')
expect(SlackRubyBot::Commands::Base).to receive(:send_message_with_gif).with('channel', 'Nothing to see here.', 'nothing', as_user: true)
app.send(:message, text: "#{SlackRubyBot.config.user} test", channel: 'channel', user: 'user')
end
end
2 changes: 1 addition & 1 deletion spec/slack-ruby-bot/commands/operators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
operator '-'

def self.call(data, command, arguments)
send_message data.channel, "#{command}: #{arguments}"
send_message data.channel, "#{command}: #{arguments}", as_user: true
end
end
end
Expand Down

0 comments on commit 84b6465

Please sign in to comment.