/
sms_listen.pl
executable file
·127 lines (116 loc) · 3.24 KB
/
sms_listen.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env perl
use strict;
use warnings;
use C4::SMS;
use C4::Auth;
use Koha;
use C4::Context;
use C4::Members;
use C4::Circulation;
my ($res,$ua);
my %commands;
my $message;
my $phone;
my $result;
my $errorcode;
my $smsid;
my $wait=600;## 10 mn. wait between sms checking
my $dbh=C4::Context->dbh;
STARTAGAIN:
($res,$ua)=get_sms_auth();
AGAIN:
$errorcode=0;
if ($res->{pRetCode}==200){
$result=read_sms($ua,$res->{pSessionId});
$errorcode=$result->{pErrCode};
print "connected\n";
} else {
kill_sms($ua,$res->{pSessionId});
warn (error_codes($res->{pErrCode}),$res->{pErrcode}) ;
# sleep $wait;
goto FINISH;
}
if ($errorcode && $errorcode !=-9005){
kill_sms($ua,$res->{pSessionId});
warn error_codes($errorcode) ;
# sleep $wait;
goto FINISH;
} elsif ($errorcode ==-9005){
print "no more messages to read\n";
goto WAITING;
}
#Parse the message to a useful hash
my @action=parse_message( $result->{pContent});
## Log the request in our database;
$smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
print "message logged\n";
##Now do the service required
if (uc($action[0]) eq "RN"){
print "dealing request\n";
my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
unless ($ok) {
##wrong user/pass
$message="Yanlis kullanici/sifre! :Wrong username/password!";
my $send=send_message($result,$message,$smsid);
goto AGAIN;
}
my $item=getiteminformation(undef,0,$action[3]);
if ($item){
my $borrower=getmember($cardnumber);
my $status=renewstatus(undef,$borrower->{borrowernumber},$item->{itemnumber});
if ($status==1) {
my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
$message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
} elsif($status==2) {
$message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
} elsif($status==3) {
$message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
} elsif($status==4) {
$message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
} elsif($status==0) {
$message="Uzatilamaz! :Can not renew:".$item->{barcode};
}
} else {
$message="Yanlis barkot! :Wrong barcode!";
}
} else {
## reply about error
$message="Yanlis mesaj formati! :Wrong message! :
RN usercardno password barcode";
} ### wrong service
send_message($result,$message,$smsid);
goto AGAIN;
WAITING:
##Now send the messages waiting in queue
my $smssth=$dbh->prepare("SELECT smsid,user_phone,message from sms_messages where date_replied like '0000-00-00%' ");
$smssth->execute();
my @phones;
while (my $data=$smssth->fetchrow_hashref){
push @phones,$data;
}
$smssth->finish;
foreach my $user(@phones){
print "replying $user->{user_phone}";
my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
my $reply="--failed\n";
if ($send->{pRetCode}==200){
$reply= "--replied\n";
mod_sms($user->{smsid},"Sent");
}
print $reply;
}
$dbh->disconnect;
sub send_message {
my ($mes,$message,$smsid)=@_;
my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
if ($send->{pRetCode}==200){
mod_sms($smsid,$message);
} else {
my $error=error_codes($send->{pErrCode});
mod_sms($smsid,"Not replied error:".$error);
}
return $send;
}
FINISH:
1;
__END__