IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Good code yeh?

Channel Check · May 16, 09:55 by Shannon Wynter

Update: Shortly after showing this to some asterisk people (the same ones I asked last night for assistance in this matter) they showed me an easier way Snippits / Channel Check 2.0

The code below still perfectly usefull if you want to check more then a few extensions.


I can’t belive that this isn’t built into Asterisk – I can’t possibly be the only person in the world that wants to be able to ring multipul extensions except when one is busy (not just off hook)

It took some hunting around, and a great deal of source searching but I’ve done it.

First I need to get a list of open channels in order to work out which ones are busy

This is a library file, I keep it in /var/lib/asterisk/perl/apps/lib

  1. sub get_chanlist {
  2. # Open a purely random file...
  3. open(TMP, "+>", undef) or die($!);
  4. # Fill it  up with data
  5. $meh = asterisk_chanlist(fileno(TMP));
  6. # GO back to start of file
  7. seek(TMP,0,0);
  8.  
  9. # I'm being slack here, load it all (No biggy, only 2 lines after all)
  10. my @Returned = <TMP>;
  11. close(TMP);
  12.  
  13. # Get the list of field names from the first record
  14. my @Keys = ($Returned[0] =~ /^\s+(\S+)\s+\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\)\s+(\S+)\s+(\S+).\s+(\S.+)\r?\n/);
  15.  
  16. shift(@Returned); pop(@Returned); # We don't want the first or last record
  17.  
  18. my @Records = ();
  19.  
  20. foreach $Record (@Returned) {
  21.   if (@Row = ($Record =~ /^\s+(\S+)\s+\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\)\s+(\S+)\s+(\S+)\s+(\S.+)\r?\n/)) {
  22.   my %hash;
  23.   @hash{@Keys} = @Row;
  24.   push(@Records, \%hash);
  25.   }
  26.     }
  27.  
  28. return @Records;
  29. }
  30.  
  31. 1;
  32. Download this code: get_chanlist.pl (Downloaded 169 time(s))

Then we have the res_perl application “chancheck.pl” (Yeh, I probably should renabe it to something more specific)

  1. package Asterisk::Embed;
  2.  
  3. push(@INC,"/var/lib/asterisk/perl/apps/lib");
  4.  
  5. require("get_chanlist.pl");
  6.  
  7. sub {
  8. my $chan_name = shift;
  9. my $chan = asterisk_get_channel_by_name($chan_name);
  10. my @chans = split(/\&/,shift);
  11.  
  12. asterisk_log("LOG_NOTICE","chancheck.pl: Checking for busy extensions (".join(', ',@chans).")\n");
  13.  
  14. my @Channels = &get_chanlist();
  15.  
  16. my $inUse = 0;
  17. foreach $Chan (@Channels) {
  18.   foreach $CheckChan (@chans) {
  19.   if ($Chan->{Channel} =~ /^\Q$CheckChan\E/i) {
  20.     $inUse++;
  21.   }
  22.   }
  23. }
  24.  
  25. if ($inUse > 0) {
  26.   asterisk_log("LOG_NOTICE","chancheck.pl: Found busy extensions\n");
  27.   asterisk_setvariable($chan,'CHANCHECK','BUSY');
  28. } else {
  29.   asterisk_log("LOG_NOTICE","chancheck.pl: All extensions free\n");
  30.   asterisk_setvariable($chan,'CHANCHECK','AVAIL');
  31. }
  32. }
  33.  
  34. Download this code: chancheck.pl (Downloaded 159 time(s))

You call this function with the same params you call your dial() with
ie: tech/ext&tech/ext&tech/ext etc

Sample code:

  1. IPDial=sip/9100&sip/9101
  2. IPVoicemail=9100@default
  3.  
  4. ; Tell our user to hold
  5. exten => s,1,Playback(pls-wait-connect-call)
  6. ; Check our lines (sip/9100&sip/9101)
  7. exten => s,2,Perl(LoadFile:chancheck.pl:${IPDial})
  8. exten => s,3,Goto(${CHANCHECK},s,1);
  9.  
  10. [AVAIL] ; $CHANCHECK
  11. ; Dial the extensions - we use music on hold here
  12. exten => s,1,Dial(${IPDial},30,tm(default)hH)
  13. exten => s,2,goto(${DIALSTATUS},s,2)
  14.  
  15. [BUSY] ; $CHANCHECK & $DIALSTATUS
  16. exten => s,1,Playback(the-party-you-are-calling)
  17. exten => s,2,Playback(vm-isonphone)
  18. exten => s,3,Goto(LEAVEAMSG,s,1)
  19.  
  20. [CHANUNAVAIL] ; $DIALSTATUS
  21. exten => s,1,NoOp
  22. exten => s,2,Playback(cannot-complete-temp-error)
  23. exten => s,3,Goto(LEAVEAMSG,s,1)
  24.  
  25. [NOANSWER] ; $DIALSTATUS
  26. exten => s,1,Playback(away-naughty-boy)
  27. exten => s,2,Goto(LEAVEAMSG,s,1)
  28.  
  29. [LEAVEAMSG]
  30. exten => s,1,DigitTimeout,5
  31. exten => s,2,ResponseTimeout,10
  32. exten => s,3,Background(T-to-leave-msg)
  33. exten => s,4,Background(press-1)
  34.  
  35. ; Press 1
  36. exten => 1,1,Voicemail(${IPVoicemail})
  37.  
  38. ; Timeout - If they take too long, give up
  39. exten => t,1,Playback(thank-you-for-calling)
  40. exten => t,2,Playback(goodbye)
  41. exten => t,3,Wait(2) ; Sometimes goodbye gets cut off
  42. exten => t,4,Hangup
  43.  
  44. exten => i,1,Playback(invalid) ; "That's not valid, try again"
  45.  
  46. Download this code: chancheck.conf (Downloaded 141 time(s))
Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==---