Limiting outbound calls

Hello
I have some troubles with making dialplan. I’ve to write dialplan which could make a limit to outbound calls monthly. For example for one trunk every month should wasted no more than 100 minutes.
I know how to make limit for every outbound call

exten => _8777[1235678]XXXXX,n,Set(TRUNK_OPTIONS=${TRUNK_OPTIONS}L(420000[:30000][:15000])) exten => _8777[1235678]XXXXX,n,Set(LIMIT_WARNING_FILE=beep) exten => _8777[1235678]XXXXX,n,Set(LIMIT_TIMEOUT_FILE=custom/vremja-isteklo) exten => _8777[1235678]XXXXX,n,Set(LIMIT_PLAYAUDIO_CALLEE=yes) exten => _8777[1235678]XXXXX,n,Set(LIMIT_PLAYAUDIO_CALLER=yes)

But don’t know how to make it per month?
I think may be GotoIfTime() could help me? But don’t know how to realise. In books there examples of gotoiftime() which can make a call only on working hours e.t.c. But nothing about limiting calls.

I know about module which can limit calls, but it doesn’t work for me.
P.S:Sorry for my poor english xx

If you want to limit the monthly minutes of the outbound calls to 100 minutes. You need a way to measure the minutes. An easy way to achieve this is use MySQL CDR Backend.

could be something like this

exten =>_x.,1,Answer
same=>n,NoOp(${STRFTIME(${EPOCH},%m)}))
same=>n,Set(MONTH=${STRFTIME(${EPOCH},%m)}))
same=>n,NoOp(Current month is ${MONTH})
same=>n,Set(TOTALMINUTE=${SHELL(mysql --user=root --password=‘1edDDS-D’ asterisk -e ‘SELECT sum(billsec) AS totalminute from cdr WHERE disposition = “ANSWERED” AND month( calldate )="${MONTH}" LIMIT 1’)})
same=>n,NoOp(no filter apply ${TOTALMINUTE})
same=>n,NoOp(filter applied ${TOTALMINUTE:12:-1})
same=>n,Set(T=${TOTALMINUTE:12:-1})

same=>n,GotoIf($["${T}"<“100”]?yes:no)
same=>n(yes),Dial(SIP/${EXTEN}@${mytrunk},25)
same=>n,hangup()

same=>n(no),Playback(monthly-minute-exceeded)
same=>n,hangup()

[quote=“ambiorixg12”]If you want to limit the monthly minutes of the outbound calls to 100 minutes. You need a way to measure the minutes. An easy way to achieve this is use MySQL CDR Backend.

could be something like this

exten =>_x.,1,Answer
same=>n,NoOp(${STRFTIME(${EPOCH},%m)}))
same=>n,Set(MONTH=${STRFTIME(${EPOCH},%m)}))
same=>n,NoOp(Current month is ${MONTH})
same=>n,Set(TOTALMINUTE=${SHELL(mysql --user=root --password=‘1edDDS-D’ asterisk -e ‘SELECT sum(billsec) AS totalminute from cdr WHERE disposition = “ANSWERED” AND month( calldate )="${MONTH}" LIMIT 1’)})
same=>n,NoOp(no filter apply ${TOTALMINUTE})
same=>n,NoOp(filter applied ${TOTALMINUTE:12:-1})
same=>n,Set(T=${TOTALMINUTE:12:-1})

same=>n,GotoIf($["${T}"<“100”]?yes:no)
same=>n(yes),Dial(SIP/${EXTEN}@${mytrunk},25)
same=>n,hangup()

same=>n(no),Playback(monthly-minute-exceeded)
same=>n,hangup()[/quote]

Thank you for answer. I have created cdr table in mysql DB. Where information about calls will be written into the table cdr.

I just don’t know how to understand this piece?

same=>n,Set(TOTALMINUTE=${SHELL(mysql --user=root --password=‘1edDDS-D’ asterisk -e ‘SELECT sum(billsec) AS totalminute from cdr WHERE disposition = “ANSWERED” AND month( calldate )="${MONTH}" LIMIT 1’)})

Function shell runs command, in my case we connect to ‘asterisk’ database, select billsec (sum of lengths of all outgoing calls) variable from table ‘cdr’ and equate it to variable totalminute? But what is disposition? I can’t understand this line: WHERE disposition = “ANSWERED” AND month( calldate )="${MONTH}" LIMIT 1’)})

Also I don’t know what for this one?
${TOTALMINUTE:12:-1}. you divide totalminute into 12?

disposition: What happened to the call: ANSWERED, NO ANSWER, BUSY. Check here for CDR fields reference.

SELECT sum(billsec) AS totalminute from cdr WHERE disposition = “ANSWERED” AND month( calldate )="${MONTH}" LIMIT 1’)})

Note this query return the total sum of the minute talked where the call was answered including inbound and outbound calls. You must include your context for outbound calls in the SQL query, in this way you will have just the sum of minute talked on outbound calls.

This ${TOTALMINUTE:12:-1}is just for remove unnecessary fields on the variable, if not it doesn’t work.
Let me know if you need some additional help.

