Skip to content

Instantly share code, notes, and snippets.

@inkel
Created May 31, 2013 19:23
Show Gist options
  • Save inkel/5687329 to your computer and use it in GitHub Desktop.
Save inkel/5687329 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
BIN = "hipchat".freeze
USAGE =<<EOS
#{BIN}(1)
NAME
#{BIN} -- Simple interface to HipChat
SYNOPSIS
#{BIN} -t TOKEN -r ROOM message
command | #{BIN} -t TOKEN -r ROOM
DESCRIPTION
Sends a message to the indicated HipChat room
-t TOKEN
The authentication token to be used.
-r ROOM
The HipChat room where the message will be send.
-c COLOR
Color of the message. One of yellow, red, green, purple,
gray or random. Defaults to yellow.
-f FROM
Name of the user sending the message. Defaults to current
username.
-F FORMAT
One of html or text. Defaults to text.
-n
Trigger a notification when the message is sent.
-h
This help page.
EOS
require "clap"
require "net/https"
options = {}
words = Clap.run ARGV, {
"-t" => lambda { |token| options[:token] = token },
"-r" => lambda { |room| options[:room] = room },
"-c" => lambda { |color| options[:color] = color },
"-f" => lambda { |from| options[:from] = from },
"-F" => lambda { |format| options[:format] = format },
"-n" => lambda { options[:notify] = 1 },
"-h" => lambda { puts USAGE ; exit 0 }
}
message = STDIN.tty? ? words.join(" ") : STDIN.read
if options[:token].nil? || options[:room].nil? || message.empty?
puts USAGE
exit 1
end
url = URI.parse("https://api.hipchat.com/v1/rooms/message")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url.path)
request.set_form_data({
auth_token: options[:token],
room_id: options[:room],
from: options[:from] || `whoami`.strip,
color: options[:color] || "yellow",
notify: options[:notify] || 0,
message: message,
message_format: options[:format] || "text"
})
response = http.request(request)
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment