Created
March 14, 2012 19:15
-
-
Save damien/2038783 to your computer and use it in GitHub Desktop.
Say whatever redis tells you to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
nice! but doesnt hubot do this?
Author
@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
I before E except after C.