[quote=“ambiorixg12”]
Let me know if you need some additional help.[/quote]

Yep, I think I need help, if it doesn’t bother you :smile:
I write it to the extensions_custom.conf file

exten => _X.,1,Answer() exten => _X.,n,NoOp(${STRFTIME(${EPOCH},,%m)}) exten => _X.,n,Set(MONTH=${STRFTIME({$EPOCH},,%m)}) exten => _X.,n,NoOp(Current month is ${MONTH}) exten => _X.,n,Set(TOTALMINUTE=${SHELL(mysql --user=root --password='123456789' asterisk -e 'SELECT sum('billsec') AS totalminute from cdr WHERE 'disposition' = "ANSWERED" AND month(calldate)="${MONTH}" LIMIT 1')}) exten => _X.,n,NoOp(no filter apply ${TOTALMINUTE}) exten => _X.,n,NoOp(filter applied ${TOTALMINUTE:12:-1}) exten => _X.,n,GotoIf($["${T}"<"100"]?yes:no) exten => _X.,n(yes),Dial(SIP/${EXTEN}@${sipnet},25) exten => _X.,n,Hangup() exten => _X.,n(no),Playback(limit-simul-calls) exten => _X.,n,Hangup()

I don’t know the syntax of shell function, couldn’t find anything helpful in google :frowning:
Only this onehttp://www.voip-info.org/wiki/view/Asterisk+func+shell, but there are no any example of connecting to mysql.
But I think problem isn’t here.

Here is log

== Using SIP RTP TOS bits 184 == Using SIP RTP CoS mark 5 -- Executing [301@from-internal:1] Answer("SIP/302-00000005", "") in new stack -- Executing [301@from-internal:2] NoOp("SIP/302-00000005", "01") in new stack -- Executing [301@from-internal:3] Set("SIP/302-00000005", "MONTH=01") in new stack -- Executing [301@from-internal:4] NoOp("SIP/302-00000005", "Current month is 01") in new stack -- Executing [301@from-internal:5] Set("SIP/302-00000005", "TOTALMINUTE=totalminute 91 ") in new stack -- Executing [301@from-internal:6] NoOp("SIP/302-00000005", "no filter apply totalminute 91 ") in new stack -- Executing [301@from-internal:7] NoOp("SIP/302-00000005", "filter applied 91") in new stack -- Executing [301@from-internal:8] GotoIf("SIP/302-00000005", "1?yes:no") in new stack -- Goto (from-internal,301,9) -- Executing [301@from-internal:9] Dial("SIP/302-00000005", "SIP/301@,25") in new stack == Everyone is busy/congested at this time (1:0/0/1) -- Executing [301@from-internal:10] Hangup("SIP/302-00000005", "") in new stack == Spawn extension (from-internal, 301, 10) exited non-zero on 'SIP/302-00000005' -- Executing [h@from-internal:1] Macro("SIP/302-00000005", "hangupcall") in new stack -- Executing [s@macro-hangupcall:1] GotoIf("SIP/302-00000005", "1?endmixmoncheck") in new stack -- Goto (macro-hangupcall,s,9) -- Executing [s@macro-hangupcall:9] NoOp("SIP/302-00000005", "End of MIXMON check") in new stack -- Executing [s@macro-hangupcall:10] GotoIf("SIP/302-00000005", "1?nomeetmemon") in new stack -- Goto (macro-hangupcall,s,28) -- Executing [s@macro-hangupcall:28] NoOp("SIP/302-00000005", "End of MEETME check") in new stack -- Executing [s@macro-hangupcall:29] GotoIf("SIP/302-00000005", "1?noautomon") in new stack -- Goto (macro-hangupcall,s,34) -- Executing [s@macro-hangupcall:34] NoOp("SIP/302-00000005", "TOUCH_MONITOR_OUTPUT=") in new stack -- Executing [s@macro-hangupcall:35] GotoIf("SIP/302-00000005", "1?noautomon2") in new stack -- Goto (macro-hangupcall,s,41) -- Executing [s@macro-hangupcall:41] NoOp("SIP/302-00000005", "MONITOR_FILENAME=") in new stack -- Executing [s@macro-hangupcall:42] GotoIf("SIP/302-00000005", "1?skiprg") in new stack -- Goto (macro-hangupcall,s,45) -- Executing [s@macro-hangupcall:45] GotoIf("SIP/302-00000005", "1?skipblkvm") in new stack -- Goto (macro-hangupcall,s,48) -- Executing [s@macro-hangupcall:48] GotoIf("SIP/302-00000005", "1?theend") in new stack -- Goto (macro-hangupcall,s,50) -- Executing [s@macro-hangupcall:50] AGI("SIP/302-00000005", "hangup.agi") in new stack -- Launched AGI Script /var/lib/asterisk/agi-bin/hangup.agi -- <SIP/302-00000005>AGI Script hangup.agi completed, returning 0 -- Executing [s@macro-hangupcall:51] Hangup("SIP/302-00000005", "") in new stack == Spawn extension (macro-hangupcall, s, 51) exited non-zero on 'SIP/302-00000005' in macro 'hangupcall' == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/302-00000005' -- Remote UNIX connection -- Remote UNIX connection disconnected dhcp-192-168-168-30*CLI> exit

I see that it found Month correctly Executing [301@from-internal:4] NoOp("SIP/302-00000005", "Current month is 01") in new stack
And it counts totalminute correctly, I presume

Executing [301@from-internal:6] NoOp("SIP/302-00000005", "no filter apply totalminute 91
I think maybe error is here

Maybe trunk name is wrong?
But for now I have only one trunk called "sipnet"
I don’t know what I’m exactly doing wrong, but it doesn’t work.

extensions_custom.conf sounds as you are using FreePBX or Elastix, And I’m not very familiarized with the dial plan, of those products.

I think this is the error exten => _X.,n(yes),Dial(SIP/${EXTEN}@${sipnet},25)

Check the variable ${sipnet} does not exist replace the variable with the trunk name something like :
Dial(SIP/${EXTEN}@sipnet,25)

[quote=“ambiorixg12”]extensions_custom.conf sounds as you are using FreePBX or Elastix, And I’m not very familiarized with the dial plan, of those products. I will share this AGI script .that i wrote It today. It works fine. You can modify it to your own need

[quote]

#!/usr/bin/php -q

<? set_time_limit(30); require('/var/lib/asterisk/agi-bin/phpagi/phpagi.php'); require('conect.php'); //conection to your DB script error_reporting(E_ALL); $agi = new AGI(); $agi->answer(); // $MONTH=date("m/d/y"); date format 02/27/10 $MONTH=date("m"); //we just need the moth for now. $query = mysql_query("SELECT sum(`billsec`) AS totalminute from cdr WHERE `disposition` = 'ANSWERED' AND month( calldate )=$MONTH LIMIT 1 "); if ($query) { if($row = mysql_fetch_array($query)) { $agi->verbose($row['calldate']); $agi->verbose($row['disposition']); if($row['totalminute']>200) { $agi->stream_file("auth-thankyou","#"); $agi->exec_goto('internal','1702',1); // modify this with the context and extension and priority for outbound calls } else { $agi->stream_file("auth-incorrect","#"); $agi->hangup(); } } } mysql_close($con); ?>

[/quote][/quote]

Thank you. Yep, my web interface for asterisk is Freepbx
Well, I’ve never used AGI scripts. Can I use AGI script in freepbx? May be you know some good books about agi scripts?

I think this is the error exten => _X.,n(yes),Dial(SIP/${EXTEN}@${sipnet},25)

Check the variable ${sipnet} does not exist replace the variable with the trunk name something like :
Dial(SIP/${EXTEN}@sipnet,25)

[quote=“ambiorixg12”]extensions_custom.conf sounds as you are using FreePBX or Elastix, And I’m not very familiarized with the dial plan, of those products.

I think this is the error exten => _X.,n(yes),Dial(SIP/${EXTEN}@${sipnet},25)

Check the variable ${sipnet} does not exist replace the variable with the trunk name something like :
Dial(SIP/${EXTEN}@sipnet,25)[/quote]
well, I correct it, now it works. Thank you again! :smile:

:wink:

ambiorixg12, can I ask another question? How can I save IVR information (which ivr number (0,1,2 etc) how many times choosed and by which number) into MYSQL database?
If I will create IVR table into my asterisk database, how can I connect with IVR? It should be something like cdr, but in cdr there are no information about IVR choice, am i right?

There are many ways to write info into a DB using Asterisk. I personally use the PHPAGI class.
You can alter the CDR table and add custom columns. then write the desire info using the Asterisk dial plan something like :

same=>n,Set(CDR(unixtime)=${unixtime})

I wont go into more details because this is a long topic. My suggestion is if you have time enough, please read some books. And once you acquire the necesary knowledge, continue with your project. Other alternative is pay for a consultancy service but this must be post on the Job and Biz section.

Here you have some books. After you read these books you will be able to achieve your project.
asteriskdocs.org/
packtpub.com/asterisk-gatewa … mming/book
phpagi.sourceforge.net/phpagi22/api-docs/

I’m beginner at voip , asterisk etc. I’ve already read series of books asterisk future of telephony and had some online courses. But it seems not enough.
Thank you for books!

Hi!

I want to do exactly the same thing, but just for some numbers.
Whe have a limit on the number of minutes for mobile networks and i want to “disable” calls to mobile numbers when the minutes limit gets close.

I already tested the CDR logs in the database, the math, etc.

Now, in what context should i put the lines you indicate? (i’m using freepbx)

And what about the pattern?
All mobile numbers start with a 9 and are 9 digits long. Should i use “_9XXXXXXXX” ?

And finally in the Dial command, is the a way to tell it to use the “default” route ? (without specifying the SIP/${EXTEN}@trunk)
I would like it to just use the outbound routes defined in freepbx.

Thanks