Exten => create, write and read a TEMP file

Hello,

I have created a script in extensions_custom.conf which on incoming calls is making a query to a remote web server based on the incoming CallerId Number, then the web server return the corresponding Name.
I want to write the corresponding Name in a temp file, then set the Name
as the CallerID Name in such a way to appear on the screen of the endpoints.
The challenge is to write, read and delete the temp file.

Here is the script:
[temp-file]
exten => s,1,NoOp(Entering user defined context [temp-file] in extensions_custom.conf)
exten => s,2,System(tmpfile=$(mktemp /tmp/abc-script.XXXXXX))
exten => s,3,NoOp(tmpfile=${tmpfile})
exten => s,4,System(/etc/asterisk/modulix2)
exten => s,5,Set(CALLERID(name)=${tmpfile})
exten => s,5,Dial(SIP/202)

In “modulix2”, which is an exe file, I have a curl -X Get command which returns the Name of the caller.
I can send the result of “modulix2” in a already created file, but not in a “/tmp/abc-script.XXXXXX” file and then to read and delete /tmp/abc-script.XXXXXX file.

Do you have any idea how to do it?

Why are you making a file that has this information and then deleting the file? Why not use AGI so you can make the db call and just return and set the CallerID Name? You could also do direct SQL calls or cURL calls from the dialplan and get this information returned.

… or you can use the function in the CallerID Superfecta that already does this?

2 Likes

seconded. This is how I would do it.

https://wiki.freepbx.org/display/FPG/CID+Superfecta+User+Guide

1 Like

If you must customize, perhaps start from something like:

exten => s,1,Set(CALLERID(name)=${CURL(https://mydomain.com/lookupname?num=${CALLERID(num)})})
exten => s,2,Dial(SIP/202)
2 Likes

Hello,
Thanks to everybody for reply.
I have tried with CID superfecta but it didn’t worked.
This is the curl command in “modulix2” and this suppose to be addapted to CID Superfecta, but I did not succeded. maybe you can have an idea:

curl -X GET ‘https://www.domain.com/api/integrations/pbx/public/newCall?NumberTo=0123456789&NumberFrom=${CALLERID(number)}
-H ‘Accept: /
-H ‘Accept-Encoding: gzip, deflate’
-H ‘Cache-Control: no-cache’
-H ‘Connection: keep-alive’
-H ‘Cookie: MSAPI=xxxxxxxxxxxxxxxxxxxxxxxxxx’
-H ‘Host: www.domain.com’ -H ‘User-Agent: PostmanRuntime/7.17.1’
-H ‘X-API-KEY: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy’
-H ‘cache-control: no-cache’

[Stewart1] meanwhile, I will try your script to see the results

Thank you

I am doing with curl. This is the only way I can interact with the remote database.
I did not tried with AGI.

I’ll try your script and I’ll come back with a feedback

Thx

Hi,

this command is executed and is it working if I make the script in this way:

exten => s,n, System ( curl -X GET ‘https://www.domain.com/api/integrations/pbx/public/newCall?NumberTo=0123456789&NumberFrom=${CALLERID(number)}
-H ‘Accept: /
-H ‘Accept-Encoding: gzip, deflate’
-H ‘Cache-Control: no-cache’
-H ‘Connection: keep-alive’
-H ‘Cookie: MSAPI=xxxxxxxxxxxxxxxxxxxxxxxxxx’
-H ‘Host: www.domain.com’ -H ‘User-Agent: PostmanRuntime/7.17.1’
-H ‘X-API-KEY: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy’
-H ‘cache-control: no-cache’)

but in this way this is not working:

exten =s,n,Set(CALLERID(name)=${curl -X GET ‘https://www.domain.com/api/integrations/pbx/public/newCall?NumberTo=0123456789&NumberFrom=${CALLERID(number)}
-H ‘Accept: /
-H ‘Accept-Encoding: gzip, deflate’
-H ‘Cache-Control: no-cache’
-H ‘Connection: keep-alive’
-H ‘Cookie: MSAPI=xxxxxxxxxxxxxxxxxxxxxxxxxx’
-H ‘Host: www.domain.com’ -H ‘User-Agent: PostmanRuntime/7.17.1’
-H ‘X-API-KEY: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy’
-H ‘cache-control: no-cache’})

the curl -X command it’s not executed.

I cannot set CallerID(name) = ${curl -X …}.
What’s wrong?

Because you’re using cURL wrong in Asterisk. There are actual cURL functions in Asterisk to set the URL, auth and payload along with setting the various cURL options like content-type, etc. You just can’t call curl like you would from the CLI.

System() is going to execute the script at the system level. There you can make your standard cURL calls and do what you where doing. You now need to get that information back into Asterisk and the dialplan.

AGI() will let you jump out of dialplan, execute the script and let you inject or manipulate the dialplan variables, commands, functions.

The cURL functions will let you make that same cURL from the dialplan and get the results to process.

I know I’m using cURL in a wrong way, but, till now, the remote website is reacting only on system level commands as showed above.
I have tried with CID superfecta, but I couldn’t adapt this cURL command to any of the existing schemes. (PBXact)
About, AGI, I’m new in this field, so it will take a little time to better understand how it works.
Meanwhile I have succeeded to make it work somehow, creating 4 different
exe files which are creating the new file, input the result of the query (caller name) in the new file, reading and setting the caller’s name from the new file and then erasing the new file.
I have to call each exe file at the right step. It’s a primitive way but
it’s a start.
On the other way I’m expecting to have a mess when I will receive two or more simultaneous calls, because that new file will be written with more and different caller names in the same time. So, we will roll the dice to see who is who. :smile:
Is it possible to adapt this script on channel level!? I mean I will create
as many different files as available channels (8 on sip trunk).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.