TheGerman

Twilio API - TrueSpam by TrueCNAM (perl)

Jun 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.58 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Twilio API - TrueSpam by TrueCNAM
  4. # Cost: US$0.003 per request
  5. # Identify robocalls, telemarketers, scams and other unwanted calls
  6. # TrueSpam scores indicate the likelihood that an incoming call is
  7. # from a robocaller, telemarketer, scammer, or other unwanted caller.
  8.  
  9. # Multiple sources are utilized to identify unwanted calls quickly
  10. # and accurately including: real-time traffic flows, a large honeypot
  11. # network, network deployed CAPTCHA's, feedback reports from
  12. # end-users, public and private government data, and other methods.
  13.  
  14. # The TrueSpam service is trusted and deployed in carrier networks
  15. # throughout North America with established ILECs, CLECs, and VoIP
  16. # providers.
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. use JSON qw(from_json);
  22. use Asterisk::AGI;
  23. use LWP::UserAgent;
  24.  
  25. # Set configuration parameters
  26. my $api_sid     = "XYZ";
  27. my $api_token   = "XYZ";
  28. my $service_url = "https://lookups.twilio.com/v1/PhoneNumbers/+1";
  29. my $api_name    = "truecnam_truespam";
  30. my $voicemail   = 40;
  31. my $disconnect  = 80;
  32.  
  33. #############################################################
  34. #
  35. # Get data from Asterisk
  36. #
  37.  
  38. my $AGI         = new Asterisk::AGI;
  39. my $calling_num = $AGI->get_variable("cidnum");
  40. my $called_num  = $AGI->get_variable("callednum");
  41.  
  42. # Send request to Twilio API
  43.  
  44. my $url = $service_url . $calling_num . "/?AddOns=" . $api_name;
  45.  
  46. my $content  = "";
  47. my $ua       = LWP::UserAgent->new(agent => "Mozilla/5.0");
  48.  
  49. # make request
  50. my $request = HTTP::Request->new(GET => $url);
  51.  
  52. # authenticate
  53. $request->authorization_basic($api_sid, $api_token);
  54.  
  55. # get response
  56. my $response = $ua->request($request);
  57.  
  58. if ($response->is_success) {
  59.             $content = $response->decoded_content;
  60.     }
  61.     else {
  62.                 die $response->status_line;
  63.                 exit 0;
  64.     }
  65.  
  66. my $res = from_json($content);    # convert JSON to a Perl data structure
  67.  
  68. my $risk_match  = $res->{add_ons}{results}{truecnam_truespam}{result}{spam_score_match};
  69. my $risk_level  = $res->{add_ons}{results}{truecnam_truespam}{result}{spam_score};
  70. my $risk_status = $res->{add_ons}{results}{truecnam_truespam}{status};
  71. my $risk_sid    = $res->{add_ons}{results}{truecnam_truespam}{request_sid};
  72.  
  73. # If everything is OK, write results to variables below
  74.  
  75. $AGI->exec('Set', "TSmatch=$risk_match");
  76. $AGI->exec('Set', "TSscore=$risk_level");
  77. $AGI->exec('Set', "TSstatus=$risk_status");
  78. $AGI->exec('Set', "TSsid=$risk_sid");
  79.  
  80. if ($risk_level >= $disconnect) {
  81.         $AGI->exec('Set', 'SPAMROUTE=HANGUP');
  82. } else {
  83.         $AGI->exec('Set', 'SPAMROUTE=PASS');
  84. }
  85.  
  86. exit 0;
Add Comment
Please, Sign In to add comment