Created
April 21, 2019 04:21
-
-
Save icelander/069a1504f0d191ded855f450492b6967 to your computer and use it in GitHub Desktop.
pingme - A simple way to ping a Mattermost webhook from the command line and send STDIN
This file contains 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 | |
# Sends a ping to a Mattermost webhook and can include stdin as a code block | |
# | |
# | |
# Options | |
# -c, --channel <channel_name> - Name of channel to post to. Use @username to send a DM | |
# -u, --username <username> - username to post as, if the webhook allows it | |
# -i, --icon_url <icon_url> - URL of user icon to use | |
require 'net/https' | |
require 'uri' | |
require 'json' | |
require 'pp' | |
require 'slop' | |
opts = Slop.parse do |o| | |
o.string '-c', '--channel', 'channel_name' | |
o.string '-u', '--username', 'username' | |
o.string '-i', '--icon_url', 'icon_url' | |
end | |
data = opts.to_hash | |
# TODO: Read this from something configurable. Or even better, make this an API call | |
webhook_url = "https://mattermost.example.com/hooks/n4kkjnksf9s9jfn49sokdfj48n" | |
text = 'Ping!' | |
if opts.arguments.length > 0 | |
text = opts.arguments.join(' ') | |
end | |
# Grab STDIN if it's not a TTY and add it as a code block | |
if ! $stdin.tty? | |
std_input = $stdin.read | |
if std_input.length > 0 | |
text += "\n" | |
text += "```\n" | |
text += std_input | |
text += "\n" | |
text += "```" | |
end | |
end | |
data[:text] = text | |
json_headers = {"Content-Type" => "application/json", | |
"Accept" => "application/json"} | |
uri = URI.parse(webhook_url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
response = http.post(uri.path, data.to_json, json_headers) | |
if response.body() != 'ok' | |
puts "Pingme error: #{response.code} #{response.body()}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment