delete
=> {
file
=>
'Str'
,
},
info
=> {
file
=>
'Str'
,
count
=> {
isa
=>
'Int'
,
optional
=> 1 },
page
=> {
isa
=>
'Int'
,
optional
=> 1 },
},
list
=> {
channel
=> {
isa
=>
'Str'
,
optional
=> 1 },
count
=> {
isa
=>
'Int'
,
optional
=> 1 },
page
=> {
isa
=>
'Int'
,
optional
=> 1 },
ts_from
=> {
isa
=>
'Str'
,
optional
=> 1 },
ts_to
=> {
isa
=>
'Str'
,
optional
=> 1 },
types
=> {
isa
=>
'Str'
,
optional
=> 1 },
user
=> {
isa
=>
'Str'
,
optional
=> 1 },
},
);
sub
revoke_public_url {
state
$rule
= Data::Validator->new(
file
=>
'Str'
,
)->
with
(
'Method'
,
'AllowExtra'
);
my
(
$self
,
$args
,
%extra
) =
$rule
->validate(
@_
);
return
$self
->request(
'revokePublicURL'
, {
%$args
,
%extra
});
}
sub
shared_public_url {
state
$rule
= Data::Validator->new(
file
=>
'Str'
,
)->
with
(
'Method'
,
'AllowExtra'
);
my
(
$self
,
$args
,
%extra
) =
$rule
->validate(
@_
);
return
$self
->request(
'sharedPublicURL'
, {
%$args
,
%extra
});
}
sub
get_upload_url_external {
state
$rule
= Data::Validator->new(
filename
=>
'Str'
,
length
=>
'Int'
,
)->
with
(
'Method'
,
'AllowExtra'
);
my
(
$self
,
$args
,
%extra
) =
$rule
->validate(
@_
);
return
$self
->request(
'getUploadURLExternal'
, {
%$args
,
%extra
});
}
sub
send_file_to_external_url {
my
(
$self
,
$url
,
$params
) =
@_
;
my
%headers
;
if
(
$self
->client->token &&
$params
->{
'http_auth'
} ) {
my
$msg
=
'Illegal parameters. You have defined \'token\' but the '
.
' method you are using defines its own HTTP Authorization header.'
;
WebService::Slack::WebApi::Exception::IllegalParameters->throw(
message
=>
$msg
,
);
}
if
(
exists
$params
->{file_type}) {
$headers
{
'Content-type'
} =
$params
->{file_type};
}
else
{
$headers
{
'Content-type'
} =
'application/octet-stream'
;
}
if
(
$self
->client->token ) {
$headers
{
'Authorization'
} =
'Bearer '
.
$self
->client->token;
}
elsif
(
$params
->{
'http_auth'
} ) {
$headers
{
'Authorization'
} =
$params
->{
'http_auth'
};
}
my
%options
= (
headers
=> \
%headers
,
content
=>
$params
->{file},
);
my
$response
=
$self
->client->ua->request(
'POST'
,
$url
,
\
%options
);
return
if
$response
->{success};
WebService::Slack::WebApi::Exception::FailureResponse->throw(
message
=>
'file upload failed.'
,
response
=>
$response
,
);
}
sub
complete_upload_external {
state
$rule
= Data::Validator->new(
files
=>
'ArrayRef[HashRef]'
,
channels
=> {
isa
=>
'ArrayRef[Str]'
,
optional
=> 1 },
channel_id
=> {
isa
=>
'Str'
,
optional
=> 1 },
initial_comment
=> {
isa
=>
'Str'
,
optional
=> 1 },
thread_ts
=> {
isa
=>
'Str'
,
optional
=> 1 },
)->
with
(
'Method'
,
'AllowExtra'
);
my
(
$self
,
$args
,
%extra
) =
$rule
->validate(
@_
);
$args
->{channels} =
join
','
, @{
$args
->{channels}}
if
exists
$args
->{channels};
return
$self
->request_json(
'completeUploadExternal'
, {
%$args
,
%extra
});
}
sub
upload {
state
$rule
= Data::Validator->new(
channels
=> {
isa
=>
'ArrayRef[Str]'
,
optional
=> 1 },
content
=> {
isa
=>
'Str'
,
optional
=> 1 },
file
=> {
isa
=>
'Str'
,
optional
=> 1 },
filename
=> {
isa
=>
'Str'
,
optional
=> 1 },
filetype
=> {
isa
=>
'Str'
,
optional
=> 1 },
initial_comment
=> {
isa
=>
'Str'
,
optional
=> 1 },
title
=> {
isa
=>
'Str'
,
optional
=> 1 },
)->
with
(
'Method'
,
'AllowExtra'
);
my
(
$self
,
$args
,
%extra
) =
$rule
->validate(
@_
);
$args
->{file} = [
$args
->{file}]
if
exists
$args
->{file};
$args
->{channels} =
join
','
, @{
$args
->{channels}}
if
exists
$args
->{channels};
return
$self
->request(
'upload'
, {
%$args
,
%extra
});
}
sub
_check_response
{
my
$self
=
shift
;
my
%args
= (
@_
);
my
$response
=
$args
{response};
my
$to
=
$args
{to};
my
$from
=
$args
{from};
my
$text
=
$args
{text};
my
$channel_not_found
=
$args
{channel_not_found};
die
"ERROR: channel_not_found must be a CODE ref!"
if
(!
defined
$channel_not_found
|| (
defined
$channel_not_found
&&
ref
(
$channel_not_found
) ne
"CODE"
));
if
(!
exists
$response
->{ok})
{
die
"ERROR: _check_response(): ok field not found in slack response hash! to='$to', from='$from'\n"
. Dumper(
$response
);
}
else
{
if
(
$response
->{ok})
{
if
(
exists
$response
->{warning})
{
return
1;
}
else
{
return
0;
}
}
else
{
if
(
$response
->{error} eq
'channel_not_found'
)
{
my
$code
=
$channel_not_found
->(
$self
,
response
=>
$response
,
to
=>
$to
,
from
=>
$from
,
text
=>
$text
);
return
2
if
(
$code
== 0);
return
$code
;
}
else
{
die
"ERROR: _check_response(): ok is false and error='$response->{error}'! to='$to', from='$from'\n"
. Dumper(
$response
);
}
}
}
}
sub
upload_v2 {
my
$self
=
shift
;
my
$args
= {
error_handler
=> \
&_check_response
,
@_
};
my
$channel
=
$args
->{channel};
my
$channel_id
=
$args
->{channel_id};
my
$file_contents
=
$args
->{file_contents};
my
$file_type
=
$args
->{file_type};
my
$file_length
=
$args
->{file_length};
my
$filename
=
$args
->{filename};
my
$from
=
$args
->{from};
my
$message
=
$args
->{message};
my
$callingObj
=
$args
->{callingObj};
my
$error_handler
=
$args
->{error_handler};
my
$channel_not_found
=
$args
->{channel_not_found};
my
$code
;
if
(!
defined
$error_handler
) {
die
"upload_v2(): error_handler must be defined!"
;
}
if
(
defined
$error_handler
&&
ref
(
$error_handler
) ne
'CODE'
) {
die
"upload_v2(): error_handler is not a CODE ref! You provided: "
.
ref
(
$error_handler
);
}
if
(!
defined
$channel_not_found
) {
die
"upload_v2(): channel_not_found must be defined!"
;
}
if
(
defined
$channel_not_found
&&
ref
(
$channel_not_found
) ne
'CODE'
) {
die
"upload_v2(): channel_not_found is not a CODE ref! You provided: "
.
ref
(
$channel_not_found
);
}
my
$external_url_response
=
$self
->get_upload_url_external(
filename
=>
$filename
,
length
=>
$file_length
);
$code
=
$error_handler
->(
$callingObj
,
response
=>
$external_url_response
,
to
=>
$channel
,
from
=>
$from
,
text
=>
$message
,
channel_not_found
=>
$channel_not_found
,
);
if
(
$code
== 0) {
my
$url
=
$external_url_response
->{upload_url};
my
$file_id
=
$external_url_response
->{file_id};
$self
->send_file_to_external_url(
$url
,
{
file
=>
$file_contents
,
file_type
=>
$file_type
}
);
my
$complete_upload_response
=
$self
->complete_upload_external(
files
=> [
{
id
=>
$file_id
,
title
=>
$filename
},
],
channel_id
=>
$channel_id
,
initial_comment
=>
$message
,
);
$code
=
$error_handler
->(
$callingObj
,
response
=>
$complete_upload_response
,
to
=>
$channel
,
from
=>
$from
,
text
=>
$message
,
channel_not_found
=>
$channel_not_found
,
);
return
(
$code
,
$complete_upload_response
);
}
return
(
$code
,
$external_url_response
);
}
1;