2020-11-01 18:06:11 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Sendmessage;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
2020-11-08 15:05:53 +00:00
|
|
|
use LWP::UserAgent;
|
|
|
|
use JSON;
|
2020-11-08 17:35:29 +00:00
|
|
|
use JSON::Parse 'parse_json';
|
2020-11-08 15:05:53 +00:00
|
|
|
use Mojo::JWT;
|
|
|
|
use Mojo::File;
|
|
|
|
use Carp;
|
2020-11-01 18:06:11 +00:00
|
|
|
|
|
|
|
has error_messages => sub {
|
2021-03-20 12:09:50 +00:00
|
|
|
return {
|
|
|
|
topic => {
|
|
|
|
required => { message => 'Topic is required', status => 400 },
|
|
|
|
},
|
|
|
|
sender => {
|
|
|
|
required => { message => 'Sender name is required', status => 400 },
|
|
|
|
in_resultset =>
|
|
|
|
{ message => 'Sender org not found', status => 400 },
|
|
|
|
},
|
|
|
|
messagetext => {
|
|
|
|
required => { message => 'Message is required', status => 400 },
|
|
|
|
},
|
|
|
|
};
|
2020-11-01 18:06:11 +00:00
|
|
|
};
|
|
|
|
|
2021-03-20 23:26:52 +00:00
|
|
|
# Credit: Peter Scott/StackOverflow https://stackoverflow.com/a/53357961/4580273
|
|
|
|
# Credit: jeffez/StackOverflow https://stackoverflow.com/q/56556438/4580273
|
2020-11-08 15:05:53 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $jwt = create_jwt_from_path_and_scopes( './localspend-47012.json',
|
|
|
|
'email https://www.googleapis.com/auth/cloud-platform' );
|
2020-11-08 17:35:29 +00:00
|
|
|
|
2020-11-08 15:05:53 +00:00
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $response = $ua->post(
|
|
|
|
'https://www.googleapis.com/oauth2/v4/token',
|
|
|
|
{
|
|
|
|
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
|
|
|
'assertion' => $jwt
|
|
|
|
}
|
2020-11-08 15:05:53 +00:00
|
|
|
);
|
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $bearer_token = parse_json( $response->content );
|
|
|
|
|
|
|
|
sub create_jwt_from_path_and_scopes {
|
|
|
|
my ( $path, $scope ) = @_;
|
|
|
|
croak("No path provided") if not defined $path;
|
|
|
|
croak("$path not available") if not -f $path;
|
|
|
|
my $json = decode_json( Mojo::File->new($path)->slurp );
|
|
|
|
croak("No Private key in $path") if not defined $json->{private_key};
|
|
|
|
croak("Not a service account") if $json->{type} ne 'service_account';
|
|
|
|
my $jwt = Mojo::JWT->new();
|
|
|
|
$jwt->algorithm('RS256');
|
|
|
|
$jwt->secret( $json->{private_key} );
|
|
|
|
|
|
|
|
$jwt->claims(
|
|
|
|
{
|
|
|
|
iss => $json->{client_email},
|
|
|
|
scope => $scope,
|
|
|
|
aud => 'https://www.googleapis.com/oauth2/v4/token',
|
|
|
|
iat => time(),
|
|
|
|
exp => time() + 3600
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$jwt->set_iat(1);
|
|
|
|
return $jwt->encode;
|
2020-11-08 15:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 18:06:11 +00:00
|
|
|
sub post_message {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
|
|
|
|
|
|
|
$validation->required('topic');
|
|
|
|
$validation->required('sender')
|
|
|
|
->in_resultset( 'name', $c->schema->resultset('Organisation') );
|
|
|
|
$validation->required('messagetext');
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
my $end_point =
|
|
|
|
"https://fcm.googleapis.com/v1/projects/localspend-47012/messages:send";
|
|
|
|
|
|
|
|
my $request = HTTP::Request->new( 'POST', $end_point );
|
|
|
|
$request->header(
|
|
|
|
'Authorization' => "Bearer $bearer_token->{access_token}" );
|
|
|
|
$request->header( 'Content-Type' => 'application/json' );
|
|
|
|
|
|
|
|
$request->content(
|
|
|
|
JSON::encode_json(
|
|
|
|
{
|
|
|
|
message => {
|
|
|
|
topic => $validation->param('topic'),
|
|
|
|
notification => {
|
|
|
|
title => $validation->param('sender'),
|
|
|
|
body => $validation->param('messagetext')
|
|
|
|
},
|
|
|
|
webpush => {
|
|
|
|
headers => {
|
|
|
|
urgency => 'very-low'
|
|
|
|
},
|
|
|
|
notification => {
|
|
|
|
title => $validation->param('sender'),
|
|
|
|
body => $validation->param('messagetext'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2020-11-08 15:05:53 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $response = $ua->request($request);
|
2020-11-08 17:35:29 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
if ( $response->is_success ) {
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Your message has been sent successfully!',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
elsif ( $response->is_error ) {
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => [ $response->decoded_content, $jwt, $bearer_token ],
|
|
|
|
error => 'message_error',
|
|
|
|
},
|
|
|
|
status => $response->code,
|
|
|
|
);
|
|
|
|
}
|
2020-11-01 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|