Add API message function
This commit is contained in:
parent
f57dc63277
commit
279127a6d4
2 changed files with 74 additions and 1 deletions
|
@ -141,8 +141,8 @@ sub startup {
|
|||
$api_public->post('/register')->to('api-register#post_register');
|
||||
$api_public->post('/logout')->to('api-auth#post_logout');
|
||||
$api_public->post('/feedback')->to('api-feedback#post_feedback');
|
||||
$api_public->post('/add-device-token')->to('api-devices#add_token');
|
||||
$api_public->post('/send-message')->to('api-sendmessage#post_message');
|
||||
#$api_public->post('/send-message')->to('api-sendmessage#post_message');
|
||||
|
||||
# Private, must be authenticated api routes
|
||||
my $api = $api_public->under('/')->to('api-auth#auth');
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package Pear::LocalLoop::Controller::Api::Sendmessage;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
use LWP::UserAgent;
|
||||
use JSON;
|
||||
use Mojo::JWT;
|
||||
use Mojo::File;
|
||||
use Carp;
|
||||
|
||||
has error_messages => sub {
|
||||
return {
|
||||
|
@ -13,6 +18,46 @@ has error_messages => sub {
|
|||
};
|
||||
};
|
||||
|
||||
=begin comment
|
||||
Credit: Peter Scott/StackOverflow
|
||||
https://stackoverflow.com/a/53357961/4580273
|
||||
Credit: jeffez/StackOverflow
|
||||
https://stackoverflow.com/q/56556438/4580273
|
||||
=cut
|
||||
|
||||
my $jwt = create_jwt_from_path_and_scopes('./localspend-47012.json', 'email https://www.googleapis.com/auth/compute');
|
||||
my $ua = LWP::UserAgent->new();
|
||||
|
||||
my $bearer_token = $ua->post('https://www.googleapis.com/oauth2/v4/token',
|
||||
{
|
||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
||||
'assertion' => $jwt
|
||||
}
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sub post_message {
|
||||
my $c = shift;
|
||||
|
||||
|
@ -27,6 +72,34 @@ sub post_message {
|
|||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
my $user = $user_rs->find({'email' => $validation->param('email')});
|
||||
|
||||
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");
|
||||
$request->header('Content-Type' => 'application/json');
|
||||
|
||||
$request->content(JSON::encode_json ({
|
||||
message => {
|
||||
token => $user->param('token'),
|
||||
notification => {
|
||||
title => 'test',
|
||||
body => 'test content'
|
||||
},
|
||||
webpush => {
|
||||
headers => {
|
||||
Urgency => 'high'
|
||||
},
|
||||
notification => {
|
||||
body => 'test content',
|
||||
requireInteraction => 'true'
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
$ua->request($request);
|
||||
|
||||
=begin comment
|
||||
$c->schema->resultset('Feedback')->create({
|
||||
user => $user,
|
||||
|
|
Reference in a new issue