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
2022-10-05 12:36:02 +02:00
#use lib "/var/www/copri-bike/shareeapp-primary/src";
2021-12-30 12:05:56 +01:00
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 ) ;
2023-04-05 07:41:11 +02:00
use Lib::Config ;
2021-12-30 12:05:56 +01:00
use Mod::DBtank ;
use Mod::Basework ;
use Data::Dumper ;
my $ q = new CGI ;
2023-04-05 07:41:11 +02:00
my $ cf = new Config ;
2021-12-30 12:05:56 +01:00
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 ;
2022-03-15 13:05:21 +01:00
#used for sending smsAck codes to user
2021-12-30 12:05:56 +01:00
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})$/ ) ;
2022-01-04 11:49:13 +01:00
my $ sms_from = "Mietradcode" ;
2021-12-30 12:05:56 +01:00
my $ sms_to = $ ctadr - > { txt07 } ; # || "+491799xxxx72";
2022-01-14 10:49:45 +01:00
my $ sms_message = "" ;
#goes to test if devel or joke bsp nr
if ( $ dbt - > { copri_conf } - > { stage } eq "test" || $ ctadr - > { txt07 } =~ /17012345678/ ) {
$ ctadr - > { txt07 } = "$dbt->{copri_conf}->{sms_to}" ;
$ sms_message = "* offline Test *" ;
}
2022-01-10 12:33:49 +01:00
if ( $ ctadr - > { txt07 } =~ /^0/ || $ ctadr - > { txt07 } !~ /\+[1-9]/ ) {
my $ sms_tosub = $ ctadr - > { txt07 } ;
2022-03-01 10:25:13 +01:00
$ sms_tosub =~ s/^\+00/\+/ ;
$ sms_tosub =~ s/^00/\+/ ;
$ sms_tosub =~ s/^0/\+49/ ;
2022-01-10 12:33:49 +01:00
$ sms_to = $ sms_tosub ;
}
2022-01-14 10:49:45 +01:00
$ sms_message . = "Ihr Mietradsystem SMS-Bestätigungscode lautet: $sms_ack_digest" ;
2021-12-30 12:05:56 +01:00
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 ;
}
2022-03-15 13:05:21 +01:00
#used for sending message to user
2022-03-07 17:14:24 +01:00
sub sms_message {
my $ self = shift ;
2022-03-14 17:37:49 +01:00
my $ todo = shift ;
2023-04-05 07:41:11 +02:00
my $ contact_hotline = shift ;
2022-03-07 17:14:24 +01:00
my $ sms_to = shift ;
my $ bike = shift ;
2023-04-05 07:41:11 +02:00
my % varenv = $ cf - > envonline ( ) ;
2022-03-07 17:14:24 +01:00
my $ sms_from = "Mietradinfo" ;
my $ sms_message = "" ;
#goes to test if devel or joke bsp nr
if ( $ dbt - > { copri_conf } - > { stage } eq "test" || $ sms_to =~ /17012345678/ ) {
$ sms_to = "$dbt->{copri_conf}->{sms_to}" ;
$ sms_message = "* offline Test *" ;
}
if ( $ sms_to =~ /^0/ || $ sms_to !~ /\+[1-9]/ ) {
my $ sms_tosub = $ sms_to ;
$ sms_tosub =~ s/^\+00/\+/ ;
$ sms_tosub =~ s/^00/\+/ ;
$ sms_tosub =~ s/^0/\+49/ ;
$ sms_to = $ sms_tosub ;
}
2022-10-05 12:36:02 +02:00
#todo locking_progress
2022-03-14 17:37:49 +01:00
if ( $ todo eq "locking_progress" ) {
2023-04-05 07:41:11 +02:00
$ sms_message . = "Ihre Miete von $bike wurde nicht beendet! Ihre kostenpflichtige Miete läuft weiter! Stellen Sie sicher, dass das Schloss geschlossen ist. $contact_hotline" ;
2022-03-14 17:37:49 +01:00
}
2022-10-05 12:36:02 +02:00
#todo if > 24h rental
2022-03-14 17:37:49 +01:00
if ( $ todo eq "24h_occupied" ) {
2023-04-05 07:41:11 +02:00
$ sms_message . = "Zur Info! Sie haben $bike seit 24 Stunden kostenpflichtig gemietet!. $contact_hotline" ;
2022-03-14 17:37:49 +01:00
}
if ( $ todo eq "48h_occupied" ) {
2023-04-05 07:41:11 +02:00
$ sms_message . = "Zur Info! Sie haben $bike seit 48 Stunden kostenpflichtig gemietet!. $contact_hotline" ;
2022-03-14 17:37:49 +01:00
}
if ( $ todo eq "72h_occupied" ) {
2023-04-05 07:41:11 +02:00
$ sms_message . = "Zur Info! Sie haben $bike seit 72 Stunden kostenpflichtig gemietet!. $contact_hotline" ;
}
#todo send_alarm2hotline
if ( $ todo eq "send_alarm2hotline" ) {
$ sms_from = "RadAlarm" ;
2023-06-01 07:50:17 +02:00
$ sms_message . = "Alarmmeldung zu Rad $bike. Öffne das Alarmjournal für weitere Informationen: $dbt->{operator}->{$varenv{dbname}}->{operatorDMS}/DMS/Alarmjournal" ;
2022-03-14 17:37:49 +01:00
}
2022-10-05 12:36:02 +02:00
#todo fraud_rental
if ( $ todo eq "fraud_rental" ) {
$ sms_message . = "Miete von einem Nutzer der als Betrugsfall hinterlegt ist! Mietrad $bike " ;
}
2022-03-07 17:14:24 +01:00
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 ) ;
2022-03-14 17:37:49 +01:00
#my $ret_json = "";#$self->get_sms_gtx($request);#for testing without send
2022-03-07 17:14:24 +01:00
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 ;
}
2021-12-30 12:05:56 +01:00
#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 ;