Skip to content

Instantly share code, notes, and snippets.

@damien
Created March 14, 2012 19:15
Show Gist options
  • Select an option

  • Save damien/2038783 to your computer and use it in GitHub Desktop.

Select an option

Save damien/2038783 to your computer and use it in GitHub Desktop.
Say whatever redis tells you to
#!/usr/bin/env ruby
%w(uri logger rubygems redis childprocess).each do |dep|
begin
require dep
rescue LoadError
puts "The #{dep} gem could not be loaded! Try running: gem install #{dep}"
end
end
$logger = Logger.new(STDOUT)
begin
uri = URI.parse(ENV['REDISTOGO_URL'])
rescue URI::InvalidURIError => e
puts 'REDISTOGO_URL is not a valid URI!'
end
$redis = Redis.new({
:timeout => 0,
:user => uri.user,
:password => uri.password,
:host => uri.host,
:port => uri.port
})
def shutdown!
$logger.info "Shutting down..."
$redis.disconnect
exit(0)
end
Signal.trap(:INT) do
$logger.info "Received SIGINT"
shutdown!
end
Signal.trap(:TERM) do
$logger.info "Received SIGTERM"
shutdown!
end
$redis.subscribe('say') do |on|
on.subscribe do |channel, subscriptions|
$logger.info "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
end
on.unsubscribe do |channel, subscriptions|
$logger.info "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
end
on.message do |channel, msg|
begin
process = ChildProcess.build("say", msg).start
rescue LaunchError => e
$logger.error e
end
end
end
# Send arbitrary strings to redis subscribers
#
# say <text> - Publishes <text> to "say" channel
# saying <text> - "say" <text> and tell us what was said
Url = require("url")
Redis = require("redis")
module.exports = (robot) ->
config = Url.parse(process.env.REDISTOGO_URL)
client = Redis.createClient(config.port, config.hostname)
if config.auth
client.auth config.auth.split(":")[1]
robot.respond /say (.*)/i, (msg) ->
text = msg.match[1]
client.publish("say", text)
robot.respond /saying (.*)/i, (msg) ->
text = msg.match[1]
client.publish("say", text)
msg.send("Saying: " + text)
@jimmycuadra
Copy link

I before E except after C. :trollface:

@jaredatron
Copy link

nice! but doesnt hubot do this?

@damien
Copy link
Author

damien commented Mar 14, 2012

@deadlyicon your shellout version of the say script was fine, but it doesn't work very well when hubot itself is running on a remote host. I'm just using redis as an intermediary to do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment