class Slackwebhook

Attributes

headers[RW]
output[R]
type[RW]
webhook[RW]

Public Class Methods

new(webhook) click to toggle source
# File lib/slackwebhook.rb, line 13
def initialize(webhook)
        #Output Variable to see th output of the scan
        @output = ""
        #Check the url and assign it
        @webhook = check_url(webhook)
        #Headers to set for the slack post request
        @headers = {'Content-Type': 'text/json'}
        #Type of request to send to the hook
        @type = "normal"

end

Public Instance Methods

check_url(webhook) click to toggle source
# File lib/slackwebhook.rb, line 57
def check_url(webhook)
        #Parse url
        uri = URI.parse(webhook)
        # If parsing of url and host of the url matches the hooks.slack.com then return url
        (webhook =~ URI::regexp && uri.host == "hooks.slack.com") ? uri : error("Invalid URL")
end
data_format(data) click to toggle source
# File lib/slackwebhook.rb, line 39
def data_format(data)
        #format the message depend upon the type field
        @body = {}            
        case @type.downcase
        when 'info'
                @body[:attachments] = [{color: "#0FF612",text: data}]
        when 'warning'
                @body[:attachments] = [{color: "#F2F60F",text: data}]
        when 'alert'
                @body[:attachments] = [{color: "#e33d3b",text: data}]
        else
                @body[:text] = data
        end
        puts @body
        @body

end
error(text) click to toggle source
# File lib/slackwebhook.rb, line 64
def error(text)
        #Display the error
        puts "[-] #{text}. Check the url documentation: https://api.slack.com/incoming-webhooks"
        #assing to output variable
        @output = text
end
send=(data) click to toggle source
# File lib/slackwebhook.rb, line 25
def send=(data)
        #Set the https request
        https = Net::HTTP.new(@webhook.host, @webhook.port)
        https.use_ssl = true
        #Set the hearders and path for the request
        request = Net::HTTP::Post.new(@webhook.request_uri, @header)
        #set the output message
        request.body = data_format(data).to_json
        #send the request and assing the response to output variable
        @output = https.request(request)
        puts @output.code
end