Asterisk for/while loop to read lines and add them to dial plan

Hi i have asterisk 11 .

i want a custom dial plan that will read Caller IDs from external file in /root/cid.txt
now i have the dial plan works fine when the CIDs i put them my self in the custom file .as below :

[ahmad]
exten => s,1,NoOp(Context : ${CONTEXT} : “Heyyyyy you are in custom”)
exten => s,n,GotoIf($["${CALLERID(num)}" = “2832930”]?next:end)
exten => s,n,GotoIf($["${CALLERID(num)}" = “2832940”]?next:end)
exten => s,n,GotoIf($["${CALLERID(num)}" = “2832950”]?next:end)
exten => s,n,GotoIf($["${CALLERID(num)}" = “2832960”]?next:end)
exten => s,n(next),goto(from-internal,30,1)
exten => s,n(end),system(echo “${DATETIME} - ${CALLERID} - ${CHANNEL}” >> /var/log/asterisk/calls)

as we see i put the CID myself —> 2832930" ,2832940 2832940 2832950 28329360.

so what i need is i want to create a file /root/cid.txt that will be updated dynamically from outside .

and i want to have a for loop or while loop in asterisk that will sweep all the lines line by line and create the rules automatically .

so if there was 1 line in the file /var/log/asterisk/calls with 1 CID like 123456

my dial plan should be equivalent to below :

[ahmad]
exten => s,1,NoOp(Context : ${CONTEXT} : “Heyyyyy you are in custom”)
exten => s,n,GotoIf($["${CALLERID(num)}" = “123456”]?next:end)
exten => s,n(next),goto(from-internal,30,1)
exten => s,n(end),system(echo “${DATETIME} - ${CALLERID} - ${CHANNEL}” >> /var/log/asterisk/calls)

if i had 2 CID ( 2 lines in the file/var/log/asterisk/calls) —> , 123456 & 123455 , then it should be equivalent to

[ahmad]
exten => s,1,NoOp(Context : ${CONTEXT} : “Heyyyyy you are in custom”)
exten => s,n,GotoIf($["${CALLERID(num)}" = “123456”]?next:end)
exten => s,n,GotoIf($["${CALLERID(num)}" = “123455”]?next:end)
exten => s,n(next),goto(from-internal,30,1)
exten => s,n(end),system(echo “${DATETIME} - ${CALLERID} - ${CHANNEL}” >> /var/log/asterisk/calls)

so again
all i need is just replace adding the CID rules with 1 single rule that will automatically sweep the external file lines.

thank you

Your 2nd GotoIf will never trigger as your first one is skipping it by jumping to the end label.

exten => s,1,NoOp(Context : ${CONTEXT} : "Heyyyyy you are in custom")
exten => s,n,GotoIf($["${CALLERID(num)}" = "123456"]?next:end)
exten => s,n,GotoIf($["${CALLERID(num)}" = "123455"]?next:end)
exten => s,n(next),goto(from-internal,30,1)

You can fix that by changing your GotoIf Logic like below:

exten => s,1,NoOp(Context : ${CONTEXT} : "Heyyyyy you are in custom")
exten => s,n,GotoIf($["${CALLERID(num)}" = "123456"]?next)
exten => s,n,GotoIf($["${CALLERID(num)}" = "123455"]?next)
exten => s,n,Goto(end)
exten => s,n(next),goto(from-internal,30,1)
exten => s,n(end),system(echo "${DATETIME} - ${CALLERID} - ${CHANNEL}" >> /var/log/asterisk/calls)

That said I still have no idea what you are trying to attempt or what you mean by ‘sweep the external file lines’

hi thanks for reply .

again

say i want to do a rule for 1000 CID numbers .
i will be hard to do them manually one by one .

so i will out the 1000 CID in 1 file
and let asterisk check if the CID is one if the CID inside that file , then go to the rule
exten => s,n(next),goto(from-internal,30,1)

else it will go to the rule

exten => s,n(end),system(echo “${DATETIME} - ${CALLERID} - ${CHANNEL}” >> /var/log/asterisk/calls)

clear ???
i don’t want to create 100 rule lines because the CID file will be changed many times and i don’t want to change the config each time myself

its like doing it dynamically

only file will be charged and i want others stuff be transparent there

cheers

I think it would be much better to use a database for this task, either the internal astdb or a external database via ODBC.

1 Like

thank you

say that i have a db with data

the question here is

how do i query all the entries of the CIDS and with into my dial plan ?

can you give me sone hints that would help me to return the values in AGI or any other forms ?

Something like

exten => s,1,NoOp(Context : ${CONTEXT} : "Heyyyyy you are in custom")
exten => s,n,GotoIf(${DB(whitelist/${CALLERID(num)})}?from-internal,30,1:end)
exten => s,n(end),system(echo "${DATETIME} - ${CALLERID} - ${CHANNEL}" >> /var/log/asterisk/calls)

Then just insert the records like:

database put whitelist 2832930 1
database put whitelist 2832940 1
database put whitelist 2832950 1

And on Hangup of an incoming call you could create an H exten to choose to add the current Callerid to blacklist by pressing a key…

I have only heard of “h” extensions. h extensions are run after the channel technology driver has been hung up and cannot reliably access media.

For those who come here for finding loop command like me.

Latest versions (16+) supports While and EndWhile
https://docs.asterisk.org/Asterisk_16_Documentation/API_Documentation/Dialplan_Applications/While