TheGerman

YouMail API - Robocall

Jun 24th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.37 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Product website: https://data.youmail.com/
  4. # Documentation: https://data.youmail.com/documentation
  5. #
  6. # Script revised by freecode768
  7. # https://www.fiverr.com/freecode768
  8.  
  9. use strict;
  10. use warnings;
  11.  
  12. use JSON qw(from_json);
  13. use Asterisk::AGI;
  14. use LWP::UserAgent;
  15.  
  16. # Set configuration parameters
  17. my $api_sid     = "977222...5605";        # UPDATE with your REAL API SID
  18. my $api_token   = "ba2227d...bb7f333380"; # UPDATE with your REAL API TOKEN
  19. my $service_url = "https://dataapi.youmail.com/api/v2/phone/";
  20.  
  21. #############################################################
  22. #
  23. # Get data from Asterisk
  24.  
  25. my $AGI = new Asterisk::AGI;
  26. my $calling_num  = $AGI->get_variable("CALLERID(num)");
  27. my $calling_name = $AGI->get_variable("CALLERID(name)");
  28. my $called_num   = $AGI->get_variable("DID");
  29.  
  30. # Send request to YouMail
  31. my $url = $service_url . $calling_num . "/?format=json&callee=" . $called_num . "&callerId=" . $calling_name;
  32.  
  33. my $content  = "";
  34. my $ua       = LWP::UserAgent->new(agent => "Mozilla/5.0");
  35.  
  36. # authenticate
  37. $ua->default_header( 'DataApiSid' => $api_sid );
  38. $ua->default_header( 'DataApiKey' => $api_token );
  39.  
  40. # make request
  41. my $request = HTTP::Request->new(GET => $url);
  42.  
  43. # get response
  44. my $response = $ua->request($request);
  45.  
  46. if ($response->is_success) {
  47.         $content = $response->decoded_content;
  48. } else {
  49.         die $response->status_line;
  50.         exit 0;
  51. }
  52.  
  53. my $res = from_json($content);    # convert JSON to a Perl data structure
  54. my $statusCode    = $res->{statusCode};
  55. my $recordFound   = $res->{recordFound};
  56. my $phoneNumber   = $res->{phoneNumber};
  57. my $timestamp     = $res->{timestamp};
  58. my $spamRisklevel = $res->{spamRisk}{level};
  59.  
  60. # spamRisk->level:
  61. # 0 = This number appears to be valid (not likely a spammer)
  62. # 1 = This number appears to be a spammer, but the data is inconclusive
  63. # 2 = There is very strong evidence that this number is a spammer
  64.  
  65. # If everything is OK, write results to variables below
  66. $AGI->exec('Set', "UMcode=$statusCode");
  67. $AGI->exec('Set', "UMfound=$recordFound");
  68. $AGI->exec('Set', "UMlevel=$spamRisklevel");
  69. $AGI->exec('Set', "UMnumbr=$phoneNumber");
  70. $AGI->exec('Set', "UMtime=$timestamp");
  71.  
  72. if ($recordFound && ($spamRisklevel eq "1" || $spamRisklevel eq "2")) {
  73.         $AGI->exec('Set', 'SPAMROUTE=HANGUP');
  74. } else {
  75.         $AGI->exec('Set', 'SPAMROUTE=CLEAN');
  76. }
  77.  
  78. exit 0;
Add Comment
Please, Sign In to add comment