diff --git a/README.md b/README.md index 817b5a6..7cd440f 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,6 @@ The `.new` method will require an 'incoming webhook' integration link from Slack ``` #String containing the URL. my $slack = WebService::Slack::webhook.new(url => "$url"); - -#Or... - -#Path to a file with the URL. -my $slack = WebService::Slack::webhook.new(path => "/path/to/file"); ``` ###`.send` @@ -25,20 +20,11 @@ This will be the method to send a message from. It will be overloaded so that it #Using a hash. my %info = ( username => "perl6-bot", - icon => ":robot_face:", + icon_emoji => ":robot_face:", text => "Beep, boop. *excited robot sounds*" ); $slack.send(%info); -#Or possibly... - -#Without a hash. -$slack.send( - username => "perl6-bot", - icon => ":robot_face:", - text => "Beep, boop. *excited robot sounds*" -); - #Or if you just want to send something... #Just a string. diff --git a/lib/WebService/Slack/webhook.pm6 b/lib/WebService/Slack/webhook.pm6 index 4ea4b4e..331081b 100755 --- a/lib/WebService/Slack/webhook.pm6 +++ b/lib/WebService/Slack/webhook.pm6 @@ -6,9 +6,29 @@ use JSON::Fast; #Make the class. class WebService::Slack::webhook { #Make some vars. - has Str $!url; + has Str $.url is required where {$_ ~~ /https\:\/\/hooks\.slack\.com\/services\//}; - #Make some methods to build the object. - multi method new(Str:D $var) { self.bless(:url($var)); } - multi method new($other?) { die "No Slack integration given!"; } + #Using a hash for the info. + multi method send(%info) { + say "Entering hash method."; + #Make sure the data is valid. + unless all %info.values.map({$_ ~~ Str|Any}) { + die "Bad data in key-value pairs.\nGot: {%info.pairs}"; + } + + #Setup the data to be sent. + my %header = :Content-type("application/json"); + my $body = Buf.new(to-json(%info).ords); + + #Send the request. + say $.url.perl ~ "\n" ~ %header.perl ~ "\n" ~ %info.perl; + my $resp = Net::HTTP::POST($.url, :%header, :$body); + } + + #using a string for the info. + multi method send(Str $msg) { + say "Entering string method."; + my %info = (:text($msg)); + self.send(%info); + } } diff --git a/t/02-make.t b/t/02-make.t index d5dc61e..668dbe5 100755 --- a/t/02-make.t +++ b/t/02-make.t @@ -6,8 +6,7 @@ use lib 'lib'; use WebService::Slack::webhook; #Make some vars -my $fake-url = "https://host.slack.com/example"; -my $file-name = ".slackurl"; +my $fake-url = "https://hooks.slack.com/services/example.html"; #Make a bad object. dies-ok {WebService::Slack::webhook.new()}, @@ -18,12 +17,4 @@ isa-ok WebService::Slack::webhook.new(url => "$fake-url"), 'WebService::Slack::webhook', 'Good object can be made with url'; -#Make a good object with a path. -"$file-name".IO.spurt($fake-url); #Make the file. -isa-ok WebService::Slack::webhook.new(path => "$file-name"), - 'WebService::Slack::webhook', - 'Good object can be made with path'; -"$file-name".IO.unlink; #Remove the file. - - done-testing; diff --git a/t/03-send.t b/t/03-send.t new file mode 100755 index 0000000..371412d --- /dev/null +++ b/t/03-send.t @@ -0,0 +1,20 @@ +#!/usr/bin/env perl6 + +use Test; +use lib 'lib'; + +use WebService::Slack::webhook; + +#Make some vars +my $fake-url = "https://hooks.slack.com/services/example/integration/url"; + +my $slack = WebService::Slack::webhook.new(url => $fake-url); +my %info = ( + text => "This is a test with a hash", + username => "test-bot" +); + +ok $slack.send("This is a test with a string"), 'Send works with string arg'; +ok $slack.send(%info), 'Send works with hash arg'; + +done-testing;