sharee.bike/copri4/main/src/Mod/SMSTransport.pm

132 lines
3 KiB
Perl
Raw Normal View History

2021-12-30 12:05:56 +01:00
package SMSTransport;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#SMS sender
#
#perl -cw do
#use lib "/var/www/copri4/shareeapp-primary/src";
use strict;
use warnings;
use POSIX;
use CGI; # only for debugging
use LWP::UserAgent;
use URI::Encode;
use JSON;
use Scalar::Util qw(looks_like_number);
use Mod::DBtank;
use Mod::Basework;
use Data::Dumper;
my $q = new CGI;
my $dbt = new DBtank;
my $bw = new Basework;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $ua = LWP::UserAgent->new;
$ua->agent("sharee smsclient");
my $uri_encode = URI::Encode->new( { encode_reserved => 1 } );
my $json = JSON->new->allow_nonref;
my $response_in = {};
my $dbh = "";
my $owner = 181;
sub sms_ack_digest {
my $self = shift;
my $ctadr = shift;
#Ack digest
my $ack_digest = $q->unescapeHTML($ctadr->{txt34}) || "";
my $email_ack_digest = $1 if($ack_digest =~ /^(.{5})/);
my $sms_ack_digest = $1 if($ack_digest =~ /(.{5})$/);
my $sms_from = "Mietradcode";
2021-12-30 12:05:56 +01:00
my $sms_to = $ctadr->{txt07};# || "+491799xxxx72";
2022-01-10 12:33:49 +01:00
if($ctadr->{txt07} =~ /^0/ || $ctadr->{txt07} !~ /\+[1-9]/){
my $sms_tosub = $ctadr->{txt07};
$sms_tosub =~ s/^00/\+/g;
$sms_tosub =~ s/^0/\+49/g;
$sms_to = $sms_tosub;
}
2021-12-30 12:05:56 +01:00
my $sms_message = "Ihr Mietradsystem SMS-Bestätigungscode lautet: $sms_ack_digest";
my $message = Encode::encode('iso-8859-1', Encode::decode('utf-8',"$sms_message"));
open(FILE,">>$dbt->{copri_conf}->{logdir}/sms_gtx.log");
print FILE "\n*** $now_dt 'sharee smsclient' \n";
my $request = {
from => $sms_from,
to => $sms_to,
text => $message,
};
print FILE "---> request:\n" . Dumper($request);
my $ret_json = $self->get_sms_gtx($request);
eval {
$response_in = decode_json($ret_json);
print FILE "<--- response_in:\n" . Dumper($response_in);
};
if ($@){
print FILE "<--- failure raw response_in:\n" . Dumper($ret_json) . "\n";
warn $@;
}
close(FILE);
return $ret_json;
}
#sms gtx http request
sub get_sms_gtx {
my $self = shift;
my $request = shift || "";
my $api_file = "/var/www/copri4/shareeconf/apikeys.cfg";
my $aconf = Config::General->new($api_file);
my %apikeyconf = $aconf->getall;
#print $apikeyconf{smsgtx}->{gtx_key};
my $endpoint = "https://rest.gtx-messaging.net/smsc/sendsms/$apikeyconf{smsgtx}->{gtx_key}/json";
my $rest;
foreach (keys (%$request)){
my $encoded_val = $uri_encode->encode($request->{$_});
$rest .= "$_=$encoded_val&";
}
$rest =~ s/\&$//;
my $gtx_request = "$endpoint?$rest";
print FILE "===> GET2gtx >> " . $gtx_request . "\n";
my $req = HTTP::Request->new(GET => "$gtx_request");
$req->content_type('application/x-www-form-urlencoded');
my $res = $ua->request($req);
if ($res->is_success) {
#print $res->content;
return $res->content;
print $res->status_line, "\n";
}else {
print $res->status_line, "\n";
}
}
#
1;