Advertisement
TheGerman

TrueCNAM Perl example for use with Asterisk

Oct 15th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.61 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Based on a public forum post - https://www.dslreports.com/forum/r30960612-
  4.  
  5. use Asterisk::AGI;
  6. use LWP::UserAgent;
  7.  
  8. use strict;
  9.  
  10. # Set configuration parameters
  11. my $api_key = "YourAPIkey";
  12. my $api_password = "YourAPIpassword";
  13. my $service_url = "https://api.truecnam.net/api/v1";
  14. my $voicemail_on_score = 40;
  15. my $disconnect_on_score = 80;
  16.  
  17. #############################################################
  18. #
  19. # Get data from Asterisk
  20.  
  21. my $AGI = new Asterisk::AGI;
  22. my $calling_num = $AGI->get_variable("cidnum");
  23. my $called_num = $AGI->get_variable("callednum");
  24.  
  25. # Send request to TrueCNAM
  26.  
  27. my $url = $service_url."?username=".$api_key."&password=".$api_password."&resp_type=extended&resp_format=csv&calling_number=".$calling_num."&call_party=terminating";
  28. $url .= "&called_number=".$called_num if ($called_num);
  29. my $content = "";
  30. my $ua = LWP::UserAgent->new;
  31. my $response = $ua->get($url);
  32.  
  33. if ($response->is_success) {
  34.         $content = $response->decoded_content;
  35.         #print $response->decoded_content . "\n";  # or whatever
  36. } else {
  37.         #die $response->status_line;
  38.         exit 0;
  39. }
  40.  
  41. my @res = split(",", $content);
  42.  
  43. # Check for error
  44. if ($res[11]) {
  45.         exit 0;
  46. }
  47.  
  48. # Check for TrueSPAM
  49. if (!$res[7]) {
  50.         exit 0;
  51. }
  52.  
  53. $AGI->exec('Set', 'SPAMSCORE=');
  54.  
  55.  
  56. if ($res[8] >= $disconnect_on_score) {
  57.         $AGI->exec('Set', 'SPAMROUTE=HANGUP');
  58.         exit 0;
  59. } elsif ($res[8] >= $voicemail_on_score) {
  60.         $AGI->exec('Set', 'SPAMROUTE=VOICEMAIL');
  61.         exit 0;
  62. }
  63.  
  64. print "Low score\n";
  65.  
  66. $AGI->exec('Set', 'SPAMROUTE=CLEAN');
  67.  
  68. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement